Bzoj4384[poi2015]trzy wie?e
Test instructions
Given a string with a length of N of only ' B ', ' C ', and ' S ' three characters, find the longest contiguous substring, making it either a single character or multiple characters, but not as many occurrences of any two characters.
Exercises
Disgusting tree-like array problem. First find the longest string with only one character. The prefix is then preprocessed and. Topic Requirements Meet
CNT[1][I]-CNT[1][J]!=CNT[2][I]-CNT[2][J] cnt[2][i]-cnt[2][j]!=cnt[3][i]-cnt[3][j] cnt[1][i]-cnt[1][j]!=cnt[3][i] -CNT[3][J]
Simplification get cnt[1][i]-cnt[2][i]!=cnt[1][j]-cnt[2][j] cnt[2][i]-cnt[3][i]!=cnt[2][j]-cnt[3][j] cnt[1][i]-cnt[3][i]!=cnt[1 ][J]-CNT[3][J]
Cnt[1][i]-cnt[2][i],cnt[2][i]-cnt[3][i],cnt[1][i]-cnt[3][i] respectively as Xi,yi,zi, the topic is converted to each I, ask for a J to make their xyz different and I and J are the biggest difference. First, the key is sorted by x, followed by Y (discretization minus the negative, using the chain list method, if the sorting method will be T) for the subscript build array, the tree array maintains 6 values: the maximum minimum value of the position previously processed, the maximum minimum value corresponding to the z, and the maximum minimum value of z different maximum minimum value. Why maintain the minimum value? Because the order of the positions will be different after the x sort, there may be situations where the position depends on the position before being processed. The specific method of making x different is that for x the same set of positions, first, they are queried to update the answer separately, and then the answer is updated to insert the tree-like array.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 10000105 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineLB (x) x&-x7 #defineINF 0X3FFFFFFF8 using namespacestd;9 Ten structnd{intid,cnt[3];}; nd NDS[MAXN];structe{intV,n;};intess,g[maxn<<1]; e ES[MAXN]; One voidPeintAintb) {es[++ess]= (e) {a,g[b]}; g[b]=ess;} A intAns,n,tot;CharS[MAXN]; - structbit{intmn1,mnz,mn2,mx1,mxz,mx2;}; Bit BITS1[MAXN],BITS2[MAXN]; - BOOLCMP1 (nd a,nd b) {returna.cnt[0]<b.cnt[0];} the voidQuery1 (nd a) { - intb=a.cnt[1]-1; - while(b) { - if(bits1[b].mnz==a.cnt[2]){ + if(Bits1[b].mn2!=inf) Ans=max (ans,a.id-bits1[b].mn2); -}Else if(Bits1[b].mn1!=inf) Ans=max (ans,a.id-bits1[b].mn1); + if(bits1[b].mxz==a.cnt[2]){ A if(bits1[b].mx2!=0) Ans=max (ans,bits1[b].mx2-a.id); at}Else if(bits1[b].mx1!=0) Ans=max (ans,bits1[b].mx1-a.id); -b-=lb (b); - } - } - voidQuery2 (nd a) { - intb=a.cnt[1]+1; in while(b<=tot) { - if(bits2[b].mnz==a.cnt[2]){ to if(Bits1[b].mn2!=inf) Ans=max (ans,a.id-bits2[b].mn2); +}Else if(Bits2[b].mn1!=inf) Ans=max (ans,a.id-bits2[b].mn1); - if(bits2[b].mxz==a.cnt[2]){ the if(bits1[b].mx2!=0) Ans=max (ans,bits2[b].mx2-a.id); *}Else if(bits2[b].mx1!=0) Ans=max (ans,bits2[b].mx1-a.id); $b+=lb (b);Panax Notoginseng } - } the voidupdate1 (nd a) { + intb=a.cnt[1]; A while(b<=tot) { the if(a.id<bits1[b].mn1) { + if(a.cnt[2]!=bits1[b].mnz) -bits1[b].mnz=a.cnt[2],bits1[b].mn2=bits1[b].mn1,bits1[b].mn1=a.id; $ Elsebits1[b].mn1=a.id; $}Else if(a.cnt[2]!=BITS1[B].MNZ&&A.ID<BITS1[B].MN2) bits1[b].mn2=a.id; - if(a.id>bits1[b].mx1) { - if(a.cnt[2]!=bits1[b].mxz) thebits1[b].mxz=a.cnt[2],bits1[b].mx2=bits1[b].mx1,bits1[b].mx1=a.id; - Elsebits1[b].mx1=a.id;Wuyi}Else if(a.cnt[2]!=BITS1[B].MXZ&&A.ID>BITS1[B].MX2) bits1[b].mx2=a.id; theb+=lb (b); - } Wu } - voidupdate2 (nd a) { About intb=a.cnt[1]; $ while(b) { - if(a.id<bits2[b].mn1) { - if(a.cnt[2]!=bits2[b].mnz) -bits2[b].mnz=a.cnt[2],bits2[b].mn2=bits2[b].mn1,bits2[b].mn1=a.id; A Elsebits2[b].mn1=a.id; +}Else if(a.cnt[2]!=BITS2[B].MNZ&&A.ID<BITS2[B].MN2) bits2[b].mn2=a.id; the if(a.id>bits2[b].mx1) { - if(a.cnt[2]!=bits2[b].mxz) $bits2[b].mxz=a.cnt[2],bits2[b].mx2=bits2[b].mx1,bits2[b].mx1=a.id; the Elsebits2[b].mx1=a.id; the}Else if(a.cnt[2]!=BITS2[B].MXZ&&A.ID>BITS2[B].MX2) bits2[b].mx2=a.id; theb-=lb (b); the } - } in intMain () { thescanf"%d", &n); scanf"%s", s+1);intcnt[3]={0,0,0}; the intL=1, r=1; while(r<=n) { while(R<=n&&s[l]==s[r]) Ans=max (ans,r-l+1), r++; l++;} Aboutnds[0]= (ND) {0,{0,0,0}}; theInc (I,1, N) { the if(s[i]=='B') cnt[0]++;if(s[i]=='C') cnt[1]++;if(s[i]=='S') cnt[2]++; thends[i]= (nd) {i,{cnt[0]-cnt[1],cnt[0]-cnt[2],cnt[1]-cnt[2]}}; + } -ess=0; Inc (I,0, n) PE (i,nds[i].cnt[1]+n+1); tot=0; theInc (I,0N2+1)if(G[i]) {Bayitot++;intX=g[i]; while(x) nds[es[x].v].cnt[1]=tot,x=ES[X].N; the } theInc (I,1, tot) bits1[i]=bits2[i]= (bit) {INF,0Inf0,0,0}; Sort (nds,nds+n+1, CMP1);intI=0; - while(i<=N) { - intJ=i; while(j<=n&&nds[j].cnt[0]==nds[i].cnt[0]) J + +; the if(i) Inc (k,i,j-1) {Query1 (nds[k]); Query2 (Nds[k]);} theInc (k,i,j-1) {update1 (nds[k]); Update2 (Nds[k]);} theI=J; the } -printf"%d", ans);return 0; the}
20160627
Bzoj4384[poi2015]trzy wie?e