题解 惊惶的SCOI2016

· · 题解

第一个自己 YY 出来的黑 Ynoi,写一篇题解纪念一下。

首先考虑转化这个柿子。

类似于 Ynoi2016 谁的梦,对每一种颜色采用正难则反——考虑不包含这个颜色的路径条数。

经过这一步转化之后,答案是显然的——只保留不等于这一种颜色的点,所有树上连通块大小的平方和。

树上联通块的信息维护,不难想到 Qtree6/7 的套路——对每一种颜色开一个 LCT 维护子树信息,将点的颜色挂在父亲边上,使得一个连通块的根可能异色,查询信息的时候在这个连通块的根上查询;转色就在原先颜色的 LCT 上 cut,变为的颜色的 LCT 上 link。

但是会出现一个问题——这里一共 n 种颜色,总不可能开 n 颗 LCT 吧……

解决方案是:因为经过转化之后,显然每一种颜色对于答案的贡献是独立的,所以我们可以对每一种颜色,只处理操作序列中与这个颜色有关的操作,然后更新这一个颜色的答案。

这样总的转色次数可以保证为 2n+2m,同时带来的好处是——现在只剩两种颜色了,等于这次选择的颜色(白),以及不等于(黑),两颗 LCT 就可以解决问题。

考虑一下需要维护什么信息。

子树大小,虚子树大小必然要维护。

考虑在白转黑/黑转白中,可能会出现一个点转色之后,联通/分隔了它所有儿子构成的连通块。此时答案需要快速更新——我们维护一个点所有虚儿子的子树大小的平方和就行了。

这所有东西的维护只要做过 BJOI2014 大融合 应该都会,此处不提。

那么这题就做完了,总复杂度 O((n+m)\log n)。

注意的几点:

代码:因为这题太冷清,所以目前暂时是 Rank1。

