Alphadog 回文树+lct

题意

这里写图片描述
这里写图片描述
sig=1表示强制在线。

分析

真是一道神题。

首先可以对该字符串建立回文树,那么两个前缀的lcp就是其分别对应回文树上的节点的lca。那么问题就变成了动态维护每个前缀在回文树上对应的点两两之间lca的长度和。
设len[i]表示回文树第i个节点对应回文串的长度,size[i]表示有多少个前缀对应这个节点。那么答案就是 szi=1szj=isize[i]size[j]len[lca(i,j)]
由于要动态维护,我们考虑维护每加入一个点后,新增加的对答案的贡献。
神奇的操作就在这里:
若我们把每个点i的权值设为len[i]-len[fa[i]],那么新增加节点x后对答案的贡献就等于先把x到根的路径上每个点的size都+1,然后 delta=uxsize[u]val[u]
为什么这样是对的呢?考虑新增点x与某个点y的贡献,当走到lca(x,y)时,被算进去的是len[lca]-len[fa[lca]],而在从lca不停往上算的过程中,我们惊奇地发现除了len[lca]以外的项全部都被抵消掉了。所以对答案的贡献实际就是len[lca]。
我们可以用lct来维护这个过程。每次access(x),通过splay上打标记来实现size+1,然后顺便维护答案即可。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;

typedef long long LL;

const int N=100005;

int n,s[N],sig;
struct tree{int l,r,val,size,fa,rev,tag;LL tot,ans;}t[N];
stack<int> sta;

bool is_root(int x)
{
    return x!=t[t[x].fa].l&&x!=t[t[x].fa].r||!t[x].fa;
}

void updata(int x)
{
    t[x].tot=t[t[x].l].tot+t[t[x].r].tot+t[x].val;
    t[x].ans=t[t[x].l].ans+t[t[x].r].ans+(LL)t[x].size*t[x].val;
}

void pushdown(int x)
{
    if (t[x].rev)
    {
        t[x].rev^=1;swap(t[x].l,t[x].r);
        if (t[x].l) t[t[x].l].rev^=1;
        if (t[x].r) t[t[x].r].rev^=1;
    }
    if (t[x].tag)
    {
        int w=t[x].tag;t[x].tag=0;
        if (t[x].l) t[t[x].l].tag+=w,t[t[x].l].size+=w,t[t[x].l].ans+=(LL)w*t[t[x].l].tot;
        if (t[x].r) t[t[x].r].tag+=w,t[t[x].r].size+=w,t[t[x].r].ans+=(LL)w*t[t[x].r].tot;
    }
}

void rttr(int x)
{
    int y=t[x].l;
    t[x].l=t[y].r;t[t[y].r].fa=x;
    if (x==t[t[x].fa].l) t[t[x].fa].l=y;
    else if (x==t[t[x].fa].r) t[t[x].fa].r=y;
    t[y].fa=t[x].fa;
    t[y].r=x;t[x].fa=y;
    updata(x);updata(y);
}

void rttl(int x)
{
    int y=t[x].r;
    t[x].r=t[y].l;t[t[y].l].fa=x;
    if (x==t[t[x].fa].l) t[t[x].fa].l=y;
    else if (x==t[t[x].fa].r) t[t[x].fa].r=y;
    t[y].fa=t[x].fa;
    t[y].l=x;t[x].fa=y;
    updata(x);updata(y);
}

void remove(int x)
{
    while (!is_root(x)) sta.push(x),x=t[x].fa;
    pushdown(x);
    while (!sta.empty()) x=sta.top(),sta.pop(),pushdown(x);
}

void splay(int x)
{
    remove(x);
    while (!is_root(x))
    {
        int p=t[x].fa,g=t[p].fa;
        if (is_root(p))
        {
            if (x==t[p].l) rttr(p);
            else rttl(p);
            return;
        }
        if (x==t[p].l)
            if (p==t[g].l) rttr(g),rttr(p);
            else rttr(p),rttl(g);
        else
            if (p==t[g].r) rttl(g),rttl(p);
            else rttl(p),rttr(g);
    }
}

void access(int x)
{
    int y=0;
    while (x)
    {
        splay(x);t[x].r=y;
        updata(x);
        y=x;x=t[x].fa;
    }
}

void make_root(int x)
{
    access(x);splay(x);t[x].rev^=1;
}

void link(int x,int y)
{
    make_root(y);splay(y);t[y].fa=x;
}

struct pam
{
    int fail[N],len[N],ch[N][26],cnt,last;

    pam()
    {
        len[1]=-1;fail[0]=fail[1]=1;cnt=1;
        link(1,2);
    }

    void ins(int c,int n)
    {
        int p=last;
        while (s[n-1-len[p]]!=s[n]) p=fail[p];
        if (!ch[p][c])
        {
            int now=++cnt,k=fail[p];len[now]=len[p]+2;
            while (s[n-1-len[k]]!=s[n]) k=fail[k];
            fail[now]=ch[k][c];ch[p][c]=now;
            t[now+1].val=len[now]-max(len[fail[now]],0);link(now+1,fail[now]+1);
        }
        last=ch[p][c];
    }

    LL solve()
    {
        int x=last+1;
        make_root(2);access(x);splay(x);t[x].tag++;t[x].size++;t[x].ans+=t[x].tot;
        return t[x].ans;
    }
}pam;

int main()
{
    scanf("%d%d",&n,&sig);
    LL ans=0;s[0]=-1;
    for (int i=1;i<=n;i++)
    {
        LL x;
        scanf("%lld",&x);
        if (sig) x^=ans;
        s[i]=x;
        pam.ins(s[i],i);
        ans+=pam.solve();
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值