First, put the question on the suffix array and consider
Known two array a B, for min (A[i],..., a[j]) + (B[i]^b[j]) maximum value
Routine questions
Initial each point is a small connected block
Add a in order from large to small to calculate the contribution of the current join side as min:
Each join will connect two connected blocks, the answer is that the two sides connected to a number of each of the two can get the difference or maximum value
Me: Is this not a linear base?
MIAOM:MDZZ, there can only be two numbers.
Me: clam, that's so hard.
Miaom:trie.
Me: Oh
No more.
1#include <bits/stdc++.h>2 #defineN 5000013 #defineMAX 184 using namespacestd;5 intNode,n;Charch;6 inta[n],b[n],c[n*max][2],size[n],rt[n],lef[n];7pair<int,int>So[n];8 intX[n],y[n],sa[n],rk[n],ht[n],v[n];9 voidGetsa (intA[],intNintm= the)Ten { One int*x=x,*y=y,i,p,j; Amemset (V,0,sizeofv); - for(i=1; i<=n;i++) v[x[i]=a[i]]++; - for(i=1; i<=m;i++) v[i]+=v[i-1]; the for(i=n;i>=1; i--) sa[v[x[i]]--]=i; - for(i=1; i<=n;i<<=1, m=p) - { -p=0; +memset (V,0,sizeofv); - for(j=n-i+1; j<=n;j++) y[++p]=J; + for(j=1; j<=n;j++)if(sa[j]>i) y[++p]=sa[j]-i; A for(j=1; j<=n;j++) v[x[y[j]]]++; at for(j=1; j<=m;j++) v[j]+=v[j-1]; - for(j=n;j>=1; j--) sa[v[x[y[j]]]--]=Y[j]; - swap (x, y); -p=1; x[sa[1]]=1; - for(j=2; j<=n;j++) - if(y[sa[j-1]]==y[sa[j]]&&y[sa[j-1]+i]==y[sa[j]+i]) x[sa[j]]=p; in Elsex[sa[j]]=++p; - if(p>=n) Break; to } + } - voidGETHT (intA[],intN) the { * intk=0; $ for(intI=1; i<=n;i++) rk[sa[i]]=i;Panax Notoginseng for(intI=1; i<=n;i++) - { the if(k) k--; + intj=sa[rk[i]-1]; A while(I+k<=n&&j+k<=n&&a[i+k]==a[j+k]) k++; theht[rk[i]]=K; + } - } $ intBuildintx) $ { - intrt=++node,now=RT; - for(inti=max;i>=0; i--) thec[now][(x>>i) &1]=++node,now=NODE; - returnRT;Wuyi } the intQueintXintYintz=0) - { Wu intbest=Z; - if(c[x][0]) About if(c[y][1]) Best=que (c[x][0],c[y][1],z*2+1); $ ElseBest=que (c[x][0],c[y][0],z*2); - if(c[x][1]) - if(c[y][0]) Best=max (Best,que (c[x][1],c[y][0],z*2+1)); - ElseBest=max (Best,que (c[x][1],c[y][1],z*2)); A returnBest ; + } the voidMergeintXinty) - { $ if(c[x][0]) the if(c[y][0]) Merge (c[x][0],c[y][0]); the Elsec[y][0]=c[x][0]; the if(c[x][1]) the if(c[y][1]) Merge (c[x][1],c[y][1]); - Elsec[y][1]=c[x][1]; in } the intGETFA (intx) the { About if(lef[x]==x)returnx; the Else returnlef[x]=GETFA (lef[x]); the } the intMain () + { -scanf"%d",&n); the for(Ch=getchar ();! Isalpha (CH); ch=GetChar ());Bayi for(intI=1; i<=n;i++,ch=GetChar ()) thea[i]=ch; the for(intI=1; i<=n;i++) -scanf"%d",&b[i]); - Getsa (a,n); the getht (a,n); the for(intI=1; i<=n;i++) thesize[i]=1, lef[i]=i,rt[i]=build (B[sa[i]]); the for(intI=2; i<=n;i++) -so[i-1]=make_pair (nht[i],i); theSort (so+1, so+n); the intret=0; the for(intI=1; i<n;i++)94 { the intx=so[i].second,y=x-1, z=n-So[i].first; theX=GETFA (x); y=GETFA (y); the if(size[x]<Size[y]) swap (x, y);98 intBes=que (Rt[y],rt[x]) +Z; About if(bes>ret) -ret=BES;101 merge (Rt[y],rt[x]);102size[x]+=Size[y];103lef[y]=x;104 } theprintf"%d\n", ret);106 return 0;107}
loj6198 Keith suffix array + and check set +trie