311. Ice-cream Tycoon

Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard

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.

Input

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 ≤ nc ≤ 106, 1 ≤ t ≤ 1012.

Output

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).

Example(s)
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 = ;
struct Node
{
int l,r;
long long num;
long long sum;
int c;//清空标记
}segTree[MAXN*];
int x[MAXN];
void push_down(int i)
{
if(segTree[i].l == segTree[i].r)return;
if(segTree[i].c != -)
{
segTree[i<<].sum = segTree[(i<<)|].sum = ;
segTree[i<<].num = segTree[(i<<)|].num = ;
segTree[i<<].c = segTree[(i<<)|].c = ;
segTree[i].c = -;
}
}
void push_up(int i)
{
if(segTree[i].l == segTree[i].r)return;
segTree[i].sum = segTree[i<<].sum + segTree[(i<<)|].sum;
segTree[i].num = segTree[i<<].num + segTree[(i<<)|].num;
}
void build(int i,int l,int r)
{
segTree[i].l = l;
segTree[i].r = r;
segTree[i].sum = segTree[i].num = ;
segTree[i].c = -;
if(l == r)return;
int mid = (l+r)/;
build(i<<,l,mid);
build((i<<)|,mid+,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<<].r])Add(i<<,c,n);
else Add((i<<)|,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<<].num >= n)return query(i<<,n);
else return segTree[i<<].sum + query((i<<)|,n-segTree[i<<].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<<].num >= n)clear(i<<,n);
else
{
clear((i<<)|,n-segTree[i<<].num);
segTree[i<<].num = segTree[i<<].sum = ;
segTree[i<<].c = ;
}
push_up(i);
}
struct Query
{
char op[];
int n;
long long c;
}q[MAXN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n = ;
int tot = ;
while(scanf("%s%d%I64d",&q[n].op,&q[n].n,&q[n].c) == )
{
if(q[n].op[] == 'A')
x[tot++] = q[n].c;
n++;
}
sort(x,x+tot);
tot = unique(x,x+tot) - x;
build(,,tot-);
for(int i = ;i < n;i++)
{
if(q[i].op[] == 'A')
Add(,q[i].c,q[i].n);
else
{
if(segTree[].num < q[i].n)printf("UNHAPPY\n");
else
{
if(query(,q[i].n) > q[i].c)printf("UNHAPPY\n");
else
{
printf("HAPPY\n");
clear(,q[i].n);
}
}
}
}
return ;
}

SGU 311. Ice-cream Tycoon(线段树)的更多相关文章

  1. SGU - 311 Ice-cream Tycoon(线段树)

    Description You've recently started an ice-cream business in a local school. During a day you have m ...

  2. SGU 531. Bonnie and Clyde 线段树

    531. Bonnie and Clyde 题目连接: http://acm.sgu.ru/problem.php?contest=0&problem=531 Description Bonn ...

  3. SGU 319 Kalevich Strikes Back(线段树扫描线)

    题目大意: n个矩形,将一个大矩形分成 n+1 块.矩形之间不重合,可是包括.求这n+1个矩形的面积 思路分析: 用线段树记录他们之间的父子关系.然后dfs 计算面积. 当给出的矩形上边的时候,就要记 ...

  4. Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心

    题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...

  5. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  6. bzoj3252攻略(线段树+dfs序)

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 562  Solved: 238[Submit][Status][Discuss] D ...

  7. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  8. SGU 319. Kalevich Strikes Back (线段树)

    319. Kalevich Strikes Back Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...

  9. bzoj千题计划311:bzoj5017: [Snoi2017]炸弹(线段树优化tarjan构图)

    https://www.lydsy.com/JudgeOnline/problem.php?id=5017 暴力: 对于每一个炸弹,枚举所有的炸弹,看它爆炸能不能引爆那个炸弹 如果能,由这个炸弹向引爆 ...

随机推荐

  1. 如何让WordPress支持上传更多文件类型

    如何让WordPress支持上传更多文件类型   可以在functions.php中这样写: 1 2 3 4 5 6 7 8 9 add_filter('upload_mimes', 'wpdit_f ...

  2. Node.js热部署方式

    1. supervisor: 2. hotnode: 3. node-dev: 4. nodemon:

  3. Nim游戏变种——取纽扣游戏

    (2017腾讯实习生校招笔试题)Calvin和David正在玩取纽扣游戏,桌上一共有16个纽扣,两人轮流来取纽扣,每人每次可以选择取1个或3个或6个(不允许不取),谁取完最后的纽扣谁赢.Cavin和D ...

  4. jquery获得图片的真实大小

    $(function(){ var imgSrc = $("#image").attr("src"); getImageWidth(imgSrc,functio ...

  5. FreeMarker模板引擎

    现在开发的项目,也是基于SOA架构,每个功能接口都是用WebService实现,Web服务的通信协议就是用XML来传输. 以前写WebService都是自动生成XML,没想到这项目竟然要自己定义XML ...

  6. 教你如何在word中像LaTex那样打出漂亮的数学公式

    转载自: http://blog.csdn.net/ibingow/article/details/8613556 记得很久以前在word里打数学公式很痛苦,要用鼠标点啊点,效率奇低,包括像MathT ...

  7. GC之七--gc日志分析工具

    性能测试排查定位问题,分析调优过程中,会遇到要分析gc日志,人肉分析gc日志有时比较困难,相关图形化或命令行工具可以有效地帮助辅助分析. Gc日志参数 通过在tomcat启动脚本中添加相关参数生成gc ...

  8. Linux下*.tar.gz文件解压缩命令

    1.压缩命令: 命令格式:tar  -zcvf   压缩文件名.tar.gz   被压缩文件名 可先切换到当前目录下.压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式:tar  -z ...

  9. svn 提交失败

    刚刚使用SVN 提交代码时提示失败. svn: Commit failed (details follow):svn: Can't open file '/home/svn/project/db/tx ...

  10. Delphi完成的断点续传例子 转

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...