Osaisen Choudai!
Time Limit: 3 Seconds Memory Limit: 65536 KB
A saisenbako (賽銭箱, offertory, or religious collection box) is situated in front of the hall of Hakurei Shrine. Hakurei Reimu (博麗霊夢), the miko of Hakurei Shrine, may ask the friends who are visting her to throw some saisen (賽銭, offerings, or coins) into it. Reimu wants as much saisen as possible. Howerver, she cannot ask for saisen too frequently, because this will make her friends refuse to offer any more. On the other hand, if she doesn't ask for saisen for a very long period, her friends will become not be willing to offer any more. Generally, at the i-th day, if she asks for saisen, then she will get si saisen, what's more, she cannot ask again for xi days (including the i-th day), and she should ask again in yi days (including the i-th day).
Given si, xi and yi for n days, how much saisen can Reimu get at most in these days if she asks saisen at the first day?
Input
The are multiple cases. Each case begins with a line of integer 0 < n < 50000, then n lines, each contains 3 positive integers: si, and xi < yi. Process to the end of file.
Output
For each test cases, output an integer, the maximun amount of saisen Reimu can get, in a seperate line. It's guaranteed that the answer always fit into a 32-bit signed integer.
Sample Input
3 1 1 2 2 2 3 3 3 4 3 1 1 3 2 2 4 3 3 5 4 10 3 10 7 1 7 5 2 5 1 1 2 5 1 1 9 10 3 10 7 1 7 5 2 5 1 1 2
Sample Output
3 4 11 13
References
Author: WU, Zejun
Source: ACM × Touhou
Contest: ZOJ Monthly, August 2010
#include<stdio.h>
#include<algorithm>
using namespace std;
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
struct Seg_tree
{
int left,right;//注意使用__int64,这个是看题目中是否有要求
int maxn,add;//用于标记下传,否则必TLE
int clamid(){ return (left+right)>>1;}
}tree[50001*3]; //注意是3倍的关系
int n;
void build(int l,int r,int idx)
{
tree[idx].left=l;
tree[idx].right=r;
tree[idx].add=0;//标记用的
tree[idx].maxn=0;
if(l==r) return;
int mid=tree[idx].clamid();
build(l,mid,LL(idx));
build(mid+1,r,RR(idx));
}
void Update(int l,int r,int idx,int c)
{
if(l<=tree[idx].left&&r>=tree[idx].right)
{
tree[idx].add=max(c,tree[idx].add);
tree[idx].maxn=max(c,tree[idx].maxn);
return ;
}
if(tree[idx].add) //要是父节点管不住了,再传给子节点
{
tree[LL(idx)].add=max(tree[idx].add,tree[LL(idx)].add);
tree[LL(idx)].maxn=max(tree[LL(idx)].add,tree[LL(idx)].maxn);
tree[RR(idx)].add=max(tree[idx].add,tree[RR(idx)].add);
tree[RR(idx)].maxn=max(tree[RR(idx)].add,tree[RR(idx)].maxn);
tree[idx].add=0;
}
int mid=tree[idx].clamid();
if(r>mid) Update(l,r,RR(idx),c);
if(l<=mid) Update(l,r,LL(idx),c);
tree[idx].maxn=max(tree[LL(idx)].maxn,tree[RR(idx)].maxn);
}
int query(int x,int idx)
{
if(tree[idx].left==tree[idx].right) return tree[idx].maxn;
if(tree[idx].add)
{
tree[LL(idx)].add=max(tree[idx].add,tree[LL(idx)].add);
tree[LL(idx)].maxn=max(tree[LL(idx)].add,tree[LL(idx)].maxn);
tree[RR(idx)].add=max(tree[idx].add,tree[RR(idx)].add);
tree[RR(idx)].maxn=max(tree[RR(idx)].add,tree[RR(idx)].maxn);
tree[idx].add=0;
}
int mid=tree[idx].clamid();
if(x<=mid) return query(x,LL(idx));
else if(x>mid) return query(x,RR(idx));
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
build(1,n+1,1);
for(int i=1; i<=n; i++)
{
int a,b,c;
scanf("%d%d%d",&c,&a,&b);
int t=query(i,1);
if(t!=0||i==1)
{
int t1,t2;
if(i+a>n) t1=n+1;else t1=i+a;
if(i+b-1>n) t2=n+1;else t2=i+b-1;
Update(t1,t2,1,t+c);
}
}
int maxn=tree[1].maxn;
printf("%d/n",maxn);
}
return 0;
}