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. R中的统计检验函数

    正态性W检验 shapiro.test()用Shapiro-Wilk W统计量做数据的正态性检验. 经验分布的Kolmogorov-Smirnov检验 ks.test()Kolmogorov-Smir ...

  2. Android学习五:Content Provider 使用

    1ContentProvider相关知识1.1在安卓应用中,通过文件方式对外共享数据,需要进行文件操作读写数据:采用sharedpreferences共享数据,需要使用sharedpreference ...

  3. linux网络虚拟化

    图解几个与Linux网络虚拟化相关的虚拟网卡-VETH/MACVLAN/MACVTAP/IPVLAN http://smilejay.com/2012/08/qemu-kvm-networking-m ...

  4. 你所不了解的setTimeout

    看到了一篇不错的文章<你会用setTimeout吗 >,转载过来的,改了个名字,一下子感觉搞大上了,嘎嘎. 加了几个关于 setTimeout 和setInterval的小知识: 关于se ...

  5. .net中常用的几种页面间传递参数的方法

    转自:http://www.cnblogs.com/lxshanye/archive/2013/04/11/3014207.html 参考:http://www.cnblogs.com/zhangka ...

  6. SQL 查询某个表被哪些存储过程使用到

    --1.查询某个表被哪些存储过程使用到 : select distinct object_name(id) from syscomments where id in (select object_id ...

  7. SQL2005 : 如何在SQL Server Profiler (事件查看器)中 跟踪查看死锁恢复

    SQL Profiler 通过 SQL Profiler 工具程序,可监控应用程序如何访问数据库引擎.普通来说,当系统性能需要优化或是应用程序对数据库访问的结果不合预期,都可以使用该工具确认视图问题所 ...

  8. view--4种Android获取View宽高的方式

    有时我们会有基于这样的需求,当Activity创建时,需要获取某个View的宽高,然后进行相应的操作,但是我们在onCreate,onStart中获取View的大小,获取到的值都是0,只是由于View ...

  9. mysql登录基本语句

    默认密码:root mysql 显示所有的数据库,代码如下: mysql> show databases; mysql> show tables; MySQL显示命令二.显示命令 1.显示 ...

  10. 三、Distributing Maya Plugins

    For example, a fully implemented render utility node will have at least three files: the plug-in fil ...