(Positions of potted flowers are assigned to index numbers in the range of 1 ... N. The i-th pot and the (i + 1)-th pot are consecutive for any given i (1 <= i < N), and 1st pot is next to N-th pot in addition.)
The board chairman informed the little cat to construct "ONE arc-style cane-chair" for tourists having a rest, and the sum of attractive values of the flowers beside the cane-chair should be as large as possible. You should notice that a cane-chair cannot be a total circle, so the number of flowers beside the cane-chair may be 1, 2, ..., N - 1, but cannot be N. In the above example, if we construct a cane-chair in the position of that red-dashed-arc, we will have the sum of 3+(-2)+1+2=4, which is the largest among all possible constructions.
Unluckily, some booted cats always make trouble for the little cat, by changing some potted flowers to others. The intelligence agency of little cat has caught up all the M instruments of booted cats' action. Each instrument is in the form of "A B", which means changing the A-th potted flowered with a new one whose attractive value equals to B. You have to report the new "maximal sum" after each instruction.
The second line contains N integers, which are the initial attractive value of each potted flower. The i-th number is for the potted flower on the i-th position.
A single integer M (4 <= M <= 100000) in the third input line, and the following M lines each contains an instruction "A B" in the form described above.
Restriction: All the attractive values are within [-1000, 1000]. We guarantee the maximal sum will be always a positive integer.
The situation into two.
1. The number of rings on the whole positive. Minus the sum of all the numbers of a minimum number of which is the result.
2. The number of the ring there is a negative number. Then there are two ways to select numbers:
1. The selected ID number contains two consecutive numbers 1 and n. At this time, the line segment tree, they are not consecutive (between 1 and is turned off in the n). All figures and subtract numbers within the range of minimum segment tree is the sum of the results.
2. The figures do not contain the selected continuous two numbers 1 and n. Maintain the maximum continuous interval of digital and can use tree line.
7 segment tree need to maintain data: sum, lmax, rmax, tmax, lmin, rmin, tmin.
Where lmax is the maximum consecutive number of the intended starting from the left end of the first section and the number, rmax is the maximum consecutive number of the intended starting from the right end of the first section and the number, tmax means the maximum number of consecutive entire interval and. Similarly min.
As condition determination, when the ring is a positive whole number of hours, for the entire section has a sum = tmax.
***************************************************************************************************************************
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 #define ls rt<<1 7 #define rs rt<<1|1 8 #define lson l,m,ls 9 #define rson m+1,r,rs 10 #define mid (l+r)>>1 11 #define MAXN 100010 12 using namespace std; 13 int N,M; 14 int a[MAXN]; 15 struct node 16 { 17 int l,r; 18 int lmax,rmax,summax; 19 int lmin,rmin,summin; 20 int sum; 21 }tree[MAXN<<2]; 22 void pushup(int rt) 23 { 24 tree[rt].sum=tree[ls].sum+tree[rs].sum; 25 26 tree[rt].lmax=max(tree[ls].lmax,tree[ls].sum+tree[rs].lmax); 27 tree[rt].rmax=max(tree[rs].rmax,tree[rs].sum+tree[ls].rmax); 28 tree[rt].summax=max(max(tree[ls].summax,tree[rs].summax),tree[ls].rmax+tree[rs].lmax); 29 30 tree[rt].lmin=min(tree[ls].lmin,tree[ls].sum+tree[rs].lmin); 31 tree[rt].rmin=min(tree[rs].rmin,tree[rs].sum+tree[ls].rmin); 32 tree[rt].summin=min(min(tree[ls].summin,tree[rs].summin),tree[ls].rmin+tree[rs].lmin); 33 34 } 35 void build(int l,int r,int rt) 36 { 37 if(l>r)return; 38 tree[rt].l=l; 39 tree[rt].r=r; 40 if(r==l) 41 { 42 tree[rt].sum=a[l]; 43 tree[rt].lmax=tree[rt].lmin=tree[rt].summax=a[l]; 44 tree[rt].rmin=tree[rt].rmax=tree[rt].summin=a[l]; 45 return; 46 } 47 int m=(l+r)>>1; 48 build(lson); 49 build(rson); 50 pushup(rt); 51 } 52 53 void update(int pos,int val,int rt) 54 { 55 if(tree[rt].l==tree[rt].r&&tree[rt].l==pos) 56 { 57 tree[rt].sum=val; 58 tree[rt].lmax=tree[rt].lmin=tree[rt].rmin=tree[rt].rmax=val; 59 tree[rt].summax=tree[rt].summin=val; 60 return; 61 } 62 int m=(tree[rt].l+tree[rt].r)>>1; 63 if(m<pos) 64 update(pos,val,rs); 65 else 66 update(pos,val,ls); 67 pushup(rt); 68 } 69 70 int main() 71 { 72 while(scanf("%d",&N)!=EOF) 73 { 74 for(int i=1;i<=N;i++) 75 scanf("%d",&a[i]); 76 build(1,N,1); 77 78 scanf("%d",&M); 79 int pos,val; 80 81 while(M--) 82 { 83 scanf("%d%d",&pos,&val); 84 update(pos,val,1); 85 86 if(tree[1].sum==tree[1].summax) 87 printf("%d\n",tree[1].sum-tree[1].summin); 88 else 89 printf("%d\n",max(tree[1].summax,tree[1].sum-tree[1].summin)); 90 } 91 } 92 return 0; 93 }
Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3528185.html