P4036 [JSOI2008] 火星人

#include <bits/stdc++.h>
#define int long long
using namespace std;
int len;
int m;
int rt = 0;
int has[1000010];
void init()
{
	srand(1);
    has[0] = 1;
    for(int i = 1;i <= 1000000;i++)
        has[i] = has[i - 1] * 101;
    //cout << has[] << endl;
}
char s[1000010];
struct edge
{
    int val;
	int lson;
	int s;
	int rson;
    int kth;
    int siz;
}e[1000010];
int cnt = 0;
int newnode(int xx)
{
    e[++cnt].val = xx;
    e[cnt].kth = rand();
    e[cnt].s = xx;
    e[cnt].siz = 1;
    return cnt;
}
void up(int x)
{
	e[x].siz = e[e[x].lson].siz + e[e[x].rson].siz + 1;
    e[x].s = e[e[x].lson].s * has[e[e[x].rson].siz + 1] + e[x].val * has[e[e[x].rson].siz] + e[e[x].rson].s;
}
int merge(int A,int B)
{
	///cout << A << " " << B << endl;
    if(!A || !B)return A + B;
    if(e[A].kth < e[B].kth)
    {
        e[A].rson = merge(e[A].rson,B);
        up(A);
        return A;
    }
    else
    {
        e[B].lson = merge(A,e[B].lson);
        up(B);
        return B;
    }
}
pair<int,int> split(int x,int val)
{
    if(!x)return pair<int,int>();
    ///cout << x << endl;
    if(val <= e[e[x].lson].siz)
    {
        pair<int,int> s = split(e[x].lson,val);
        e[x].lson = s.second;
        s.second = x;
        up(x);
        return s;
    }
    else
    {
        pair<int,int> s = split(e[x].rson,val - e[e[x].lson].siz - 1);
        e[x].rson = s.first;
        s.first = x;
        up(x);
        return s;
       /* for(int i = 1;i <= len;i++)
        {
        	printf("%d",f[i]); 
		}*/
    }
}
int Has(int L,int R)
{
    if(L > R)return 0;
    pair<int,int> retl = split(rt,R);
    pair<int,int> retr = split(retl.first,L - 1);
    int res = e[retr.second].s;
    retl.first = merge(retr.first,retr.second);
    rt = merge(retl.first,retl.second);
    ///cout << L << " " << R << endl;
    return res;
}
void add(int x,int val)
{
    pair<int,int> retl = split(rt,x);
    retl.first = merge(retl.first,newnode(val));
    rt = merge(retl.first,retl.second);
    len++;
}
int query(int x,int y)
{
    int l = 0,r = min(len - x + 1,len - y + 1);
    while(l < r)
    {
    	//cout << l << " " << r << endl;
        int mid = (l + r + 1) / 2;
        if(Has(x,x + mid - 1) == Has(y,y + mid - 1))l = mid;
        else r = mid - 1;
    }
    return l;
}
void updata1(int x,int val)
{
    pair<int,int> retl = split(rt,x);
    pair<int,int> retr = split(retl.first,x - 1);
    e[retr.second].s = e[retr.second].val = val;
    retl.first = merge(retr.first,retr.second);
    rt = merge(retl.first,retl.second);
}
signed main()
{
	init();
	int x,y;
    scanf("%s",s + 1);
	scanf("%lld",&m);
    len = strlen(s + 1);
    for(int i = 1;i <= len;i++)
        rt = merge(rt,newnode(s[i] - 'a' + 1));
    for(int i = 1;i <= m;i++)
    {
        scanf("%s",s + 1);
        if(s[1] == 'Q')
        {
            scanf("%lld%lld",&x,&y);
            printf("%lld\n",query(x,y));
        }
        else if(s[1] == 'R')
        {
            scanf("%lld%s",&x,s + 1);
            updata1(x,s[1] - 'a' + 1);
        }
        else
        {
            scanf("%lld %s",&x,s + 1);
            add(x,s[1] - 'a' + 1);
        }
    }
    return 0;
}
posted @ 2024-09-24 20:27  kanade16  阅读(8)  评论(0编辑  收藏  举报