//El Psy Kongroo
#include <cstdio>
typedef long long LL;
const int maxn = 4e5+5;
const int Mxdt = (1<<16);
inline char pc(char ch,bool bj) {
    static char buf[Mxdt],*p1=buf,*p2=buf+Mxdt;
    return ((bj)||(*p1++=ch)&&p1==p2)&&fwrite(p1=buf,1,p1-buf,stdout),0;
}
void print(LL x) {
    if(x>9)print(x/10);
    pc(x%10^48,false);
}
inline void printnum(LL x,int ch) {
    if(x < 0)pc('-',false),x=-x;
    print(x),pc(ch,false);
}
char gc(){
    static char buf[1<<16],*S,*T;
    if(T==S){
        T=(S=buf)+fread(buf,1,1<<16,stdin);
        if(T==S) return EOF;
    }
    return *S++;
}
#define getchar gc
inline int re() {
    char c;
    while((c=getchar())<'0'||c>'9');
    int res = c-'0';
    while((c=getchar())>='0'&&c<='9')res = res*10+c-'0';
    return res;
}
struct pair {int x,y;};
int n,m,col[maxn],fth[maxn],head[maxn],tot;
struct Edge {
    int next,to;
} e[maxn<<1];
void add_edge(int x,int y){e[++tot].next = head[x],e[tot].to = y,head[x] = tot;}
void getfa(int u,int F) {fth[u] = F;for(int i=head[u],v; v=e[i].to,i; i=e[i].next)if(v != F)getfa(v,u);}
struct Link_Cut_Tree {
    //\sigma{white cluster siz^2}
    int col[maxn];
    LL fsizsq[maxn],Ans;
    int fa[maxn],ch[maxn][2],siz[maxn],fsiz[maxn];
    LL sq(int x) {return 1ll*x*x;}
    void psp(int x) {siz[x] = fsiz[x] + siz[ch[x][0]] + siz[ch[x][1]] + 1;}
    bool iden(int x) {return ch[fa[x]][1] == x;}
    bool nrt(int x) {return ch[fa[x]][0] == x || ch[fa[x]][1] == x;}
    void cnct(int x,int F,bool sid) {ch[fa[x] = F][sid] = x;}
    void rot(int x) {
        int y = fa[x],z = fa[y],xy = iden(x);
        if(nrt(y))ch[z][iden(y)] = x;
        cnct(ch[x][xy^1],y,xy),cnct(y,x,xy^1),fa[x] = z,psp(y);
    }
    void Splay(int x) {
        while(nrt(x)) {
            int y = fa[x];
            if(nrt(y))iden(x)^iden(y)?rot(x):rot(y);
            rot(x);
        }
        psp(x);
    }
    void access(int x) {
        for(int y=0; x; x=fa[y=x]) {
            Splay(x);
            fsiz[x] += siz[ch[x][1]]-siz[y],fsizsq[x] += sq(siz[ch[x][1]])-sq(siz[y]);
            ch[x][1] = y,psp(x);
        }
    }
    void evert(int x) {access(x),Splay(x);}
    void setblack(int x) {
        if(x == 1)return evert(x),col[x] = 0,Ans += fsizsq[x],Ans -= sq(siz[x]),void();
        access(x),Splay(fth[x]);
        col[x] = 0,Ans += fsizsq[x];
        if(col[fth[x]] == 1) {
            int pos = fth[x],pr;
            while(ch[pos][0])pos = ch[pos][0];
            if(pos == 1 && col[1])Ans -= sq(pr=siz[fth[x]]),Splay(pos);
            else Splay(pos),Ans -= sq(pr=siz[ch[pos][1]]);
            access(x),Splay(fth[x]),ch[fth[x]][1] = fa[x] = 0,siz[fth[x]] -= siz[x];
            Ans += sq(pr-siz[x]);
        } else ch[fth[x]][1] = fa[x] = 0,siz[fth[x]] -= siz[x],Ans -= sq(siz[x]);
    }
    void setwhite(int x) {
        if(x == 1)return evert(x),col[x] = 1,Ans -= fsizsq[x],Ans += sq(siz[x]),void();
        evert(x),evert(fth[x]);
        col[x] = 1,Ans -= fsizsq[x]+sq(siz[ch[x][1]]);
        if(col[fth[x]] == 1) {
            int pos = fth[x],pr;
            while(ch[pos][0])pos = ch[pos][0];
            if(pos == 1 && col[1])Ans -= sq(pr=siz[fth[x]]),Splay(pos);
            else Splay(pos),Ans -= sq(pr=siz[ch[pos][1]]);
            Splay(fth[x]),fa[x] = fth[x],fsizsq[fth[x]] += sq(siz[x]),fsiz[fth[x]] += siz[x],siz[fth[x]] += siz[x];
            Ans += sq(pr+siz[x]);
        } else fa[x] = fth[x],fsizsq[fth[x]] += sq(siz[x]),fsiz[fth[x]] += siz[x],siz[fth[x]] += siz[x],Ans += sq(siz[x]);
    }
} LCT;
int frt[maxn],ed[maxn],nod;
struct Op {
    int tim,x,ty,next;
} op[maxn<<2];
void Add(int x,int t,int y,int ty) {
    if(!frt[x])frt[x] = ed[x] = ++nod,op[nod] = {t,y,ty,0};
    else op[ed[x]].next = ++nod,ed[x] = nod,op[nod] = {t,y,ty,0};
}
LL ans[maxn];
int main() {
    n = re(),m = re();
    for(int i=1; i<=n; ++i)col[i] = re(),Add(col[i],0,i,-1);
    for(int i=1,x,y; i<n; ++i)x = re(),y = re(),add_edge(x,y),add_edge(y,x);
    getfa(1,0);
    for(int i=1,x,y; i<=m; ++i) x = re(),y = re(),Add(col[x],i,x,1),Add(col[x]=y,i,x,-1);
    for(int i=1; i<=n; ++i)Add(col[i],m+1,i,1);
    for(int i=1; i<=n; ++i)LCT.siz[i] = 1;
    for(int i=1; i<=n; ++i)LCT.setwhite(i);
    for(int i=1; i<=n; ++i) {
        for(int j=frt[i]; j;) {
            LL preans = LCT.Ans;
            int tim = op[j].tim;
            while(j && op[j].tim == tim) {op[j].ty==1?LCT.setwhite(op[j].x):LCT.setblack(op[j].x);j = op[j].next;}
            ans[tim] += LCT.Ans-preans;
        }
    }
    for(int i=1; i<=m; ++i)ans[i] += ans[i-1];
    for(int i=0; i<=m; ++i)printnum(-ans[i],'\n');
    return pc('♂',1);
}