SGU 311. Ice-cream Tycoon(线段树)
2014-05-02 12:44
344 查看
311. Ice-cream Tycoon
[align=center]Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes[/align]
[align=center]input: standard
output: standard[/align]
You've recently started an ice-cream business in a local school. During a day you have many suppliers delivering the ice-cream for you, and many students buying it from you. You are not allowed to set the prices, as you are told the price for each piece of ice-cream by the suppliers.
The day is described with a sequence of queries. Each query can be either
ARRIVE n c
, meaning that a supplier has delivered n pieces of ice-cream priced c each to you, or
BUY n t
, meaning that a student wants to buy n pieces of ice-cream, having a total of t money. The latter is processed as follows: in case n cheapest pieces of ice-cream you have cost no more than t (together), you sell those n cheapest pieces to the student; in case they cost more, she gets nothing. You start the day with no ice-cream.
For each student, output
HAPPY
if she gets her ice-cream, and
UNHAPPY
if she doesn't.
[align=left]Input[/align]
The input file contains between 1 and 105 queries (inclusive), each on a separate line. The queries are formatted as described above, either
ARRIVE n c
or
BUY n t
, 1 ≤ n, c ≤ 106, 1 ≤ t ≤ 1012.
[align=left]Output[/align]
For each
BUY
-query output one line, containing either the word
HAPPY
or the word
UNHAPPY
(answers should be in the same order as the corresponding queries).
[align=left]Example(s)[/align]
sample input | sample output |
ARRIVE 1 1 ARRIVE 10 200 BUY 5 900 BUY 5 900 BUY 5 1000 | HAPPY UNHAPPY HAPPY |
线段树维护每个点有的个数和总和。
需要给某个点加上一个个数,清空一个区间的值,查询一个区间的总价值和。
需要离线下,离散化处理、
/* *********************************************** Author :kuangbin Created Time :2014/5/2 11:48:25 File Name :E:\2014ACM\专题学习\数据结构\线段树\SGU311.cpp ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int MAXN = 100010; struct Node { int l,r; long long num; long long sum; int c;//清空标记 }segTree[MAXN*3]; int x[MAXN]; void push_down(int i) { if(segTree[i].l == segTree[i].r)return; if(segTree[i].c != -1) { segTree[i<<1].sum = segTree[(i<<1)|1].sum = 0; segTree[i<<1].num = segTree[(i<<1)|1].num = 0; segTree[i<<1].c = segTree[(i<<1)|1].c = 0; segTree[i].c = -1; } } void push_up(int i) { if(segTree[i].l == segTree[i].r)return; segTree[i].sum = segTree[i<<1].sum + segTree[(i<<1)|1].sum; segTree[i].num = segTree[i<<1].num + segTree[(i<<1)|1].num; } void build(int i,int l,int r) { segTree[i].l = l; segTree[i].r = r; segTree[i].sum = segTree[i].num = 0; segTree[i].c = -1; if(l == r)return; int mid = (l+r)/2; build(i<<1,l,mid); build((i<<1)|1,mid+1,r); } void Add(int i,int c,int n) { segTree[i].sum += (long long)c*n; segTree[i].num += n; if(x[segTree[i].l] == c && x[segTree[i].r] == c) return; push_down(i); if(c <= x[segTree[i<<1].r])Add(i<<1,c,n); else Add((i<<1)|1,c,n); } //查询买前n个需要的钱 long long query(int i,int n) { if(segTree[i].l ==segTree[i].r) { return (long long)n*x[segTree[i].l]; } push_down(i); if(segTree[i<<1].num >= n)return query(i<<1,n); else return segTree[i<<1].sum + query((i<<1)|1,n-segTree[i<<1].num); } //清除前n个 void clear(int i,int n) { if(segTree[i].l == segTree[i].r) { segTree[i].num -= n; segTree[i].sum = segTree[i].num * x[segTree[i].l]; return; } push_down(i); if(segTree[i<<1].num >= n)clear(i<<1,n); else { clear((i<<1)|1,n-segTree[i<<1].num); segTree[i<<1].num = segTree[i<<1].sum = 0; segTree[i<<1].c = 0; } push_up(i); } struct Query { char op[10]; int n; long long c; }q[MAXN]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n = 0; int tot = 0; while(scanf("%s%d%I64d",&q .op,&q .n,&q .c) == 3) { if(q .op[0] == 'A') x[tot++] = q .c; n++; } sort(x,x+tot); tot = unique(x,x+tot) - x; build(1,0,tot-1); for(int i = 0;i < n;i++) { if(q[i].op[0] == 'A') Add(1,q[i].c,q[i].n); else { if(segTree[1].num < q[i].n)printf("UNHAPPY\n"); else { if(query(1,q[i].n) > q[i].c)printf("UNHAPPY\n"); else { printf("HAPPY\n"); clear(1,q[i].n); } } } } return 0; }
相关文章推荐
- 互联网产品2
- 图的连通性-kosaraju算法学习
- unity3d 应用内分享
- 某些MySQL版本后台修改信息后乱码修复方法
- Unity3D脚印6——模型动画
- 如何判断Edittext是否为空
- 图论强连通专题:POJ2762
- POJ 3565 Ants(匹配? 计算几何!)
- 投掷色子系列编程题
- xp snapshot.
- Unity3d 经验小结
- java中static修饰符的特点
- MFC的消息机制
- 互联网基础产品
- linux下多线程的创建与等待详解
- unity3d iPhone文件目录介绍
- MPC8313ERDB不新建pkg包,把文件放进Ramdisk
- 图论强连通专题:POJ2186
- Java类和对象
- 【DataStructure&AlgorithmInJava】Ch02-HighArrayDemo