后缀平衡树
考虑如果可以离线,只需构出后缀数组套上线段树。在线的话我们需要一个在线的后缀数组,结合题意中后缀的含义,用后缀平衡树即可。
#include<set>
#include<cstdio>
#include<algorithm>
#define N 800005
using namespace std;
namespace runzhe2000
{
typedef double db;
db alpha = 0.7, badl, badr; char s[N];
int n, m, len, code, tail, in[N];
struct node
{
node *ch[2], *fa;
db v; int siz, id;
}mem[N], *tot, *null, *root, *pos[N], *bad, *q[N];
node *newnode(){node *p = ++tot; *p = *null;return p;}
int type(node *x){return x->fa->v < x->v ? 1 : 0;}
void init()
{
bad = null = tot = mem;
null->fa = null->ch[0] = null->ch[1] = null;
null->v = null->siz = 0;
pos[0] = root = newnode();
pos[0]->v = 0.5; pos[0]->siz = 1; pos[0]->id = 0;
}
node* insert(node *fa, node *x, db l, db r, int id)
{
if(x == null){pos[id] = x = newnode(); x->fa = fa; x->id = id; x->siz = 1; x->v = (l+r)/2; return x;}
if(s[id] < s[x->id] || (s[id] == s[x->id] && pos[id-1]->v < pos[x->id-1]->v)) x->ch[0] = insert(x, x->ch[0], l, (l+r)/2, id);
else x->ch[1] = insert(x, x->ch[1], (l+r)/2, r, id);
x->siz++; if(x->siz * alpha < max(x->ch[0]->siz, x->ch[1]->siz)) bad = x, badl = l, badr = r;
return x;
}
void fetch(node *x)
{
if(x == null) return; q[++tail] = x;
fetch(x->ch[0]); fetch(x->ch[1]);
}
bool cmp(node *x, node *y){return x->v < y->v;}
node* rebuild(int L, int R, db l, db r, node *fa)
{
if(L > R) return null;
int mid = (L+R)>>1;
q[mid]->fa = fa;
q[mid]->v = (l+r)/2;
q[mid]->ch[0] = rebuild(L,mid-1,l,(l+r)/2,q[mid]);
q[mid]->ch[1] = rebuild(mid+1,R,(l+r)/2,r,q[mid]);
q[mid]->siz = q[mid]->ch[0]->siz + q[mid]->ch[1]->siz + 1;
return q[mid];
}
void check_bad()
{
if(bad == null) return;
tail = 0; node *badfa = bad->fa; fetch(bad);
sort(q+1, q+1+tail, cmp);
node *p = rebuild(1,tail,badl,badr,badfa);
if(badfa != null) badfa->ch[type(p)] = p; else root = p;
bad = null;
}
struct seg{int v;}t[N*5];
void build(int x, int l, int r)
{
t[x].v = 0; if(l == r) return; int mid = (l+r)>>1;
build(x<<1,l,mid); build(x<<1|1,mid+1,r);
}
void modi(int x, int l, int r, int p, int v)
{
if(l == r){t[x].v = v; return;} int mid = (l+r)>>1;
p <= mid ? modi(x<<1,l,mid,p,v) : modi(x<<1|1,mid+1,r,p,v);
if(!t[x<<1].v) t[x].v = t[x<<1|1].v;
else if(!t[x<<1|1].v) t[x].v = t[x<<1].v;
else t[x].v = pos[in[t[x<<1].v]]->v > pos[in[t[x<<1|1].v]]->v ? t[x<<1|1].v : t[x<<1].v;
}
int query(int x, int l, int r, int ql, int qr)
{
if(ql <= l && r <= qr) return t[x].v;
int mid = (l+r)>>1, p1 = 0, p2 = 0;
if(ql <= mid) p1 = query(x<<1,l,mid,ql,qr);
if(mid < qr) p2 = query(x<<1|1,mid+1,r,ql,qr);
if(!p1)return p2; if(!p2) return p1; return pos[in[p1]]->v > pos[in[p2]]->v ? p2 : p1;
}
void main()
{
scanf("%d%d%d%d%s",&n,&m,&len,&code,s+1);
for(int i = 1, ii = len>>1; i <= ii; i++) swap(s[i], s[len-i+1]);
build(1,1,n); init();
for(int i = 1; i <= len; i++)
insert(root, root, 0, 1, i), check_bad();
for(int i = 1; i <= n; i++)
{
int p; scanf("%d",&p);
in[i] = p; modi(1,1,n,i,i);
}
char opt[5]; int ans = 0;
for(int i = 1; i <= m; i++)
{
scanf("%s",opt);
if(opt[0] == 'I')
{
int c; scanf("%d",&c);
code ? (c ^= ans) : 0;
s[++len] = c + 'a';
insert(root, root, 0, 1, len), check_bad();
}
else if(opt[0] == 'C')
{
int x, to; scanf("%d%d",&x,&to);
in[x] = to;
modi(1,1,n,x,x);
}
else
{
int l, r; scanf("%d%d",&l,&r);
ans = query(1,1,n,l,r);
printf("%d\n",ans);
}
}
}
}
int main()
{
runzhe2000::main();
}