1557. Can you answer these queries iiproblem code: gss2
Being a completist and a simplist, kid Yang Zhe cannot solve but get wrong answer from most of the OI problems. and he refuse to write two program of same kind at all. so he always failes in contests.
When having a contest, Yang Zhe looks at the score of every problems first. for the problems of the same score, Yang Zhe will do only one of them. if he's lucky enough, he can get all the scores wanted.
Amber is going to hold a contest in spoj. She has made a listNCandidate problems, which fit Yang Zhe very well. so Yang Zhe can solve any problem he want. amber lined up the problems, began to select. she will select a subsequence of the list as the final problems. being a girl of great compassion, she 'd like to select such a subsequence (can be empty) that Yang Zhe will get the maximal score over all the possible subsequences.
Amber found the subsequence easily after a few minutes. To make things harder, Amber decided that, Yang Zhe can take this contest only if Yang Zhe can answer herQQuestions. The question is: if the final problems are limited to be a subsequenceList[X..Y] (1 <=X<=Y<= N), what's the maximal possible score Yang Zhe can get?
As we know, Yang Zhe is a bit idiot (so why did he solve the problem with a negative score ?), He got wrong answer again... tell him the correct answer!
Input
- Line 1: integerN(1 <=N<= 100000 );
- Line 2:NIntegers denoting the score of each problem, each of them is a integer in range [-100000,100 000];
- Line 3: integerQ(1 <=Q<= 100000 );
- Line 3 +I(1 <=I<=Q): Two integersXAndYDenotingITh question.
Output
- LineI: A single integer, the answer toITh question.
Example
Input:94-2-2 3-1-4 2 2-631 21 54 9Output:453
I have seen abnormal line tree, but I have never seen such abnormal lines ......
In my opinion, the wonderful thing about the passing of the lazy tag of this line segment tree is that it uses a hidden rule passed by the lazy Tag: that is, if a tag is changed, it means there are no other labels above it, therefore, when a tag is put down, the time domain marked below does not overlap with the tag.
I do the reference of the question: http://blog.csdn.net/acm_cxlove/article/details/7854526 (note: the question of the Mark did not open long)
Two lazy tags: lazy: the number of added lazy2 values in the corresponding time interval.
Maintain two values: mx: current maximum value of this interval his: historical maximum value of this interval
For more information, see connection question.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAXQ 110000#define MAXR 200200#define VAL1 100100#define MAXN 101000#define MAXT 410000#define lch (now<<1)#define rch (now<<1^1)#define INFL 0x3f3f3f3f3f3f3f3fLL#ifdef WIN32#define LL "%I64d"#else #define LL "%lld"#endiftypedef long long qword;int n,m;struct node{ int l,r; qword mx; qword his; qword lazy;//delta qword lazy2;//delta_max}tree[MAXT];void down(int now){ if (tree[now].l==tree[now].r) { tree[now].lazy=0; tree[now].lazy2=0; return ; } tree[lch].lazy2=max(tree[lch].lazy2,tree[lch].lazy+tree[now].lazy2); tree[rch].lazy2=max(tree[rch].lazy2,tree[rch].lazy+tree[now].lazy2); tree[lch].his=max(tree[lch].his,tree[lch].mx+tree[now].lazy2); tree[rch].his=max(tree[rch].his,tree[rch].mx+tree[now].lazy2); tree[lch].mx+=tree[now].lazy; tree[rch].mx+=tree[now].lazy; tree[lch].lazy+=tree[now].lazy; tree[rch].lazy+=tree[now].lazy; tree[now].lazy=0; tree[now].lazy2=0;}void up(int now){ if (tree[now].l==tree[now].r)return ; tree[now].mx=max(tree[lch].mx,tree[rch].mx); tree[now].his=max(tree[lch].his,tree[rch].his);}void build_tree(int now,int l,int r){ tree[now].l=l; tree[now].r=r; tree[now].his=0; tree[now].lazy=0; tree[now].lazy2=0; tree[now].mx=0; if (l==r) { return ; } int mid=(l+r)/2; build_tree(lch,l,mid); build_tree(rch,mid+1,r);}void add_val(int now,int l,int r,qword z){ if (l==tree[now].l&&r==tree[now].r) { tree[now].lazy+=z; tree[now].lazy2=max(tree[now].lazy2,tree[now].lazy); tree[now].mx+=z; tree[now].his=max(tree[now].mx,tree[now].his); return ; } down(now); int mid=(tree[now].l+tree[now].r)/2; if (r<=mid) { add_val(lch,l,r,z); up(now); return ; } if (mid<l) { add_val(rch,l,r,z); up(now); return ; } add_val(lch,l,mid,z); add_val(rch,mid+1,r,z); up(now);}qword get_max(int now,int l,int r){ if (l==tree[now].l&&r==tree[now].r) { return tree[now].his; } int mid=(tree[now].l+tree[now].r)/2; down(now); if (r<=mid) { return get_max(lch,l,r); } if (mid<l) { return get_max(rch,l,r); } qword t; t= max(get_max(lch,l,mid),get_max(rch,mid+1,r)); up(now); return t;}int num[MAXN];int rec[MAXR];int prev[MAXN];struct qur_t{ int x,y,id; qword ans;}qur[MAXQ];bool cmp_y(const qur_t &q1,const qur_t &q2){ return q1.y<q2.y;}bool cmp_id(const qur_t &q1,const qur_t &q2){ return q1.id<q2.id;}int main(){ freopen("input.txt","r",stdin); int i,j,k,x,y,z; scanf("%d",&n); memset(rec,-1,sizeof(rec)); for (i=0;i<n;i++) { scanf("%d",&num[i]); prev[i]=rec[num[i]+VAL1]; rec[num[i]+VAL1]=i; } scanf("%d",&m); for (i=0;i<m;i++) { scanf("%d%d",&qur[i].x,&qur[i].y); qur[i].x--; qur[i].y--; qur[i].id=i; } sort(qur,&qur[m],cmp_y); int now=-1; build_tree(1,0,n-1); for (i=0;i<m;i++) { while (now<qur[i].y) { now++; // cout<<"Add:"<<prev[now]+1<<"~"<<now<<":"<<num[now]<<endl; add_val(1,prev[now]+1,now,num[now]); } // cout<<"Query:"<<qur[i].x<<"~"<<qur[i].y<<endl; qur[i].ans=get_max(1,qur[i].x,qur[i].y); } sort(qur,&qur[m],cmp_id); // cout<<"a"<<endl; for(i=0;i<m;i++) { printf(LL "\n",qur[i].ans); }}