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. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  2. OpenMP对于嵌套循环应该添加多少个parallel for 分类: OpenMP C/C++ Linux 2015-04-27 14:48 53人阅读 评论(0) 收藏

    一个原则是:应该尽量少的使用parallelfor, 因为parallel for也需要时间开销.即: (1)如果外层循环次数远远小于内层循环次数,内层循环较多时,将parallel for加在内层循 ...

  3. kuangbin_MST C (POJ 2031)

    全程double精度就能过了 间接0距离不用管 prim自动连起来的 G++交的话只能用%f输出 C++的话加不加l都可以 (这么说以后用%f肯定不会错咯) 不过我不懂为什么他们的空间时间差了好多倍. ...

  4. myeclipse启动tomcat报错cannot find a free socket for debugger

    解决办法,命令行输入netsh winsock reset,winsock是Windows网络编程接口,winsock工作在应用层,它提供与底层传输协议无关的高层数据传输编程接口 netsh wins ...

  5. win7 下 arp 绑定mac和Ip

    我们都知道直接执行arp -s 命令即可绑定IP和MAC地址,但是在Win7下会遇到不能运行arp -s 进行静态mac绑定的情况,提示“ARP 项添加失败: 拒绝访问.”(英文版提示:The ARP ...

  6. Spring源码学习之:ClassLoader学习(3)

    ClassLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回 这个类的class对象. 1.1 几个相关概念Clas ...

  7. 切换到android studio环境

    下载: android-studio-bundle-135.1740770-windows.exe sysimg_arm-21_r03.zip 设置环境变量: ANDROID_SDK_HOME     ...

  8. Hadoop的I/O操作

    HDFS的数据完整性 检验数据是否损坏最常见的措施是:在数据第一次引入系统时计算校验和并在数据通过一个不可靠通道进行传输时再次计算校验和,这样就能发现数据是否被损坏.HDFS会对写入的所有数据计算校验 ...

  9. [Spring MVC] - 表单提交

    Spring MVC自带的表单标签比较简单,很多时候需要借助EL和JSTL来完成. 下面是一个比较简单的表单提交页面功能: 1.User model package com.my.controller ...

  10. ios crash 日志分析

    以下内容来自网络 https://coderwall.com/p/ezdcmg/symbolicating-an-ios-crash-log-without-the-original-dsym-fil ...