2434: [Noi2011] Ali typewriter time limit:10 Sec Memory limit:256 MB
submit:1861 solved:1070
[Submit] [Status] [Discuss] Description
Ali like to collect all kinds of strange things, he recently Amoy to an old-fashioned typewriter. There are only 28 keys on the typewriter, with 26 lowercase English letters and ' B ' and ' P ' two letters printed respectively.
The ALI study found that the typewriter worked like this:
L Enter lowercase letters and the letter is added to a groove in the typewriter (the letter is added to the end of the groove).
L PRESS the ' B ' button and the last letter in the typewriter groove disappears.
L PRESS the ' P ' button and the typewriter will print all the existing letters in the groove and wrap them on the paper, but the letters in the groove will not disappear.
For example, Ali input APAPBBP, the characters printed on the paper are as follows:
A
Aa
Ab
We numbered the printed string on the paper from 1 onwards to N. Typewriter has a very interesting function, in the typewriter hidden a small keyboard with a number, on the keypad input two number (x, y) (where 1≤x,y≤n), the typewriter will show the first x printed string in the first number of printed strings in the occurrence of how many times.
Ali found this feature was very excited, he wanted to write a program to complete the same function, can you help him?
Input
The first line of input contains a string that gives all the characters of the beaver input in the order of Ali input.
The second line contains an integer m, which indicates the number of queries.
The next M-line describes all the queries entered by the keypad. Where line I contains two integers x, y, which indicates that I inquired for (x, y).
Output
Output m line, where line I contains an integer representing the answer to the I query.
Sample InputAPAPBBP
3
1 2
1 3
2 3
Sample Output2
1
0HINT
1<=n<=10^5
1<=m<=10^5
Input total length <=10^5
Ideas
Build the AC automaton with all the strings and find the fail array, and use the fail pointer to build the fail tree, where the father is the largest suffix of the son. For inquiry (x, y), set the tail node in the automaton to POS, that is, the number of points on the path of the statistic pos-y in the subtree of X.
All nodes in a tree's subtree correspond to a continuous interval on the DFS sequence, and after the DFS sequence is obtained, statistics can be converted into intervals and problems, using bit.
Offline processing All inquiries, statistics all inquiries about Y to que[y], and then with O (n) plus the node, we have all the nodes on the road corresponds to +1, when access to the node RT, the ROOT-RT path of all the nodes have been visited, processing about RT inquiry can be.
The total time complexity is O (NLOGN).
Code
1#include <cstdio>2#include <vector>3#include <queue>4#include <cstring>5#include <iostream>6#include <algorithm>7 #definefor (A,B,C) for (int a= (b); a<= (c); a++)8 using namespacestd;9 Ten Const intN = 1e5+Ten; One CharS[n]; A intC[n],ans[n],n; -vector<pair<int,int> >Que[n]; - the voidAddintXintv) { - while(x<n) c[x]+=v,x+=x&-x; - } - intQueryintx) { + intres=0; - while(x) res+=c[x],x-=x&-x; + returnRes; A } at structAcauto { - intsz,ch[n][ -],FA[N],L[N],R[N],POS[N],F[N],DFSC; -vector<int>G[n]; -Acauto () {sz=1;d fsc=0; memset (CH,0,sizeof(CH)); } - voidInsert () { - intu=0, id=0; in for(intI=0; s[i];i++) { - if(s[i]=='P') pos[++id]=u; to Else if(s[i]=='B') u=Fa[u]; + Else { - intc=s[i]-'a'; the if(!Ch[u][c]) { *Ch[u][c]=sz; Fa[sz]=u; sz++; $ }Panax Notoginsengu=Ch[u][c]; - } the } + } A voidGet_fail () { thequeue<int>Q; +f[0]=0; - for(intC=0,v;c< -; C + +) $ if(ch[0][C]) f[ch[0][c]]=0, Q.push (ch[0][c]); $ while(!Q.empty ()) { - intQr=Q.front (); Q.pop (); - for(intC=0;c< -; C + +) { the intu=Ch[qr][c]; - if(!u)Continue;WuyiQ.push (U);intv=F[QR]; the while(V&&!ch[v][c]) v=F[v]; -f[u]=Ch[v][c]; Wu } - } About for(intI=1; i<sz;i++) $ G[f[i]].push_back (i); - } - voidGET_DFSC (intu) { -l[u]=++DFSC; A for(intI=0; I<g[u].size (); i++) + GET_DFSC (G[u][i]); ther[u]=DFSC; - } $ voidsolve () { the intId=0, u=0; theAdd (l[0],1); the for(intI=0; s[i];i++) { the if(s[i]=='P') { -id++; in for(intj=0; J<que[id].size (); j + +) { the intx=Pos[que[id][j].first]; theAns[que[id][j].second]=query (R[x])-query (l[x]-1); About } the } the Else if(s[i]=='B') Add (l[u],-1), u=Fa[u]; the Elseu=ch[u][s[i]-'a'],add (L[u],1); + } - } the }ac;Bayi the voidReadint&x) { the CharC=getchar ();intf=1; x=0; - while(!isdigit (c)) {if(c=='-') f=-1; c=GetChar ();} - while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar (); thex*=F; the } the intMain () { thescanf"%s", s); - Ac.insert (); the Ac.get_fail (); theAC.GET_DFSC (0); the read (n);94 intx, y; the for(intI=0; i<n;i++) { the read (x), read (y); the Que[y].push_back (Make_pair (x,i));98 } About ac.solve (); - for(intI=0; i<n;i++)101printf"%d\n", Ans[i]);102 return 0;103}
Bzoj 2434 [Noi2011] Ali typewriter (fail tree + off-line processing +bit)