求助
查看原帖
求助
377440
Y2y7m楼主2023/2/2 00:58

样例过了爆零

#include <bits/stdc++.h>

using namespace std;
#define ull unsigned long long
#define lson t[i].ch[0]
#define rson t[i].ch[1]
const int maxn=2e5+10;
const ull p=131;
int n,m;
ull power[maxn];
struct fhq_treap
{
    int ch[2];
    int pri;
	ull val,hs;
    int sz;
}t[maxn];
int cnt,root;
int newnode(int val)
{
	t[++cnt].hs=t[cnt].val=val;
	t[cnt].sz=1,t[cnt].pri=rand();
	return cnt;
}
void pushup(int i)
{
    t[i].sz=t[lson].sz+t[rson].sz+1;
    t[i].hs=t[rson].hs+t[lson].hs*power[t[rson].sz+1]+t[i].val*power[t[rson].sz];
}
int merge(int x,int y)
{
    if(!x||!y) return x+y;
    if(t[x].pri<t[y].pri)
    {
        t[x].ch[1]=merge(t[x].ch[1],y);
        pushup(x);
        return x;
    }
    else
    {
        t[y].ch[0]=merge(x,t[y].ch[0]);
        pushup(y);
        return y;
    }
}
void split(int i,int k,int &x,int &y)
{
    if(!i)
    {
		x=y=0;
		return ;
	}
    if(t[lson].sz>=k)
    {
    	y=i;
		split(lson,k,x,lson);
	}
    else
    {
    	x=i;
		split(rson,k-t[lson].sz-1,rson,y);
	}
    pushup(i);
}
void insert(int x,int val)
{
    int a=0,b=0;
    split(root,x,a,b);
    root=merge(merge(a,newnode(val)),b);
}
void del(int x)
{
    int a=0,b=0,c=0;
    split(root,x,a,c);
    split(a,x-1,a,b);
    b=merge(t[b].ch[0],t[b].ch[1]);
    root=merge(merge(a,b),c);
}
ull query(int l,int r)
{
    int a=0,b=0,c=0;
    split(root,r,a,c);
	split(a,l-1,a,b);
    ull ans=t[b].hs;
    root=merge(merge(a,b),c);
    return ans;
}
bool chk(int x,int y,int mid)
{
    return query(x,x+mid-1)==query(y,y+mid-1);
}
char a[maxn];
signed main()
{
	srand(time(0));
    ios::sync_with_stdio(false);
	cin>>a+1>>m;
	n=strlen(a+1);
	power[0]=1;
	for(int i=1;i<=200000;i++) power[i]=power[i-1]*p;
	for(int i=1;i<=n;i++) root=merge(root,newnode(a[i]-'a'+1));
	string op,d;
	int x,y;
	while(m--)
	{
		cin>>op;
      	if(op[0]=='Q')
      	{
      		cin>>x>>y;
      		int l=0,r=min(n-x+1,n-y+1),ans=0;
      		while(l<=r)
      		{
      			int mid=(l+r)/2;
      			if(chk(x,y,mid)) l=mid+1,ans=mid;
      			else r=mid-1;
            }
           	cout<<ans<<endl;
        }
        else if(op[0]=='R')
        {
            cin>>x>>d;
            del(x);
			insert(x-1,d[0]-'a'+1);
        }
        else
        {
            cin>>x>>d;
            n++;
			insert(x,d[0]-'a'+1);
        }
	}
    return 0;
}
2023/2/2 00:58
加载中...