"Bzoj" "3238" "AHOI2013" diff (diff)

Source: Internet
Author: User

Title Link: www.lydsy.com/JudgeOnline/problem.php?id=3238

Suffix array

The implication of this problem is very strong ah ... A look is to use the suffix xx an algorithm, because this konjac only suffix array, so take the suffix array to write.

  

The requirements of this topic ... It is obvious that we can directly preprocess the sum of T (i) +t (j) to N (n-1) * (n+1)/2. (Should it be easy to push?) Draw it Yourself (example): When I is 1, J can be 2, 3, 4, 5. Then 1 calculates 4 times, 2~5 each time, then 2 counts three times, calculates once, 3 counts two times, 4, 5 calculates once, 4 counts once, 5 counts 1 times. Add up altogether, each count 4 times. The and is n (n+1)/2, a total of (n-1) times, and then a ride on the line. )

The difficulty lies in the reduction of LCP ... We can do this place directly on the height array, and we can see that each pair (I,J) corresponds to an interval on the height array, or even a point! (When i,j two sub-strings rank is connected) then again, the interval (point) on each height array corresponds to one of the LCP we require.

What good is this? The original act of violence is to enumerate i,j, use RMQ to calculate LCP, and then subtract, and now we convert directly to the LCP, regardless of who and whose LCP (in fact, does not fall on any one), so the problem is much simpler: the use of the particularity of LCP, we for each height[i], can find an interval [l,r], making height[i]=min (Height[l]~height[r]). The sum means I is the minimum value on this interval [l,r]. The logarithm of this lcp=height[i] is (l-i+1) * (r-i+1) "PS." As we have said before, [I,i] Such a point is also "that is, we can subtract from the answer 2*height[i]* (l-i+1) * (r-i+1) such a value.

But!! This will have a repeat calculation:

For a chestnut: height is 1 2 3 1 2 1 1 o'clock, the first 2 of the [l,r] interval is [1,7], the second is [1,7], there is obviously repeated calculation ([L,i] and [I,r] The two paragraphs overlap, that is, calculated two times) so we are calculating for each I can reach [l,r] Interval , an equal element is encountered and must be handled separately: for example, if you encounter an equal element to the right, you can continue to expand and the left encounter will stop.

  

Now the analysis is clear, the final question is: How to calculate l[i],r[i], that is, each height[i] corresponding to the interval?

Here we can use a thing called monotonic stack, maintenance stack elements height[j] is smaller than the current height[j], if the large pop, so that can O (N) to find out all the l[i],r[i].

Error: 1. When calculating the answer, you must add the (LL) type cast to the multiplication, or you will get an error.

2. When the stack is empty, it means that all elements on the left (right) are larger than the current, and the range should be the entire interval from the endpoint (1 or N) to I, not I (see Code)

1 /**************************************************************2 problem:32383 User:programmingape4 language:c++5 result:accepted6 time:3416 Ms7 memory:23244 KB8 ****************************************************************/9  Ten //Exercise six T1 yan Hongyu One //Bzoj 3238 A#include <cmath> -#include <cstdio> -#include <cstring> the#include <cstdlib> -#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) + #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + using namespacestd; A Const intn=500010; attypedefLong LongLL; - //#define DEBUG - intN,m,sa[n],c[n],wa[n],wb[n],wv[n],rank[n],height[n],l[n],r[n]; -   - intcmpint*r,intAintBintl) { -     returnR[A]==R[B] && r[a+l]==r[b+l]; in } -   to voidDA (Char*s,int*sa,intNintm) { +     inti,j,p,*x=wa,*y=WB; -Rep (i,m) c[i]=0; theRep (I,n) c[x[i]=s[i]]++; *F (I,1, M-1) c[i]+=c[i-1]; $D (i,n-1,0) sa[--c[x[i]]]=i;Panax Notoginseng      for(p=0, j=1;p <n;j<<=1, m=p) { -          for(p=0, I=n-j;i<n;++i) y[p++]=i; theRep (I,n)if(SA[I]&GT;=J) y[p++]=sa[i]-J; +           ARep (i,m) c[i]=0; theRep (I,n) c[x[y[i]]]++; +F (I,1, M-1) c[i]+=c[i-1]; -D (i,n-1,0) sa[--c[x[y[i]]]]=Y[i]; $Swap (x, y); p=1; x[sa[0]]=0; $F (I,1, N-1) x[sa[i]]=cmp (y,sa[i-1],SA[I],J)? P1: p++; -     } - } the   - voidCalheight (Char*s,int*sa,intN) {Wuyi     intk=0; theF (I,1, N) rank[sa[i]]=i; - Rep (i,n) { Wu         if(k) k--; -         intj=sa[rank[i]-1]; About          while(S[i+k]==s[j+k]) k++; $height[rank[i]]=K; -     } - } -   A intq[n],st[n],top=0; + CharS[n]; the intMain () { - //freopen ("Input.txt", "R", stdin); $ //freopen ("Output.txt", "w", stdout); thescanf"%s",&s); the     intn=strlen (s); theRep (I,n) s[i]=s[i]-'a'+2; thes[n]=0; -       inDA (s,sa,n+1, -); the calheight (s,sa,n); theheight[1]=height[n+1]=0; About   thell ans= (LL) ((ll) n (n1) * (n+1))/2, Delta=0;  the     //sum of T (i) and T (j) the       +top=0; -st[top++]=1; theF (I,1, N) {Bayi          while(Top && height[st[top-1]] > Height[i]) top--; the         if(top) l[i]=st[top-1]+1; the         Elsel[i]=1; -st[top++]=i; -     } the       thetop=0; theSt[top++]=n; r[n]=N; theD (I,n,1){ -          while(Top && height[st[top-1]] >= height[i]) top--; the         if(top) r[i]=st[top-1]-1; the         ElseR[i]=n;//!!!! thest[top++]=i;94     } the       the #ifdef Debug theF (I,1, N) printf ("%d", Height[i]);98printf"\ n"); AboutF (I,1, N) printf ("%d", L[i]); -printf"\ n");101F (I,1, N) printf ("%d", R[i]);102printf"\ n");103     #endif104F (I,2, N) { thedelta+= (LL)2* (LL) height[i]* (LL) (i-l[i]+1) * (LL) (r[i]-i+1);106 #ifdef Debug107printf"%d *%d *%d =%d\n", height[i],i-l[i]+1, r[i]-i+1, height[i]* (i-l[i]+1) * (r[i]-i+1));108         #endif109     } the #ifdef Debug111printf"%lld%lld\n", Ans,delta); the     #endif113ans-=Delta; theprintf"%lld\n", ans); the     return 0; the}
View Code

"Bzoj" "3238" "AHOI2013" diff (diff)

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.