Description





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 nc

, meaning that a supplier has delivered n pieces of ice-cream priced c each to you, or

BUY nt

, 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 10 5 queries (inclusive), each on a separate line. The queries are formatted as described above, either

ARRIVE nc

or

BUY nt

, 1 ≤ nc ≤ 10 6, 1 ≤ t ≤ 10 12.

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

Sample Input

sample input
sample output
ARRIVE 1 1
ARRIVE 10 200
BUY 5 900
BUY 5 900
BUY 5 1000
HAPPY
UNHAPPY
HAPPY

题意:一个商店,有两种操作:(1)ARRIVE n c表示进货n个,每一个c元。(2)BUY n t表示一个买货的人要买n个,一共拿了t元钱。假设如今店里的货的数量大于等于n且最廉价的n个的价格小于等于t则将最廉价的卖给他。否则不卖。

思路:离线的线段树,我们以价格作为结点,然后离散化,好久没做。看了final爷kuangbing的题解,注意的是优先处理最小的n个

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
typedef long long ll;
using namespace std;
const int maxn = 100010; struct Node {
int l, r;
ll num;
ll sum;
int flag;
} segTree[maxn<<2];
int x[maxn]; void pushdown(int x) {
if (segTree[x].l == segTree[x].r)
return;
if (segTree[x].flag != -1) {
segTree[lson(x)].sum = segTree[rson(x)].sum = 0;
segTree[lson(x)].num = segTree[rson(x)].num = 0;
segTree[lson(x)].flag = segTree[rson(x)].flag = 0;
segTree[x].flag = -1;
}
} void pushup(int x) {
if (segTree[x].l == segTree[x].r)
return;
segTree[x].sum = segTree[lson(x)].sum + segTree[rson(x)].sum;
segTree[x].num = segTree[lson(x)].num + segTree[rson(x)].num;
} void build(int x, int l, int r) {
segTree[x].r = r;
segTree[x].l = l;
segTree[x].sum = segTree[x].num = 0;
segTree[x].flag = -1;
if (l == r) return;
int mid = l + r >> 1;
build(lson(x), l, mid);
build(rson(x), mid+1, r);
} void Add(int i, int c, int n) {
segTree[i].sum += (ll) c * n;
segTree[i].num += n;
if (x[segTree[i].l] == c && x[segTree[i].r] == c)
return;
pushdown(i); if (c <= x[segTree[lson(i)].r])
Add(lson(i), c, n);
else Add(rson(i), c, n);
} ll query(int i, int n) {
if (segTree[i].l == segTree[i].r) {
return (ll) n * x[segTree[i].l];
}
pushdown(i);
if (segTree[lson(i)].num >= n)
return query(lson(i), n);
else return segTree[lson(i)].sum + query(rson(i), n-segTree[lson(i)].num);
} 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;
} pushdown(i);
if (segTree[lson(i)].num >= n)
clear(lson(i), n);
else {
clear(rson(i), n-segTree[lson(i)].num);
segTree[lson(i)].num = segTree[lson(i)].sum = 0;
segTree[lson(i)].flag = 0;
}
pushup(i);
} struct Query {
char op[10];
int n;
ll c;
} q[maxn]; int main() {
int n = 0;
int tot = 0;
while (scanf("%s%d%lld", q[n].op, &q[n].n, &q[n].c) != EOF) {
if (q[n].op[0] == 'A')
x[tot++] = q[n].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;
}

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

  1. SGU 531. Bonnie and Clyde 线段树

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

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

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

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

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

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

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

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

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

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

    311. Ice-cream Tycoon Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: standar ...

  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. LayuI固定块关闭

    1.近期项目使用了layui的固定块,但是当到某个独立页面时,固定块还在,就显得突兀: 2.通过F12查看,发现代码: <ul class="layui-fixbar" st ...

  2. 服务器编程心得(四)—— 如何将socket设置为非阻塞模式

    1. windows平台上无论利用socket()函数还是WSASocket()函数创建的socket都是阻塞模式的: SOCKET WSAAPI socket( _In_ int af, _In_ ...

  3. [题解] cogs 1669 神秘的咒语

    http://cogs.pro:8080/cogs/problem/problem.php?pid=1669 "The Real Incantation is Their Common In ...

  4. C++知识点总结(纯C++!!)

    1.重载函数是否能够通过函数返回值的类型不同来区分? 不可以.因为在C++编程中,函数的返回值可以忽略(不使用其返回值),程序中调用此时函数名相同和参数相同的两个函数对编译器和程序员来说是没有办法区分 ...

  5. django扩展User模型(model),profile

    from django.contrib.auth.models import User # Create your models here. class Profile(models.Model): ...

  6. 解决hibernate产生的id序列或者setXX不能同步到数据库到问题(this.hibernateTemplate.flush();hibernateTemplate.getSessionFactory().getCurrentSession().connection().commit())

    通过WarehouseInventoryPreLog warehouseInventoryPreLog = new WarehouseInventoryPreLog();产生一个id序列 如果不flu ...

  7. php基础语句 变量 符号

    中心主题 标记与注释 // /* */ 输出语句 echo输出 echo可以输出多个字符串,逗号隔开 print输出 print只能输出一个字符串,返回true或false print_r() 可以把 ...

  8. ERP类系统设计学习

    文章:分布式.服务化的ERP系统架构设计 文章的方法是对系统进行拆分,拆分成多个子系统.

  9. [Istio]流量管理API v1alpha3路由规则

    Istio提供一个API进行流量管理,该API允许用户将请求路由到特定版本的服务,为弹性测试注入延迟和失败,添加断路器等,所有这些功能都不必更改应用程序本身的代码.Istio 1.0中引入新的流量管理 ...

  10. Ubuntu启用IPv6上google的方法

    Pv6就是我们通常所说的互联网协议,是TCP/IP的核心协议,那么在Linux下如何开启IPv6呢?下面以Ubuntu为例,给大家介绍下Ubuntu启用IPv6的方法. 方法: $sudo apt-g ...