SGU 311. Ice-cream Tycoon(线段树)
311. Ice-cream Tycoon
Memory limit: 65536 kilobytes
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.
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 ≤ n, c ≤ 106, 1 ≤ t ≤ 1012.
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 output |
ARRIVE 1 1 |
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(线段树)的更多相关文章
- SGU - 311 Ice-cream Tycoon(线段树)
Description You've recently started an ice-cream business in a local school. During a day you have m ...
- SGU 531. Bonnie and Clyde 线段树
531. Bonnie and Clyde 题目连接: http://acm.sgu.ru/problem.php?contest=0&problem=531 Description Bonn ...
- SGU 319 Kalevich Strikes Back(线段树扫描线)
题目大意: n个矩形,将一个大矩形分成 n+1 块.矩形之间不重合,可是包括.求这n+1个矩形的面积 思路分析: 用线段树记录他们之间的父子关系.然后dfs 计算面积. 当给出的矩形上边的时候,就要记 ...
- Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心
题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- bzoj3252攻略(线段树+dfs序)
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 562 Solved: 238[Submit][Status][Discuss] D ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- SGU 319. Kalevich Strikes Back (线段树)
319. Kalevich Strikes Back Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...
- bzoj千题计划311:bzoj5017: [Snoi2017]炸弹(线段树优化tarjan构图)
https://www.lydsy.com/JudgeOnline/problem.php?id=5017 暴力: 对于每一个炸弹,枚举所有的炸弹,看它爆炸能不能引爆那个炸弹 如果能,由这个炸弹向引爆 ...
随机推荐
- Android学习七:new Date使用
1.例子 学习时间函数,并实现了简单的多个按钮监听同一个事件的方法 2.代码 代码很简单,也很清晰 package com.example.datetime; import java.text.Sim ...
- UI Automator Viewer获取手机镜像时报错
使用UI Automator Viewer获取手机镜像时报错,具体信息如下: Error while obtaining UI hierarchy XML file: com.android.ddml ...
- 1、java中常用名字规范
包名:多个单词组成是所有单词字母小写. 类名.接口名:所有单词首字母大写. 变量名.函数名:多单词组成时第一个单词首字母小写,从第二个单词开始首字母大写. 常量名:所有字母大写,单词之间用 “_” 连 ...
- Android 触摸手势基础 官方文档概览2
Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...
- 把自己主要在做的几个工程都传到了GitHub上
GitHub链接 https://github.com/MichaelSuen-thePointer 里面有4个项目,一个是我的C大程大作业,一个3600+行的字典程序,已经弃坑不再更新 还有一个叫w ...
- 【php】assert函数的用法
[php]assert函数的用法 http://www.douban.com/note/217557007/ 2012-06-01 10:32:37 assert这个函数在php语言中是用来判断一 ...
- MyEclipse中查询
在当前文件中查询: Ctrl + F 在整个项目中查询: Ctrl + H(选File Search)
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- Android UI 绘制过程浅析(二)onMeasure过程
前言 View的绘制过程分为 measure.layout.draw三个步骤,接下来对这三个步骤逐一进行研究. measure方法的签名 public final void measure(int w ...
- Mantis 1.2.19 on Windows Server 2012 r2 datacenter 安装及配置随笔
一.前言 新的小团队需要搭建一个缺陷管理的工具,之前用过bugfree,感觉比较适合,但是 禅道不太适合,放弃之,于是又百度推荐的: .JTrac13.BugNet14.BugOnline15.eTr ...