SGU 311. Ice-cream Tycoon

311. Ice-cream TycoonTime limit per test: 3 second(s)
Memory limit: 65536 kilobytesinput: 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 eitherARRIVE n c, meaning that a supplier has delivered n pieces of ice-cream pricedc 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 casen 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 UNHAPPYif she doesn’t.

InputThe input file contains between 1 and 105 queries (inclusive), each on a separate line. The queries are formatted as described above, either ARRIVE n cor BUY n t, 1 ≤ nc ≤ 106, 1 ≤ t ≤ 1012.

OutputFor 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
这个题目一看就是裸的线段树囧。。就是老TLE瀑布汗。。。这次我火了。。写了个带标记的非递归
线段树终于A了内流满面囧。。。
Code:
#include <vector>
#include <algorithm>
#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <time.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define Debug(x) cout<<#x<<"="<<x<<endl;
#define For(i,l,r) for(int i=l;i<=r;i++)
#define tr(e,x) for(typeof(x.begin()) e=x.begin();e!=x.end();e++)
#define printTime cout<<"Time:"<<pre-clock()<<endl;pre=clock();
const int inf=~0U>>1,maxc=1<<20;
using namespace std;
typedef long long ll;
char cmd[100];
#define scan(t)
{
char pt;
while(pt=getchar(),pt<‘0’||pt>’9’);
t=pt-‘0’;
while(pt=getchar(),pt>=’0’&&pt<=’9′)t=t*10+pt-‘0’;
}
bool Mark[1<<21];
ll sum[1<<21]={},num[1<<21]={};
void set(int t)
{
sum[t]=sum[t*2]+sum[t*2+1];
num[t]=num[t*2]+num[t*2+1];
}
void MarkIt(int t)
{
sum[t]=num[t]=0;
Mark[t]=true;
}
void PushDown(int t)
{
if(Mark[t])
MarkIt(t*2),MarkIt(t*2+1);
Mark[t]=false;
}
void Update(int t,ll c)
{
int i,l,r;
for(i=1,l=0,r=maxc;i<maxc;)
{
PushDown(i);i<<=1;
int m=l+r>>1;
if(t>=m)i++,l=m;
else r=m;
}
i=t+maxc;sum[i]+=t*c;num[i]+=c;
for(i>>=1;i;i>>=1)set(i);
}
ll get(int n)
{
if(num[1]<n)return -1;
ll ret=0;int i;
for(i=1;i<maxc;)
{
PushDown(i);i<<=1;
if(num[i]>=n)continue;
ret+=sum[i];n-=num[i];i++;
}
return ll(n)*(i-maxc)+ret;
}
void clear(int n)
{
int i;
for(i=1;i<maxc;)
{
PushDown(i);i<<=1;
if(num[i]>=n)continue;
n-=num[i];MarkIt(i);i++;
}
Update(i-maxc,-n);
}
int main()
{
//freopen("in","r",stdin);
int n;ll c;
while(scanf("%s",cmd)==1)
{
scan(n);scan(c);
if(cmd[0]==’A’)Update(c,n);
else
{
ll tmp=get(n);
if(tmp==-1||tmp>c)
puts("UNHAPPY");
else
puts("HAPPY"),clear(n);
}
}
}

One thought on “SGU 311. Ice-cream Tycoon

Leave a Reply

Your email address will not be published. Required fields are marked *