[BZOJ4556] [tjoi2016&&heoi2016] String (two-part answer + suffix array +rmq+ Chairman tree)

Source: Internet
Author: User

4556: [tjoi2016&heoi2016] string time limit:20 Sec Memory limit:128 MB
submit:1360 solved:545
[Submit] [Status] [Discuss] Description's sister's birthday, her little friend bought a birthday gift from some east. Birthday presents in a magical box. Outside the box was written a string s of N, and M questions. Good sister must answer this question correctly, in order to open the box to get gifts, promotion, as CEO, married Gaofu, embark on the pinnacle of life. Each problem has a,b,c,d four parameters, ask you substring s[a. b] for all substrings and s[c. What is the maximum length of the longest public prefix for d]? My sister is not good at doing such a problem, so she asked you for help, how can you assist her? The first line of input inputs has two positive integer n,m, each representing the length of the string and the number of queries. The next line is a string of n long. Next m line, each line has 4 number a,b,c,d, indicates asks S[a. b] for all substrings and s[c. The maximum value of the longest public prefix for d]. 1<=n,m<=100,000, string with only lowercase English letters, a<=b,c<=d,1<=a,b,c,d<=n Output

For each query, output the answer.

Sample Input 5 5
Aaaaa
1 1 1 5
1 5 1 1
2 3 2 3
2 4 2 3
2 3 2 4Sample Output 1
1
2
2
2HINT Source [Submit] [Status] [Discuss]

No brain data structure in the Triad, first build a group to find the height of the array and St table, and then the two-point answer, found that meet the two-point limit of the range, and then according to this range in the chairman of the tree to find whether there is a solution in [a,b-mid+1].

Code a long error, and this problem my program constant do not know why the other people's twice times, try all kinds of cards often skills are useless, finally in my final moment to give up a dropped: The Chairman tree query not written que (rt[r])-que (Rt[l-1]), and to write Que (rt[l-1 ],RT[R]), which reduces the time to repeat positioning. This optimization is significantly more effective than other methods such as register and array dimension interchange.

#include <cstdio> #include <cstring> #include <algorithm> #define RG Register Int#define Rep (i,l,r) for (RG i= (L); i<= (R); i++) using namespace Std;const int N=200100;int N,m,a,b,c,d,nd,h[n],x[n],y[n],s[n],sa[n],rt[n],rk [N],lg[n],ls[n*20],rs[n*20],sm[n*20],st[n][20];char S[n];void Rd (int &x) {x=0; char Ch=getchar (); while (ch< ' 0 ' || Ch> ' 9 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();} BOOL Cmp (RG L,rg R,rg k) {return l+k<=n && r+k<=n && y[l]==y[r] && y[l+k]==y[r+k];} void Build (rg m) {memset (y,0,sizeof (y)); Rep (i,0,m) s[i]=0;rep (i,1,n) s[x[i]=s[i]-' a ' +1]++;rep (i,1,m) s[i]+=s[i-1]; for (RG i=n; i; i--) sa[s[x[i]]--]=i;for (RG k=1,p=0; p<n; k<<=1,m=p) {p=0;rep (i,n-k+1,n) y[++p]=i;rep (i,1,n) if ( sa[i]>k) Y[++p]=sa[i]-k;rep (i,0,m) s[i]=0;rep (i,1,n) s[x[y[i]]]++;rep (i,1,m) s[i]+=s[i-1];for (rg i=n; i; i--) sa[s[ X[y[i]]]--]=y[i];rep (I,1,n) y[i]=x[i]; P=1; X[sa[1]]=1;rep (i,2,n) x[sa[i]]=cmp (Sa[i],sa[i-1],k)? P:++p;}} void GetHeight () {int k=0; Rep (i,1,n) Rk[sa[i]]=i;rep (i,1,n) {for (RG j=sa[rk[i]-1]; i+k<=n && j+k<=n && Amp S[I+K]==S[J+K]; k++); h[rk[i]]=k; if (k) k--;}} void Rmq () {Rep (i,1,n) St[i][0]=h[i];rep (J,1,lg[n]) Rep (i,1,n-(1<<j) +1) st[i][j]=min (st[i][j-1],st[i+ (1< < (J-1))][j-1]);} void Add (int y,int &x,int l,int r,int pos) {x=++nd; sm[x]=sm[y]+1; ls[x]=ls[y]; Rs[x]=rs[y];if (l==r) return; int mid= ( L+r) >>1;if (pos<=mid) Add (Ls[y],ls[x],l,mid,pos); else Add (rs[y],rs[x],mid+1,r,pos);} int que (int y,int x,int l,int r,int l,int R) {if (!x) return 0;if (l==l && r==r) return sm[x]-sm[y];int mid= (l+r) &G T;>1;if (R<=mid) return que (ls[y],ls[x],l,mid,l,r); else if (L>mid) return que (rs[y],rs[x],mid+1,r,l,r); else Return que (ls[y],ls[x],l,mid,l,mid) +que (rs[y],rs[x],mid+1,r,mid+1,r);} int Ask (RG L,rg R) {RG t=lg[r-l+1]; return min (st[l][t],st[r-(1<<t) +1][t]);} void GetRange (RG x,int &l,int &r,rg mid) {l=r=x;for (int i=17; ~I && r<n; i--) if (r+ (1<<i) <=n && Ask (x+1,r+ (1<<i)) >=mid) r=r+ (1<<i); for (int i=17; ~i && l>1; i--) if (L (1<<i) >=0 && Ask (L (1<<i) +1,x) >=mid) l=l-(1<<i);} int Jud (RG mid) {int l,r; GetRange (Rk[c],l,r,mid); if (Que (rt[l-1],rt[r],1,n,a,b-mid+1) >0) return 1; else return 0;} int main () {freopen ("bzoj4556.in", "R", stdin), Freopen ("Bzoj4556.out", "w", stdout), scanf ("%d%d%s", &n,&m,s+1 ); lg[1]=0; Rep (I,2,n) Lg[i]=lg[i>>1]+1;build (30); GetHeight (); RMQ (); Rep (i,1,n) Add (Rt[i-1],rt[i],1,n,sa[i]), while (m--) {Rd (a); Rd (b); Rd (c); Rd (d); if (!jud (1)) {printf ("0\n"); Continue }int l=1,r=min (d-c+1,b-a+1), Ans=0;while (l<r) {int mid= (l+r) >>1;if (Jud (mid)) ans=mid,l=mid+1; else r=mid;} if (Jud (L)) printf ("%d\n", L); else printf ("%d\n", ans);} return 0;}

[BZOJ4556] [tjoi2016&&heoi2016] String (two-part answer + suffix array +rmq+ Chairman tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.