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
| #include <bits/stdc++.h> using namespace std; const int N=200010; int n,m; struct node{int lm,rm,tm,num;}; node operator+(node l,node r){ node p; p.num=l.num+r.num; p.lm=min(l.lm,l.num+r.lm); p.rm=min(r.rm,r.num+l.rm); p.tm=min({l.tm,r.tm,l.rm+r.lm}); return p; } struct SEG{ node a[N<<2]; void build(int p,int l,int r,int *b){ if(l==r){ int t=min(0,b[l]); a[p]={t,t,t,b[l]}; return ; } int mid=(l+r)>>1; build(p<<1,l,mid,b); build(p<<1|1,mid+1,r,b); a[p]=a[p<<1]+a[p<<1|1]; } void insert(int p,int l,int r,int x,int y){ if(!x)return ; if(l==r){ int t=min(0,y); a[p]={t,t,t,y}; return ; } int mid=(l+r)>>1; if(mid>=x)insert(p<<1,l,mid,x,y); else insert(p<<1|1,mid+1,r,x,y); a[p]=a[p<<1]+a[p<<1|1]; } node query(int p,int l,int r,int ql,int qr){ if(ql>qr)return {0,0,0,0}; if(ql<=l&&r<=qr)return a[p]; int mid=(l+r)>>1; if(mid>=ql&&mid<qr)return query(p<<1,l,mid,ql,qr)+query(p<<1|1,mid+1,r,ql,qr); if(mid>=ql)return query(p<<1,l,mid,ql,qr); return query(p<<1|1,mid+1,r,ql,qr); } }P0,P1,Q0,Q1; struct BIT{ int c[N]; void clear(){for(int i=1;i<=n;i++)c[i]=0;} void add(int x,int y){for(;x<=n;x+=(x&-x))c[x]+=y;} int query(int x,int cnt=0){for(;x>=1;x-=(x&-x))cnt+=c[x];return cnt;} }B0,B1; int a[N],tot0,tot1,id0[N],id1[N],a0[N],a1[N],b0[N],b1[N]; int p0[N],q0[N],p1[N],q1[N]; void init(){ B0.clear(),B1.clear(); for(int i=1;i<=n;i++)a0[i]=a1[i]=b0[i]=b1[i]=0; for(int i=1;i<=n;i++){ if(a[i])B1.add(i,1); else B0.add(i,1); } tot0=tot1=0; for(int i=n;i>=1;i-=2)tot0++; for(int i=n+1;i>=1;i-=2)tot1++; int t1=tot1,t0=tot0; for(int i=n;i>=1;i-=2)id0[i]=id0[i-1]=t0,t0--; for(int i=n+1;i>=1;i-=2)id1[i]=id1[i-1]=t1,t1--; for(int i=1;i<=n;i++){ if(a[i])b0[id0[i]]++,b1[id1[i]]++; else a0[id0[i]]++,a1[id1[i]]++; } for(int i=1;i<=tot0;i++)p0[i]=a0[i]-1,q0[i]=3*b0[i]-2; for(int i=1;i<=tot1;i++)p1[i]=a1[i]-1,q1[i]=3*b1[i]-2; P0.build(1,1,tot0,p0); P1.build(1,1,tot1,p1); Q0.build(1,1,tot0,q0); Q1.build(1,1,tot1,q1); } void update(int x){ if(a[x]==0){ B0.add(x,-1); B1.add(x,1); a0[id0[x]]--,a1[id1[x]]--; b0[id0[x]]++,b1[id1[x]]++; } else{ B0.add(x,1); B1.add(x,-1); a0[id0[x]]++,a1[id1[x]]++; b0[id0[x]]--,b1[id1[x]]--; } a[x]^=1; p0[id0[x]]=a0[id0[x]]-1; p1[id1[x]]=a1[id1[x]]-1; q0[id0[x]]=3*b0[id0[x]]-2; q1[id1[x]]=3*b1[id1[x]]-2; P0.insert(1,1,tot0,id0[x],p0[id0[x]]); P1.insert(1,1,tot1,id1[x],p1[id1[x]]); Q0.insert(1,1,tot0,id0[x],q0[id0[x]]); Q1.insert(1,1,tot1,id1[x],q1[id1[x]]); } bool check0(int k){ if(k==n)return 0; int id=id0[k+1]; int t1=P0.query(1,1,tot0,id,tot0).tm; int t2=Q0.query(1,1,tot0,id,tot0).tm; int t3=P0.query(1,1,tot0,id,tot0).lm; int t4=Q0.query(1,1,tot0,id,tot0).lm; int X=B0.query(k),Y=B1.query(k); return X+Y+t1<0||3*X+3*Y+2+t2<0||X+t3<0||3*Y+2+t4<0; } bool check1(int k){ int id=id1[k+1]; int t1=P1.query(1,1,tot1,id,tot1).tm; int t2=Q1.query(1,1,tot1,id,tot1).tm; int t3=P1.query(1,1,tot1,id,tot1).lm; int t4=Q1.query(1,1,tot1,id,tot1).lm; int X=B0.query(k),Y=B1.query(k); return X+Y+t1<0||3*X+3*Y+2+t2<0||X+t3<0||3*Y+2+t4<0; } int query(){ int l=1,r=n; while(l<r){ int mid=(l+r)>>1; if((n-mid)&1){if(!check1(mid))r=mid;else l=mid+1;} else {if(!check0(mid))r=mid;else l=mid+1;} } return l; } void WZH(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%1d",a+i); init(); printf("%d ",query()); while(m--){ int x; scanf("%d",&x); update(x); printf("%d ",query()); } puts(""); } int main(){ int T,cid; scanf("%d%d",&cid,&T); while(T--)WZH(); return 0; }
|