Bzoj_3207_ 's mocking Plan 1_ (hash+ chairman Tree)

Source: Internet
Author: User

Describe

http://www.lydsy.com/JudgeOnline/problem.php?id=3207

Give a string of length \ (n\), and \ (m\) A string of length \ (k\), for each string of length \ (k\) in the original string \ ([x,y]\) interval has occurred.

Analysis

This problem requires a comparison of the length of \ (k\) of the string, so we put the hash value of these strings are calculated, the problem is translated into the "([x,y]\) interval has been a hash value."

To find out how many times a certain value in the interval has occurred, you can use the Chairman tree.

P.S.

1. Learn the chairman of the tree pointer to the wording, more than the array slow ah ... It seems necessary to learn the array of Balance tree notation ... But the advantage is that you don't have to count your space ...

2. The problem of their own space will be the MLE, so the card space restrictions on the good, the estimated data is relatively small, can be too.

Array:

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn=200000+5, x= -;5typedef unsignedLong Longull;6 ConstUll inf=~0ull;7 intn,m,k,cnt;8 intA[MAXN],RT[MAXN];9Ull s[maxn],x=1;Ten structnode{intL,r,s; }t[maxn* -]; OneInlineintReadint&AMP;X) {x=0;intk=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c=='-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) x=x*Ten+c-'0';returnx*=K;} A voidUpdate (ull l,ull R,int&Pos,ull D) { -T[++cnt]=t[pos]; pos=cnt; t[pos].s++; -     if(L==R)return; theUll mid=l+ (r-l)/2; -     if(d<=mid) Update (L,MID,T[POS].L,D); -     ElseUpdate (mid+1, r,t[pos].r,d); - } + BOOLQueryintXinty,ull l,ull r,ull D) { -     if(t[y].s-t[x].s==0)return false; +     if(L==R)return true; AUll mid=l+ (r-l)/2; at     if(D<=mid)returnquery (t[x].l,t[y].l,l,mid,d); -     Else returnQuery (t[x].r,t[y].r,mid+1, r,d); - } - intMain () { - Read ( n); Read (m); Read (k) ; -      for(intI=1; i<=n;i++){ in read (a[i]); -s[i]=s[i-1]*x+(ull) a[i]; to     } +      for(intI=1; i<=k;i++) x*=X; -      for(inti=k;i<=n;i++) rt[i]=rt[i-1], update (0ull,inf,rt[i],s[i]-s[i-k]*x); the      for(intI=1; i<=m;i++){ *Ull hash=0;BOOLans; $         intl,r; Read (l); Read (r);Panax Notoginseng          for(intj=1, t;j<=k;j++) hash=hash*x+read (t); -         if(r+1-L&LT;K) ans=false; the         ElseAns=query (rt[l+k-2],rt[r],0ull,inf,hash); +Ans?puts ("No"):p UTS ("Yes"); A     } the     return 0; +}
View Code

Pointer:

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn=200000+5, x= -;5typedef unsignedLong Longull;6 ConstUll inf=~0ull;7 intn,m,k,cnt;8 intA[MAXN];9Ull s[maxn],x=1;Ten structnode{ Onenode* l,* R;ints; A node () {} -Node *l,node* R,ints): L (L), R (R), s (s) {} -}* rt[maxn],*NULL; theInlineintReadint&AMP;X) {x=0;intk=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c=='-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) x=x*Ten+c-'0';returnx*=K;} -node* Update (node*t,ull l,ull r,ull D) { -     if(L==R)return NewNodeNULL,NULL, t->s+1); -Ull mid=l+ (r-l)/2; +     if(D<=mid)return NewNode (update (T-&GT;L,L,MID,D), t->r,t->s+1); -     Else return NewNode (t->l,update (t->r,mid+1, r,d), t->s+1); + } A BOOLQuery (node* x,node*y,ull l,ull r,ull D) { at     if(y->s-x->s==0)return false; -     if(L==R)return true; -Ull mid=l+ (r-l)/2; -     if(D<=mid)returnQuery (x->l,y->l,l,mid,d); -     Else returnQuery (x->r,y->r,mid+1, r,d); - } in intMain () { - Read ( n); Read (m); Read (k) ; to     NULL=Newnode; +     NULL->l=NULL,NULL->r=NULL,NULL->s=0; -      for(intI=1; i<=n;i++){ the read (a[i]); *s[i]=s[i-1]*x+(ull) a[i]; $     }Panax Notoginseng      for(intI=1; i<=k;i++) x*=X; -rt[k-1]=NewNodeNULL,NULL,0); the      for(inti=k;i<=n;i++) Rt[i]=update (rt[i-1],0ull,inf,s[i]-s[i-k]*x); +      for(intI=1; i<=m;i++){ AUll hash=0;BOOLans; the         intl,r; Read (l); Read (r); +          for(intj=1, t;j<=k;j++) hash=hash*x+read (t); -         if(r+1-L&LT;K) ans=false; $         ElseAns=query (rt[l+k-2],rt[r],0ull,inf,hash); $Ans?puts ("No"):p UTS ("Yes"); -     } -     return 0; the}
View Code

Bzoj_3207_ 's mocking Plan 1_ (hash+ 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.