标签:
4 1 0 1 0 5 0 1 4 1 2 3 0 1 4 1 3 3 0 4 4
1 2 0
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100010; 4 struct node { 5 int lsum[2],rsum[2],mx[2]; 6 bool lazy; 7 } tree[maxn<<2]; 8 void pushup(int v,int k) { 9 for(int i = 0; i < 2; ++i) { 10 tree[v].lsum[i] = tree[v<<1].lsum[i]; 11 tree[v].rsum[i] = tree[v<<1|1].rsum[i]; 12 if(tree[v].lsum[i] == k - (k>>1)) 13 tree[v].lsum[i] += tree[v<<1|1].lsum[i]; 14 if(tree[v].rsum[i] == (k>>1)) 15 tree[v].rsum[i] += tree[v<<1].rsum[i]; 16 tree[v].mx[i] = max(tree[v<<1].mx[i],tree[v<<1|1].mx[i]); 17 tree[v].mx[i] = max(tree[v].mx[i],tree[v<<1].rsum[i]+tree[v<<1|1].lsum[i]); 18 } 19 } 20 21 void change(int v) { 22 swap(tree[v].lsum[0],tree[v].lsum[1]); 23 swap(tree[v].rsum[0],tree[v].rsum[1]); 24 swap(tree[v].mx[0],tree[v].mx[1]); 25 tree[v].lazy = !tree[v].lazy; 26 } 27 void pushdown(int v) { 28 if(tree[v].lazy) { 29 change(v<<1); 30 change(v<<1|1); 31 tree[v].lazy = false; 32 } 33 } 34 void build(int L,int R,int v) { 35 tree[v].lazy = false; 36 if(L == R) { 37 int tmp; 38 scanf("%d",&tmp); 39 tree[v].mx[0] = tree[v].lsum[0] = tree[v].rsum[0] = !tmp; 40 tree[v].mx[1] = tree[v].lsum[1] = tree[v].rsum[1] = tmp; 41 return; 42 } 43 int mid = (L + R)>>1; 44 build(L,mid,v<<1); 45 build(mid+1,R,v<<1|1); 46 pushup(v,R - L + 1); 47 } 48 void update(int L,int R,int lt,int rt,int v) { 49 if(lt <= L && rt >= R) { 50 change(v); 51 return; 52 } 53 pushdown(v); 54 int mid = (L + R)>>1; 55 if(lt <= mid) update(L,mid,lt,rt,v<<1); 56 if(rt > mid) update(mid+1,R,lt,rt,v<<1|1); 57 pushup(v,R - L + 1); 58 } 59 int query(int L,int R,int lt,int rt,int v) { 60 if(lt <= L && rt >= R) return tree[v].mx[1]; 61 pushdown(v); 62 int mid = (L + R)>>1,ret = 0; 63 if(lt <= mid) ret = max(ret,query(L,mid,lt,rt,v<<1)); 64 if(rt > mid) ret = max(ret,query(mid+1,R,lt,rt,v<<1|1)); 65 if(lt <= mid && rt > mid) 66 ret = max(ret,min(mid - lt + 1,tree[v<<1].rsum[1]) + min(rt - mid,tree[v<<1|1].lsum[1])); 67 pushup(v,R - L + 1); 68 return ret; 69 } 70 int main() { 71 int n,m,op,a,b; 72 while(~scanf("%d",&n)){ 73 build(1,n,1); 74 scanf("%d",&m); 75 while(m--){ 76 scanf("%d %d %d",&op,&a,&b); 77 if(op) update(1,n,a,b,1); 78 else printf("%d\n",query(1,n,a,b,1)); 79 } 80 } 81 return 0; 82 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4468942.html