标签:
For each query with p = 2, print the maximum sum of the aesthetics values in one shot in interval [x, y].
1 5 7 3 -1 2 -4 6 1 2 1 5 2 1 3 1 2 1 2 1 5 2 1 4 0 2 4 2 1 5
4 -3 3 1 6
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 200010; 6 struct node { 7 int lt,rt,maxv,lazy; 8 } tree[maxn<<2]; 9 int d[maxn],a[maxn],n,m,k; 10 void pushup(int v) { 11 tree[v].maxv = max(tree[v<<1].maxv + tree[v<<1].lazy,tree[v<<1|1].maxv+tree[v<<1|1].lazy); 12 } 13 void pushdown(int v) { 14 if(tree[v].lazy) { 15 tree[v<<1].lazy += tree[v].lazy; 16 tree[v<<1|1].lazy += tree[v].lazy; 17 tree[v].lazy = 0; 18 } 19 } 20 void build(int lt,int rt,int v) { 21 tree[v].lt = lt; 22 tree[v].rt = rt; 23 tree[v].lazy = 0; 24 if(lt == rt) { 25 tree[v].maxv = d[tree[v].lt + k - 1] - d[tree[v].lt-1]; 26 return; 27 } 28 int mid = (lt + rt)>>1; 29 build(lt,mid,v<<1); 30 build(mid+1,rt,v<<1|1); 31 pushup(v); 32 } 33 void update(int lt,int rt,int val,int v){ 34 if(lt <= tree[v].lt && rt >= tree[v].rt){ 35 tree[v].lazy += val; 36 return; 37 } 38 pushdown(v); 39 if(lt <= tree[v<<1].rt) update(lt,rt,val,v<<1); 40 if(rt >= tree[v<<1|1].lt) update(lt,rt,val,v<<1|1); 41 pushup(v); 42 } 43 int query(int lt,int rt,int v){ 44 if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].maxv + tree[v].lazy; 45 pushdown(v); 46 int ans = -0x3f3f3f3f; 47 if(lt <= tree[v<<1].rt) ans = max(ans,query(lt,rt,v<<1)); 48 if(rt >= tree[v<<1|1].lt) ans = max(ans,query(lt,rt,v<<1|1)); 49 pushup(v); 50 return ans; 51 } 52 int main() { 53 int T,p,x,y; 54 scanf("%d",&T); 55 while(T--) { 56 scanf("%d %d %d",&n,&m,&k); 57 for(int i = 1; i <= n; ++i) { 58 scanf("%d",d+i); 59 a[i] = d[i]; 60 d[i] += d[i-1]; 61 } 62 build(1,n-k+1,1); 63 while(m--){ 64 scanf("%d %d %d",&p,&x,&y); 65 if(!p){ 66 update(max(1,x - k + 1),min(n-k+1,x),y - a[x],1); 67 a[x] = y; 68 }else if(p == 1){ 69 update(max(1,x - k + 1),min(n-k+1,x),a[y] - a[x],1); 70 update(max(1,y - k + 1),min(n-k+1,y),a[x] - a[y],1); 71 swap(a[x],a[y]); 72 }else if(p == 2) printf("%d\n",query(x,y-k+1,1)); 73 } 74 } 75 return 0; 76 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4448485.html