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. struts2 ajax的一种实现方式

    /** * ajax请求,通过省份id获取学习中心 */ public void getSitesByPid() { HttpServletResponse response = ServletAct ...

  2. Python Webk框架学习 Flask

    Flask是一个使用Python编写的轻量级Web应用框架.基于Werkzeug WSGI工具箱和Jinja2 模板引擎. Flask使用BSD授权.Flask也被称为“microframework” ...

  3. linux cpu性能测试

    sysbench --test=cpu --cpu-max-prime=20000000 run --num-threads=4 mpstat -P ALL 1 1000000

  4. [洛谷OJ] P1114 “非常男女”计划

    洛谷1114 “非常男女”计划 本题地址:http://www.luogu.org/problem/show?pid=1114 题目描述 近来,初一年的XXX小朋友致力于研究班上同学的配对问题(别想太 ...

  5. Unity Shader——Writing Surface Shaders(2)——Custom Lighting models in Surface Shaders

    Surface Shader中的自定义光照模型 当你在编写 Surface Shaders 时,是在描述一个表面的属性(反射颜色.法线……),而且光的交互过程是由一个光照模型来计算的.内建的光照模型有 ...

  6. LINQ查询操作符之Select、Where、OrderBy、OrderByDescending、GroupBy、Join、GroupJoin及其对应的查询语法

    介绍    ·Select - Select选择:延迟    ·Where - Where查询:延迟    ·OrderBy - 按指定表达式对集合正序排序:延迟    ·OrderByDescend ...

  7. FFmpeg.exe使用随笔

    一.将图片合成为MP4 1.将照片合成为h264 ffmpeg -framerate 12 -i %3d.png -c:v libx264 -pix_fmt yuv420p test.h2642.将h ...

  8. python的中文编码问题

    下文转自 http://blog.csdn.net/mayflowers/article/details/1568852 1.        在Python中使用中文 在Python中有两种默认的字符 ...

  9. 转:WIN7上搭建Windows Phone 8 开发环境——VMware Workstation下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法

    转自:http://www.cnblogs.com/shaddock2013/p/3155024.html 最近在试着在Windows 7上搭建Windows Phone 8的开发调试环境,使用的是V ...

  10. ios 使用可视化工具charles转换pcap文件,进行流量统计(通过tcpdump抓包)

    环境准备:使用mac电脑,下载xcode,Charles 连接iPhone手机,打开xcode-window-devices-查看设备UDID 打开终端:rvictl –s 设备号 ,查看虚拟端口号 ...