好久没写过这么长的代码了= =
像狗一样看了一个上午终于在下午的时候狠下心来写对拍
对拍大法好qwq
一份暴力一份正解
以后有空再写线段树
暴力
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 |
#include <cstdio> #include <algorithm> #define p(x) ('0'<=x&&x<='9') char cc; template <class T> void read(T &x) { cc=getchar(); x=0; while(!p(cc)) cc=getchar(); while(p(cc)) x=x*10+cc-48,cc=getchar(); } using namespace std; #define N 500 int a[N]; int count(int l,int r) { int cnt=0; for(int i=l;i<=r;i++) if(a[i]) cnt++; return cnt; } int solve(int l,int r) { int node=0,cur[2],col=-1; cur[0]=cur[1]=0; for(int i=l;i<=r;i++) { if(col!=a[i]) { if(~col) cur[col]=max(cur[col],node); col=a[i]; node=0; } node++; } cur[col]=max(cur[col],node); return cur[1]; } void fill(int l,int r,int col) { for(int i=l;i<=r;i++) a[i]=col; } void rev(int l,int r) { for(int i=l;i<=r;i++) a[i]^=1; } int n,m; int main() { freopen("BZOJ1858.in","r",stdin); freopen("BZOJ1858BF.out","w",stdout); read(n); read(m); for(int i=1;i<=n;i++) read(a[i]); for(int i=1;i<=m;i++) { int _,b,c; read(_); read(b); read(c); if(_==0) fill(b+1,c+1,0); if(_==1) fill(b+1,c+1,1); if(_==2) rev(b+1,c+1); if(_==3) printf("%d",count(b+1,c+1)); if(_==4) printf("%d",solve(b+1,c+1)); if(m>1&&_>=3) puts(""); printf("%d : ",i); for(int i=1;i<=n;i++) printf("%d ",a[i]); puts(""); } return 0; } |
正解
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
#include <cstdio> #include <iostream> #include <cmath> #include <bitset> #define p(x) ('0'<=x&&x<='9') char cc; template <class T> void read(T &x) { cc=getchar(); x=0; while(!p(cc)) cc=getchar(); while(p(cc)) x=x*10+cc-48,cc=getchar(); } template <class T> void comp(T &x,T y) { y>x?x=y:x=x; } using namespace std; #define N 100010 #define SQR 400 int n,m,SIZ,CNT; bitset <SQR> B[SQR],U; int L[SQR],R[SQR],Len_L[2][SQR],Len_R[2][SQR],MXL[2][SQR]; int belong[N],a; int BFcount(int x,int l,int r) { int ret=0; for(int i=l;i<=r;i++) if(B[x][i]==1) ret++; return ret; } int count(int l,int r) { if(belong[l]==belong[r]) return BFcount(belong[l],(l-1)%SIZ+1,(r-1)%SIZ+1); int ret=BFcount(belong[l],(l-1)%SIZ+1,SIZ)+BFcount(belong[r],1,(r-1)%SIZ+1); for(int i=belong[l]+1;i<belong[r];i++) ret+=B[i].count(); return ret; } int cur[2]; void calc(int x,int l,int r) //Checked { int col=-1,node=0; cur[0]=cur[1]=0; for(int i=l;i<=r;i++) { if(col!=B[x][i]) { if(~col) comp(cur[col],node); node=0; col=B[x][i]; } node++; } comp(cur[B[x][r]],node); } int solve(int l,int r) { int ret=0,ext=0; if(belong[l]==belong[r]) //Checked { calc(belong[l],(l-1)%SIZ+1,(r-1)%SIZ+1); return cur[1]; } //Checked calc(belong[l],(l-1)%SIZ+1,SIZ); ret=cur[1]; ext=min(Len_R[1][belong[l]],R[belong[l]]-l+1); //Checked for(int i=belong[l]+1;i<belong[r];i++) { if(MXL[1][i]==SIZ) { ext+=MXL[1][i]; continue; } ext+=Len_L[1][i]; comp(ret,ext); comp(ret,MXL[1][i]); ext=Len_R[1][i]; } //Checked calc(belong[r],1,(r-1)%SIZ+1); ext+=min(Len_L[1][belong[r]],r-L[belong[r]]+1); comp(ret,ext); comp(ret,cur[1]); return ret; } void build(int x) //Checked { int len=R[x]-L[x]+1; Len_L[1][x]=Len_L[0][x]=Len_R[0][x]=Len_R[1][x]=MXL[0][x]=MXL[1][x]=0; int node; for(node=2;B[x][node]==B[x][node-1]&&node<=len;node++); Len_L[B[x][1]][x]=node-1; for(node=len-1;B[x][node]==B[x][node+1]&&node;node--); Len_R[B[x][len]][x]=(len-node); int col=-1; node=0; for(int i=1;i<=len;i++) { if(col!=B[x][i]) { if(~col) comp(MXL[col][x],node); node=0; col=B[x][i]; } node++; } comp(MXL[col][x],node); } void fill(int l,int r,int col) // Checked { if(belong[l]==belong[r]) { for(int i=(l-1)%SIZ+1;i<=(r-1)%SIZ+1;i++) B[belong[l]][i]=col; build(belong[l]); return; } for(int i=(l-1)%SIZ+1;i<=SIZ;i++) B[belong[l]][i]=col; build(belong[l]); for(int i=1;i<=(r-1)%SIZ+1;i++) B[belong[r]][i]=col; build(belong[r]); for(int i=belong[l]+1;i<belong[r];i++) { if(col==1) { B[i]=U; MXL[0][i]=Len_L[0][i]=Len_R[0][i]=0; MXL[1][i]=Len_L[1][i]=Len_R[1][i]=SIZ; } else { B[i].reset(); MXL[1][i]=Len_L[1][i]=Len_R[1][i]=0; MXL[0][i]=Len_L[0][i]=Len_R[0][i]=SIZ; } } } void rev(int l,int r) //Checked { if(belong[l]==belong[r]) { for(int i=(l-1)%SIZ+1;i<=(r-1)%SIZ+1;i++) B[belong[l]].flip(i); build(belong[l]); return; } for(int i=(l-1)%SIZ+1;i<=SIZ;i++) B[belong[l]].flip(i); build(belong[l]); for(int i=1;i<=(r-1)%SIZ+1;i++) B[belong[r]].flip(i); build(belong[r]); for(int i=belong[l]+1;i<belong[r];i++) { B[i]^=U; swap(Len_L[0][i],Len_L[1][i]); swap(Len_R[0][i],Len_R[1][i]); swap(MXL[0][i],MXL[1][i]); } } void init() { read(n); read(m); SIZ=floor(sqrt(n)); for(int i=1;i<=SIZ;i++) U[i]=1; CNT=(n-1)/SIZ +1; for(int i=1;i<=n;i++) belong[i]=(i-1)/SIZ +1; for(int i=1;i<=n;i++) { read(a); if(a)B[belong[i]][(i-1)%SIZ +1]=1; } for(int i=1;i<=CNT;i++) { L[i]=(i-1)*SIZ+1; R[i]=i*SIZ; } R[CNT]=n; for(int i=1;i<=CNT;i++) build(i); } int main() { // freopen("BZOJ1858.in","r",stdin); // freopen("BZOJ1858.out","w",stdout); int a,b,c; init(); for(int i=1;i<=m;i++) { read(a); read(b); read(c); if(a==0) fill(b+1,c+1,0); if(a==1) fill(b+1,c+1,1); if(a==2) rev(b+1,c+1); if(a==3) printf("%d\n",count(b+1,c+1)); if(a==4) printf("%d\n",solve(b+1,c+1)); // printf("%d : ",i); // for(int i=1;i<=n;i++) cout << B[belong[i]][(i-1)%SIZ+1] << " "; // puts(""); } return 0; } |