"BZOJ1014" "JSOI2008" Martians prefix splay processing interval, hash+dichotomy (two points) check out solution

Source: Internet
Author: User

Test instructions not repeat, too clear.

The first thing to say is to create an SPT based on the original string, and suggest adding a blank character.

The composition of a tree, according to the order of the size of the balance tree can be kept as a sequence, and can be found by rank k of the sequence.


After the tree has been constructed, click Insert, point to modify, ask God horse's code has detailed comments.

/*bzoj 1014 Novice Look at the time to suggest starting from the main function, according to the order of operation of the brain simulation. P.S. The hash of this code is a natural overflow rather than a mod operation. */#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include < Algorithm> #define N 250010#define is (x) (son[fa[x]][1]==x) using namespace Std;typedef unsigned long long ll;char start [N];int Digit[n]; LL power[n]={1};struct Node{int root,n; LL hash[n];int val[n],fa[n],son[n][2],size[n];inline void Update (int p) {size[p]=size[son[p][0]]+size[son[p][1]]+1; hash[p]=hash[son[p][0]]*power[size[son[p][1]]+1]+val[p]*power[size[son[p][1]]]+hash[son[p][1]];/* This code is to get a 53-bit hash value, hash[p] represents the hash value of the paragraph, think about it well understood. */}inline void Build (int l,int r,int mid) {if (l<mid)/* There are several */{int lmid=l+mid-1>>1; on the left Build (l,mid-1,lmid); fa[lmid]=mid;son[mid][0]=lmid;} if (mid<r)/* There are several */{int on the right rmid=mid+1+r>>1; Build (mid+1,r,rmid); fa[rmid]=mid;son[mid][1]=rmid;} Val[mid]=digit[mid],update (mid);/*val represents the current character (digital version) */}inline void link (int x,int y,int d) {son[y][d]=x;fa[x]=y;} inline void Rotate (int x) {int y=fa[x],z=fa[Y],id=is (x), t=son[x][!id];if (t) fa[t]=y;son[y][id]=t;link (X,z,is (y)); link (y,x,!id); update (y);} inline void splay (int x,int k) {int y,z;while (fa[x]!=k) {y=fa[x];z=fa[y];if (z==k) {Rotate (x); break;} if (Is (x) ==is (y)) Rotate (y), Rotate (x), else Rotate (x), Rotate (x);} Update (x); if (!k) root=x;} The inline int Select (int rank,int k)/* Locates the node and rotates it to the son of K (k=0 to the root) */{if (Size[root]<rank) return-1;/* cannot find */int x=root; while (Size[son[x][0]]+1!=rank)/* Loop condition: Root is not the node you are looking for */{if (size[son[x][0]]+1>rank) X=son[x][0];else rank=rank-size[ SON[X][0]]-1,X=SON[X][1];} /* Already found node */splay (x,k); return x;} inline void newnode (int &x,int y,int W) {x=++n;son[x][0]=son[x][1]=0;val[x]=w;fa[x]=y;size[x]=1;} inline void Insert (int x,int p) {int l=select (x,0), R=select (x+1,l),/*x to Root, x+1 to the right child node of the root, that is, the left subtree of R is guaranteed to be Null*/newnode (Son[r][0] , r,p); Splay (n,0);} inline void change (int x,int p) {x=select (x,0), Val[x]=p,splay (x,0);} inline bool Check (int a,int b,int len) {int x; Select (a-1,0); X=select (a+len,root);/* The interval (len starting with a) rotate to lrt*/if (x==-1) return 0; LL hash1=hash[Son[x][0]]; Select (b-1,0); X=select (b+len,root); if (x==-1) return 0; LL Hash2=hash[son[x][0]];return Hash1==hash2;}} Tree;void handle () {int I,m,l,r,mid,l,r;char a[5];for (int i=1;i<n;i++) power[i]=power[i-1]*53;scanf ("%s", start); Tree.n=strlen (start) +2;/* add a white space character */for (int i=2;i<=tree.n-1;i++) digit[i]=start[i-2]-' A ' +1;tree.root= (1 + TREE.N) >>1,tree. Build (1,tree.n,1+tree.n>>1);/* */SCANF ("%d", &m), for (int i=1;i<=m;i++) {scanf ("%s", a);/* L + The reason for 1 is because the sequence adds a white space character */if (a[0]== ' Q ') {scanf ("%d%d", &l,&r), L=0,r=tree.n;while (l<r) {/* two diverged */mid=l+r> >1;if (Tree.check (l+1,r+1,mid)) L=mid+1;else R=mid;} printf ("%d\n", L-1);} else if (a[0]== ' R ') {scanf ("%d%s", &l,a), tree. Change (l+1,a[0]-' a ' + 1);} ELSE{SCANF ("%d%s", &l,a), tree. Insert (l+1,a[0]-' a ' + 1);}}} int main () {//freopen ("test.in", "R", stdin); handle (); return 0;}


Copy to Google TranslateTranslation Results

"BZOJ1014" "JSOI2008" Martians prefix splay processing interval, hash+dichotomy (two points) check out solution

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.