"bzoj3238" difference [AHOI2013] (suffix array + monotonic stack)

Source: Internet
Author: User

Topic Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=3238

This problem from about six months ago began to chew, but that year because some details did not adjust out, see sakits god Ben Blog After also did not understand where they hang, so he copied a problem. And now suddenly think of filling this hole (in fact, to review the suffix array template), a change of thinking before the problem ... However, I still do not know why the wrong way of writing ... Seems to be hanging on the weight of the sentence?

Solution (Accepted):

First we can look at this equation (see below), we can take it apart and become Sigma (Ti) +len (Tj)) -2*sigma (LCP (TI,TJ)). One of the previous items can be calculated as N (n+1) * (n-1)/2.

After the sum of the LCP, we can first use the suffix array to convert it to the height array of the interval minimum sum. Before the notation is to count the height array each value as the minimum value of the interval of how many, then to calculate the answer, but it seems likely to have repeated calculations, to add a lump of judgment.

But we can change the statistic, count the sum of the minimum of the interval of each number as the right endpoint, and add them up. Here is the use of a monotonous stack of a magical nature: a sequence is pressed into the monotone stack, the sequence of the suffix of the minimum/maximum value will be in the monotone stack appear. It can then be transformed into a statistical contribution to the sum of the minimum values for each number in the monotonic stack. The method of calculation is as below:

Furthermore, since the contribution of each number in the stack is not changed from the time it is pressed into the stack, the answer can be calculated as long as the number of presses and the number of rounds are maintained.

Then the problem is solved ^_^

Code:

#include <cstdio>#include<cmath>#include<cstring>#include<cstdlib>#include<ctime>#include<algorithm>#include<queue>#include<vector>#definell Long Long#defineMAXN 500010inline ll read () {ll tmp=0;CharC=getchar (), f=1;  while(c<'0'||'9'&LT;C) {if(c=='-') f=-1; C=GetChar ();}  while('0'<=c&&c<='9') {tmp=tmp*Ten+c-'0'; C=GetChar ();} returntmp*F;}Chars[maxn];ll sa[maxn],rk[maxn],tsa[maxn],trk[maxn],sum[maxn],h[maxn];ll st[maxn];ll N;voidGetsa () {intI,tot,cnt= the, Len;  for(i=1; i<=cnt;i++) sum[i]=0;  for(i=0; i<n;i++) trk[i+1]=s[i]+1, ++sum[trk[i+1]];  for(i=2; i<=cnt;i++) sum[i]+=sum[i-1];  for(i=n;i;i--) sa[sum[trk[i]]--]=i; CNT=1; rk[sa[1]]=1;  for(i=2; i<=n;i++){         if(trk[sa[i]]!=trk[sa[i-1]]) ++cnt; rk[sa[i]]=CNT; }     for(len=1; cnt<n;len<<=1){         for(i=1; i<=n;i++) trk[i]=Rk[i];  for(i=1; i<=n;i++) sum[i]=0; tot=0;  for(i=n-len+1; i<=n;i++) tsa[++tot]=i;  for(i=1; i<=n;i++)if(Sa[i]>len) tsa[++tot]=sa[i]-Len;  for(i=1; i<=n;i++) rk[i]=trk[tsa[i]],++Sum[rk[i]];  for(i=2; i<=cnt;i++) sum[i]+=sum[i-1];  for(i=n;i;i--) sa[sum[rk[i]]--]=Tsa[i]; CNT=1; rk[sa[1]]=1;  for(i=2; i<=n;i++){            if(trk[sa[i]]!=trk[sa[i-1]]|| trk[sa[i]+len]!=trk[sa[i-1]+len]) ++cnt; rk[sa[i]]=CNT; }    }     for(i=1; i<=n;i++){        if(rk[i]==1)Continue; if(h[rk[i-1]]>0) h[rk[i]]=h[rk[i-1]]-1;Elseh[rk[i]]=0;  while(s[i+h[rk[i]]-1]==s[sa[rk[i]-1]+h[rk[i]]-1])++H[rk[i]]; }}intMain () {inti; scanf ("%s", s); n=strlen (s);    Getsa (); LL top=0, Ans= (n1) *n* (n+1)/2, tot=0; st[0]=0; h[0]=-(1<< -);  for(i=1; i<=n;i++){         while(h[st[top]]>=H[i]) {Tot-=h[st[top]]* (st[top]-st[top-1]); --top; } tot+=h[i]* (I-st[top]); st[++top]=i; Ans-=2*tot; } printf ("%lld", ans);}
bzoj3238

"bzoj3238" difference [AHOI2013] (suffix array + monotonic stack)

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.