1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| #include<bits/stdc++.h> using namespace std;
const int N = 4e6+6; int a[N]; struct Node{ int o,l,r; int cntL,cntR,ans; };
Node t[N*4][2];bool tag[N*4];
void inv(int o){ swap(t[o][0],t[o][1]); tag[o] = !tag[o]; } void pushdown(int o){ if(tag[o]){ int lch=o<<1,rch=o<<1|1; inv(lch);inv(rch); tag[o]=0; } }
Node stk[10000];int tp; void find_nodes(int o,int ql,int qr,int op){ if(ql<=t[o][op].l&&t[o][op].r<=qr){stk[++tp]=t[o][op];return;} int lch=o<<1,rch=o<<1|1; int mid=(t[o][op].l+t[o][op].r)>>1; pushdown(o); if(ql<=mid)find_nodes(lch,ql,qr,op); if(qr> mid)find_nodes(rch,ql,qr,op); }
int getkth_L(int o,int ql,int qr,int op,int k){ tp=0; find_nodes(o,ql,qr,op); for(int i=1;i<=tp;i++){ if(stk[i].cntL >= k){ int l = stk[i].l, r = stk[i].r; o = stk[i].o; while(l<r){ pushdown(o); int mid=(l+r)>>1; int lch=o<<1,rch=o<<1|1; if(t[lch][op].cntL >= k)r=mid,o=lch; else k=k-t[lch][op].cntL+t[lch][op].cntR,l=mid+1,o=rch; } return l; } k = k - stk[i].cntL + stk[i].cntR; } assert(0); return -1; }
int getkth_R(int o,int ql,int qr,int op,int k){ tp=0; find_nodes(o,ql,qr,op); for(int i=tp;i>=1;i--){ if(stk[i].cntR >= k){ int l = stk[i].l, r = stk[i].r; o = stk[i].o; while(l<r){ pushdown(o); int mid=(l+r)>>1; int lch=o<<1,rch=o<<1|1; if(t[rch][op].cntR >= k)l=mid+1,o=rch; else k=k-t[rch][op].cntR+t[rch][op].cntL,r=mid,o=lch; } return l; } k = k - stk[i].cntR + stk[i].cntL; } assert(0); return -1; }
Node merge(const Node& p,const Node& q,int op){ int elim = min(p.cntR, q.cntL); Node ret{min(p.o,q.o)>>1,p.l,q.r,p.cntL+q.cntL-elim,p.cntR+q.cntR-elim,max(p.ans,q.ans)}; int newans; if(p.cntR == q.cntL){ newans = (q.cntR ? getkth_R(q.o,q.l,q.r,op,q.cntR)-1 : q.r) - (p.cntL ? getkth_L(p.o,p.l,p.r,op,p.cntL)+1 : p.l) + 1; } else if(p.cntR >= q.cntL){ newans = (q.cntR ? getkth_R(q.o,q.l,q.r,op,q.cntR)-1 : q.r) - (getkth_R(p.o,p.l,p.r,op,q.cntL+1)+1) + 1; } else{ newans = (getkth_L(q.o,q.l,q.r,op,p.cntR+1)-1) - (p.cntL ? getkth_L(p.o,p.l,p.r,op,p.cntL)+1 : p.l) + 1; } ret.ans = max(ret.ans, newans); return ret; }
void build(int o,int l,int r){ if(l==r){ t[o][0] = {o,l,r, a[l]==1,a[l]==0,0}; t[o][1] = {o,l,r, a[l]==0,a[l]==1,0}; return; } int lch=o<<1,rch=o<<1|1; int mid=(l+r)>>1; build(lch,l,mid);build(rch,mid+1,r); t[o][0] = merge(t[lch][0],t[rch][0],0); t[o][1] = merge(t[lch][1],t[rch][1],1); } void modify(int o,int l,int r,int ql,int qr){ if(ql <=l && r<=qr){ inv(o);return; } pushdown(o); int lch=o<<1,rch=o<<1|1; int mid=(l+r)>>1; if(ql<=mid)modify(lch,l,mid,ql,qr); if(qr>mid) modify(rch,mid+1,r,ql,qr); t[o][0] = merge(t[lch][0],t[rch][0],0); t[o][1] = merge(t[lch][1],t[rch][1],1); }
Node query(int o,int l,int r,int ql,int qr){ if(ql<=l && r<=qr)return t[o][0]; pushdown(o); int lch=o<<1,rch=o<<1|1; int mid=(l+r)>>1; if(qr<=mid)return query(lch,l,mid,ql,qr); if(ql>mid)return query(rch,mid+1,r,ql,qr); return merge(query(lch,l,mid,ql,qr),query(rch,mid+1,r,ql,qr),0); }
int main(){ ios::sync_with_stdio(0);cin.tie(0); int n,m;cin>>n>>m; for(int i=1;i<=n;i++)cin>>a[i];
build(1,1,n); int lastans = 0; while(m--){ int op,l,r;cin>>op>>l>>r; l=(l+lastans)%n+1,r=(r+lastans)%n+1; if(l>r)swap(l,r); if(op==1){ lastans = query(1,1,n,l,r).ans; cout << lastans << '\n'; } else modify(1,1,n,l,r); } return 0; }
|