伸展树uva11996

不知道哪错了,一直超时。。。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define Key_value ch[ch[root][1]][0]
using namespace std;
typedef unsigned LL;
const int maxn=400010;
const int B=123;
char str[maxn];
int N,M;
int ch[maxn][2],pre[maxn],size[maxn],setv[maxn],key[maxn];
LL hash1[maxn],hash2[maxn],xp[maxn];
int root,tot1,tot2;
int len=0;
void NewNode(int &r,int f,int val)
{
    r=++tot1;
    ch[r][0]=ch[r][1]=0;
    pre[r]=f;
    setv[r]=0;
    hash1[r]=hash2[r]=key[r]=val;
    size[r]=1;
}
void pushup(int r)
{
    if(!r)return;
    int lson=ch[r][0],rson=ch[r][1];
    size[r]=size[lson]+size[rson]+1;
    hash1[r]=hash1[rson]+hash1[lson]*xp[size[rson]+1]+key[r]*xp[size[rson]];
    hash2[r]=hash2[lson]+hash2[rson]*xp[size[lson]+1]+key[r]*xp[size[lson]];
}
void build(int &x,int l,int r,int f)
{
    if(l>r)return;
    int mid=(l+r)>>1;
    NewNode(x,f,str[mid]-'0');
    build(ch[x][0],l,mid-1,x);
    build(ch[x][1],mid+1,r,x);
    pushup(x);
}
void init()
{
    root=tot1=tot2=0;
    ch[root][0]=ch[root][1]=size[root]=pre[root]=setv[root]=0;
    hash1[root]=hash2[root]=0;
    NewNode(root,0,0);
    NewNode(ch[root][1],root,0);
    build(Key_value,1,N,ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}
void update_rev(int r)
{
    if(!r)return;
    swap(ch[r][0],ch[r][1]);
    swap(hash1[r],hash2[r]);
    setv[r]^=1;

}
void pushdown(int r)
{
    if(setv[r])
    {
        update_rev(ch[r][0]);
        update_rev(ch[r][1]);
        setv[r]=0;
    }
}
void Rotate(int x,int kind)
{
    int y=pre[x];
    pushdown(y);
    pushdown(x);
    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;
    if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    ch[x][kind]=y;
    pre[y]=x;
    pushup(y);
}
void Splay(int r,int goal)
{
    pushdown(r);
    while(pre[r]!=goal)
    {
        if(pre[pre[r]]==goal)
        {
            pushdown(pre[r]);
            pushdown(r);
            Rotate(r,ch[pre[r]][0]==r);
        }
        else
        {
            pushdown(pre[pre[r]]);
            pushdown(pre[r]);
            pushdown(r);
            int y=pre[r];
            int kind=(ch[pre[y]][0]==y);
            if(ch[y][kind]==r)
            {
                Rotate(r,!kind);
                Rotate(r,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(r,kind);
            }
        }
    }
    pushup(r);
    if(goal==0)root=r;
}
void debug(int r)
{
    if(!r)return;
    debug(ch[r][0]);
    cout<<key[r]<<" "<<hash1[r]<<endl;
    debug(ch[r][1]);
}
int get_kth(int r,int k)
{
    pushdown(r);
    int t=size[ch[r][0]]+1;
    if(t==k)return r;
    if(t>k)return get_kth(ch[r][0],k);
    return get_kth(ch[r][1],k-t);
}
void Insert(int x,int y)
{
    Splay(get_kth(root,x+1),0);
    Splay(get_kth(root,x+2),root);
    NewNode(Key_value,ch[root][1],y);
    pushup(ch[root][1]);
    pushup(root);
    len++;
}
void Remove(int pos)
{
    Splay(get_kth(root,pos),0);
    Splay(get_kth(root,pos+2),root);
    pre[Key_value]=0;
    Key_value=0;
    pushup(ch[root][1]);
    pushup(root);
    len--;
}
void Reverse(int p1,int p2)
{
    Splay(get_kth(root,p1),0);
    Splay(get_kth(root,p2+2),root);
    update_rev(Key_value);
    pushup(ch[root][1]);
    pushup(root);
}
void LCP(int p1,int p2)
{
    int ans=0;
    int l=0,r=size[root]-p2;
    LL tmp1,tmp2;
    while(l+1<r)
    {
        int mid=l+(r-l)/2;
        Splay(get_kth(root,p1),0);
        Splay(get_kth(root,p1+mid+1),root);
        tmp1=hash2[Key_value];
        Splay(get_kth(root,p2),0);
        Splay(get_kth(root,p2+mid+1),root);
        tmp2=hash2[Key_value];
        if(tmp1==tmp2)ans=mid,l=mid;
        else r=mid;
    }
    printf("%d\n",ans);
}
int main()
{freopen("in.txt","r",stdin);
    xp[0]=1;
    for(int i=1;i<maxn;i++)xp[i]=xp[i-1]*B;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        scanf("%s",str+1);
        init();
        len=N;
        int p,c,op;
        while(M--)
        {
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d%d",&p,&c);
                Insert(p,c);
            }
            else if(op==2)
            {
                scanf("%d",&p);
                Remove(p);
            }
            else if(op==3)
            {
                scanf("%d%d",&p,&c);
                Reverse(p,c);
            }
            else
            {
                scanf("%d%d",&p,&c);
                LCP(p,c);
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值