ZOJ 3381 Osaisen Choudai! (RMQ+DP,4级)

D - Osaisen Choudai!
Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Appoint description:

Description

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 sisaisen, 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

上海アリス幻樂団

acm_x_touhou

题意:忽略题目背景,就是要收集最多的钱, 如果第i天拿到了si 的钱, 那么第i+x[i] 天 到 第i + y[i] - 1 天必须再拿一次,否则就再也拿不到钱了,当然,第i +x[i]天之前也是拿不到的, 题目要求第一天必须拿。。

思路:dp[x]=s[x]+max(dp[y])  y>=x+a&&y<=x+b

      dp[x]选 x之后x的最优值

  枚举必超,RMQ就完事了。

#include<iostream>
#include<cstring>
#include<cstdio>
#define ll(x) (1<<x)
#define clr(f,z) memset(f,z,sizeof(f))
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int mm=5e4+9;
int rmq[mm][26];
int bit[mm],n,s[mm],x[mm],y[mm];
void initRMQ()
{
  bit[0]=-1;
  FOR(i,1,mm-1)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1];
}
void ST(int x)
{
  for(int j=1;x+ll(j)-1<=n;++j)
    rmq[x][j]=max(rmq[x][j-1],rmq[x+ll(j-1)][j-1]);
}
int RMQ(int l,int r)
{ if(r>n)r=n;
  if(l>r)return 0;
  int t=bit[r-l+1];
  r-=ll(t)-1;
  return max(rmq[l][t],rmq[r][t]);
}
int main()
{
  while(~scanf("%d",&n))
  { initRMQ();clr(rmq,0);
    FOR(i,1,n)
    scanf("%d%d%d",&s[i],&x[i],&y[i]);
    for(int i=n;i>=1;--i)
      rmq[i][0]=s[i]+RMQ(i+x[i],i+y[i]-1),ST(i);
    printf("%d\n",rmq[1][0]);
  }
  return 0;
}




posted @ 2013-09-01 17:03  剑不飞  阅读(152)  评论(0编辑  收藏  举报