poj2750 Potted Flower [segment tree]

Seeking a maximum continuous ring to be modified, and not all sub-segment and selected.


First off the ring, on the $ 1 \ sim n $ chain, there is no limit to consider the maximum continuous Select and sub-segment. By far the largest solution only two cases, one is that this one is another.

The first direct routine maintenance, and the second is nothing more than the whole of sub-segments and subtracting the minimum and continuous. Whichever one of the largest on the line.

Then, taking into account if the whole election. In this case it can only be forced to take a second election law. That is the first sentence if the special election law, and equal to the current range, is forced to use the second (certainly prove right, and this is actually flawed, but the answer can not be wrong). So no brain wave segment_tree code can be.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define dbg(x) cerr<<#x<<" = "<<x<<endl
 7 using namespace std;
 8 typedef pair<int,int> pii;
 9 typedef long long ll;
10 template<typename T>inline char MIN(T&A,T B){return A>B?A=B,1:0;}
11 template<typename T>inline char MAX(T&A,T B){return A<B?A=B,1:0;}
12 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
13 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
14 template<typename T>inline T read(T&x){
15     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
16     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
17 }
18 const int N=1e5+7;
19 int A[N];
20 int n,q,x,val;
21 #define lc i<<1
22 #define rc i<<1|1
23 int maxl[N<<2],maxr[N<<2],maxv[N<<2],minl[N<<2],minr[N<<2],minv[N<<2],sumv[N<<2];
24 inline void Pushup(int i){
25     maxl[i]=_max(maxl[lc],sumv[lc]+maxl[rc]),maxr[i]=_max(maxr[rc],sumv[rc]+maxr[lc]);
26     minl[i]=_min(minl[lc],sumv[lc]+minl[rc]),minr[i]=_min(minr[rc],sumv[rc]+minr[lc]);
27     maxv[i]=_max(_max(maxv[lc],maxv[rc]),maxr[lc]+maxl[rc]);
28     minv[i]=_min(_min(minv[lc],minv[rc]),minr[lc]+minl[rc]);
29     sumv[i]=sumv[lc]+sumv[rc];
30 }
31 void build(int i,int L,int R){
32     if(L==R){maxl[i]=maxr[i]=maxv[i]=minl[i]=minr[i]=minv[i]=sumv[i]=A[L];return;}
33     int mid=L+R>>1;build(lc,L,mid),build(rc,mid+1,R),Pushup(i);
34 }
35 void Update(int i,int L,int R){
36     if(L==R){maxl[i]=maxr[i]=maxv[i]=minl[i]=minr[i]=minv[i]=sumv[i]=A[L]=val;return;}
37     int mid=L+R>>1;x<=mid?Update(lc,L,mid):Update(rc,mid+1,R);Pushup(i);
38 }
39 
40 int main(){//freopen("test.in","r",stdin);freopen("test.out","w",stdout);
41     read(n);for(register int i=1;i<=n;++i)read(A[i]);
42     build(1,1,n);
43     read(q);while(q--){
44         read(x),read(val);
45         Update(1,1,n);
46         if(maxv[1]==sumv[1])printf("%d\n",sumv[1]-minv[1]);
47         else printf("%d\n",_max(maxv[1],sumv[1]-minv[1]));
48     }
49     return 0;
50 }

 

Guess you like

Origin www.cnblogs.com/saigyouji-yuyuko/p/11409494.html