Can you answer these queries II
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
https://www.spoj.com/problems/GSS2/
Description
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 the program of same kind at all. So he is failes in contests.
When had a contest, Yang Zhe looks at the score of every problems first. For the problems of the same score, Yang Zhe would do only one of the them. If he ' s lucky enough, he can get all the scores wanted.
Amber is going-a contest in SPOJ. She has made a list of N candidate 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'll 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 is empty) that Yang Zhe would get the Maxima L score over all the possible subsequences.
Amber found the subsequence easily after a few minutes. To make things harder, Amber decided so, Yang Zhe can take this contest only if Yang Zhe can answer her Q quest Ions. The question Is:if the final problems is limited to be a subsequence of list[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-did he solve the problem with a negative score?), he got wrong Answer again. . Tell him the correct answer!
Input
- Line 1:integer n (1 <= n <= 100000);
- Line 2: N integers denoting the score of all problem, each of the them are a integer in range [-100000, 100000];
- Line 3:integer q (1 <= q <= 100000);
- Linei (1 <= i <= Q): integers X and Y denoting the ith question.
Output
- Line I: A single integer, the answer to the ith question.
Sample Input
94-2-2 3-1-4 2 2-631 21 54 9
Sample Output
453
HINT
Test instructions
Give you the number of n, query the interval maximum contiguous sub-segments, and the same number within the interval is calculated only once
Exercises
No modification operation, very obvious offline line segment tree
Suppose we do not consider the same number of rules that are counted only once, what should we do?
For ever-increasing r, we maintain C[i] from a[i]-a[r] and, obviously, we output Max (C[l],c[l+1],c[l+2]....c[r]) is the answer.
It feels stupid to think about it ...
How do we maintain the same number in the interval calculation only once? For each number, we only maintain (pre[a[i]]+1,i) This interval just fine
And then this problem is solved.
Code:
#include <bits/stdc++.h>using namespaceStd;typedefLong LongSgtreedatatype;structtreenode{intL, R; Sgtreedatatype sum, lazy, cursum, prelazy; voidUpdata (Sgtreedatatype v) {sum+=v; Lazy+=v; Cursum=Max (cursum,sum); Prelazy=Max (Prelazy,lazy); }};treenode tree[500005];inlinevoidPush_down (into) {Sgtreedatatype Prelazy=Tree[o].prelazy; Sgtreedatatype Lazy=Tree[o].lazy; tree[2*o].prelazy = Max (tree[2*o].prelazy,tree[o*2].lazy +Prelazy); tree[2*o].cursum = Max (tree[2*o].cursum,tree[o*2].sum +Prelazy); tree[2*o].lazy + = lazy; tree[2*o].sum + =Lazy; tree[2*o+1].prelazy = Max (tree[2*o+1].prelazy,tree[o*2+1].lazy +Prelazy); tree[2*o+1].cursum = Max (tree[2*o+1].cursum,tree[o*2+1].sum +Prelazy); tree[2*o+1].lazy + = lazy; tree[2*o+1].sum + =Lazy; Tree[o].lazy=0, Tree[o].prelazy =0;} InlinevoidPUSH_UP (into) {Tree[o].sum= Max (tree[2*o].sum,tree[2*o+1].sum); Tree[o].cursum= Max (tree[2*o].cursum,tree[2*o+1].cursum);} InlinevoidBuild_tree (intLintRinto) {Tree[o]. L= L, Tree[o]. R = R,tree[o].sum = Tree[o].lazy = Tree[o].prelazy = Tree[o].cursum =0 ; if(R >L) {intMid = (l+r) >>1; Build_tree (L,mid,o*2); Build_tree (Mid+1, r,o*2+1); }}inlinevoidUpdata (intQlintQr,sgtreedatatype V,into) { intL = Tree[o]. L, R =Tree[o]. R if(QL <= L && R <=QR) Tree[o].updata (v); Else{push_down (o); intMid = (l+r) >>1; if(QL <= mid) Updata (ql,qr,v,o*2); if(QR > Mid) updata (ql,qr,v,o*2+1); PUSH_UP (o); }}inline sgtreedatatype Query (intQlintQrinto) { intL = Tree[o]. L, R =Tree[o]. R if(QL <= L && R <= QR)returntree[o].cursum; Else{push_down (o); intMid = (l+r) >>1; Sgtreedatatype Res=0; if(QL <= mid) Res =max (res, query (QL,QR,2*o)); if(QR > Mid) Res =max (Res,query (QL,QR,2*o+1)); PUSH_UP (o); returnRes; }}intn,m;inta[100005];structnode{intL,r,id;};BOOLCMP (node A,node B) {returna.r<B.R;} Node query[100005];intpos[3000005];Long Longans[1000005];intMain () {memset (pos,0,sizeof(POS)); scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); scanf ("%d",&m); Build_tree (1N1); for(intI=0; i<m;i++) {scanf ("%d%d",&query[i].l,&QUERY[I].R); Query[i].id=i; } sort (Query,query+m,cmp); intN =100005; for(intI=1, j=0; i<=n;i++) {updata (Pos[a[i]+n]+1, I,a[i],1); Pos[a[i]+n]=i; while(j<m&&query[j].r==i) {ans[query[j].id]=query (QUERY[J].L,QUERY[J].R,1); J++; } } for(intI=0; i<m;i++) printf ("%lld\n", Ans[i]);}
Spoj 1557. Can you answer these queries II segment tree