POJ3368 Frequent values (RMQ segment tree)

Source: Internet
Author: User

The topic probably says to an increment sequence, asking the interval to appear the most number.

Using the MO team algorithm is more intuitive, although it should be t. It seems to be the Chairman tree. But the sequence of questions given is ordered, and the same number converges together.

Consider dividing the sequence into a paragraph so that each segment contains a very large number of the same

This is for each interval query:

      • It is possible that the left or right side of this interval does not contain a complete paragraph, and that the length of the section is double-searched for the left or right end points.
      • And the removal of the two sides is not complete, but also requires a number of the middle of the maximum length of the complete section, this is used RMQ to quickly solve the

So this problem can be solved.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineMAXN 2222226 7 intLREC[MAXN],RREC[MAXN];8 9 inttree[maxn<<2],n,x,y;Ten voidUpdateintIintJintk) { One     if(i==j) { Atree[k]=y; -         return; -     } the     intMid=i+j>>1; -     if(x<=mid) Update (i,mid,k<<1); -     ElseUpdate (mid+1,j,k<<1|1); -Tree[k]=max (tree[k<<1],tree[k<<1|1]); + } - intQueryintIintJintk) { +     if(x>y)return 0; A     if(X<=i && j<=y) { at         returnTree[k]; -     } -     intMid=i+j>>1, res=0; -     if(X<=mid) Res=max (Res,query (i,mid,k<<1)); -     if(Y>mid) Res=max (Res,query (mid+1,j,k<<1|1)); -     returnRes; in } -  to intSEQ[MAXN]; + intMain () { -     intn,q,a,b; the      while(~SCANF ("%d", &n) &&N) { *scanf"%d",&q); $          for(intI=1; i<=n; ++i) {Panax Notoginsengscanf"%d", seq+i); -         } theseq[n+1]=111111; +  A         intrn=0; thelrec[0]=1; +          for(intI=1; i<=n+1; ++i) { -             if(seq[i]!=seq[i+1]){ $rrec[rn]=i; $rn++; -lrec[rn]=i+1; -             } the         } - Wuyimemset (Tree,0,sizeof(tree)); the          for(n=1; n<rn; n<<=1); -          for(intI=0; i<rn; ++i) { WuX=i; y=rrec[i]-lrec[i]+1; -Update0, N-1,1); About         } $  -         intI,j,res; -          while(q--){ -scanf"%d%d",&a,&b); AI=lower_bound (Rrec,rrec+rn,a)-Rrec; +J=upper_bound (lrec,lrec+rn,b)-lrec-1; the             if(rrec[i]>=b) { -printf"%d\n", b-a+1); $                 Continue; the             } theRes=max (rrec[i]-a+1, b-lrec[j]+1); thex=i+1; y=j-1; theRes=max (Res,query (0, N-1,1)); -printf"%d\n", res); in         } the     } the     return 0; About}

POJ3368 Frequent values (RMQ segment 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.