# bzoj2555 SubString

https://www.lydsy.com/JudgeOnline/problem.php?id=2555

#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define N 1500000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
{
char ch=0;
int x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
#define lson son[x][0]
#define rson son[x][1]
int v[N],sz[N],sw[N],st[N],f[N],flag[N],son[N][2];
void pushup(int x){sz[x]=sz[lson]+sz[rson]+sw[x]+v[x];}
void update(int x){flag[x]^=1;swap(lson,rson);}
void pushdown(int x)
{
if(!flag[x])return;
if(lson)update(lson);
if(rson)update(rson);
flag[x]=0;
}
bool get(int x){return son[f[x]][1]==x;}
bool isroot(int x){return (son[f[x]][0]!=x)&&(son[f[x]][1]!=x);}
void rotate(int x)
{
int y=f[x],z=f[y],tx=get(x),ty=get(y),p=son[x][!tx];
if(!isroot(y))son[z][ty]=x;son[x][!tx]=y;son[y][tx]=p;
if(p)f[p]=y;f[y]=x;f[x]=z;pushup(y);pushup(x);
}
void splay(int x)
{
int cnt=0,tmp=x;
st[++cnt]=x;
while(!isroot(x))st[++cnt]=f[x],x=f[x];
for(int i=cnt;i>=1;i--)pushdown(st[i]);
x=tmp;
while(!isroot(x))
{
int y=f[x];
if(!isroot(y))rotate(get(x)==get(y)?y:x);
rotate(x);
}
pushup(x);
}
void access(int x)
{
for(int y=0;x;y=x,x=f[x])
{
splay(x);
sw[x]+=sz[rson];
rson=y;
sw[x]-=sz[rson];
pushup(x);
}
}
void makeroot(int x){access(x);splay(x);update(x);}
void cut(int x,int y){if(!x||!y)return;makeroot(x);access(y);splay(y);f[x]=son[y][0]=0;pushup(y);}
int root=1,last=1,size=1;
struct node{int len,lnk,nxt[26];}s[N];
void build(int k)
{
int cur=++size,p=last;
last=cur;v[cur]=1;s[cur].len=s[p].len+1;
while(p&&!s[p].nxt[k])s[p].nxt[k]=cur,p=s[p].lnk;
int q=s[p].nxt[k];
else
{
int clone=++size;
while(p&&s[p].nxt[k]==q)s[p].nxt[k]=clone,p=s[p].lnk;
}
}
int solve(int x)
{
if(!x||x==root)return 0;
makeroot(s[x].lnk);access(x);
return sw[x]+v[x];
}
char t[N*3],opt[20];
int decode(int k)
{
int len=strlen(t);
for(int i=0;i<len;i++)k=(k*131+i)%len,swap(t[i],t[k]);
return len;
}
int main()
{
scanf("%s",t);len=strlen(t);
for(int i=0;i<len;i++)build(t[i]-'A');
for(int o=1;o<=q;o++)
{
scanf("%s",opt);scanf("%s",t);
int len=decode(key);
if(opt[0]=='Q')
{
int x=root,flag=false,ans;
for(int i=0;i<len;i++)
{
int k=t[i]-'A';
if(!s[x].nxt[k]){flag=true;break;}
x=s[x].nxt[k];
}
if(flag)ans=0;else ans=solve(x);
key^=ans;printf("%d\n",ans);
}
else for(int i=0;i<len;i++)build(t[i]-'A');
}
return 0;
}

posted @ 2019-02-07 04:16  Creed-qwq  阅读(204)  评论(0编辑  收藏  举报