当先锋百科网

首页 1 2 3 4 5 6 7

树剖裸题,复习一波。。一开始还看错题目了以为要维护一段区间的颜色数量。。还以为要上主席树。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=e5+;
int n,m,cnt;
int val[N],head[N],next[N],go[N],fa[N],pos[N];
int a[N],tot;
int siz[N],son[N],top[N],dep[N];
struct node
{
    int l,r,sum,lc,rc,lazy;
}t[N*10];
inline void add(int x,int y)
{
    go[++tot]=y;
    next[tot]=head[x];
    head[x]=tot;
}
inline void dfs1(int x,int f)
{
    siz[x]=;
    for(int i=head[x];i;i=next[i])
    {
        int v=go[i];
        if (v!=f)
        {
            dep[v]=dep[x]+;
            fa[v]=x;
            dfs1(v,x);
            siz[x]+=siz[v];
            if (!son[x]||siz[son[x]]<siz[v])
            son[x]=v;
        }
    }
}
int tim;
inline void dfs2(int x,int chain)
{
    int k=;
    pos[x]=++tim;
    top[x]=chain;
    for(int i=head[x];i;i=next[i])
    {
        int v=go[i];
        if (v!=fa[x])
        {
            if (siz[v]>siz[k])
            k=v;
        }
    }
    if (!k)return ;
    dfs2(k,chain);
    for(int i=head[x];i;i=next[i])
    {
        int v=go[i];
        if (v!=fa[x]&&v!=k)
        dfs2(v,v);
    }
}
inline void build(int x,int l,int r)
{
    t[x].l=l,t[x].r=r;
    if (l==r)return ;
    int mid=(l+r)>>;
    build(x*2,l,mid);
    build(x*2+,mid+,r);
}
inline void pushlazy(int x)
{
    if (t[x].lazy==||t[x].l==t[x].r)return;
    int v=t[x].lazy;
    t[x].lazy=;
    t[x<<].sum=t[x<<|].sum=;
    t[x<<].lc=t[x<<].rc=t[x<<|].lc=t[x<<|].rc=t[x<<].lazy=t[x<<|].lazy=v;
}
inline void queryc(int x,int l,int r,int v)
{
    if (t[x].l==l&&t[x].r==r)
    {
        t[x].sum=;
        t[x].lc=t[x].rc=t[x].lazy=v;
        return;
    }
    pushlazy(x);
    int mid=(t[x].l+t[x].r)>>;
    if (r<=mid)queryc(x<<,l,r,v);
    else if (l>mid)queryc(x<<|,l,r,v);
    else
    {
        queryc(x<<,l,mid,v);
        queryc(x<<|,mid+,r,v);
    }
    t[x].lc=t[x<<].lc;
    t[x].rc=t[x<<|].rc;
    t[x].sum=t[x<<].sum+t[x<<|].sum;
    if (t[x<<].rc==t[x<<|].lc)t[x].sum--;
}
inline void solvec(int x,int y,int z)
{
    while (top[x]!=top[y])
    {
        if (dep[top[x]]<dep[top[y]])swap(x,y);
        queryc(,pos[top[x]],pos[x],z);
        x=fa[top[x]];
    }
    if (dep[x]>dep[y])swap(x,y);
    queryc(,pos[x],pos[y],z);
}
inline int queryq(int x,int l,int r)
{
    pushlazy(x);
    if (t[x].l==l&&t[x].r==r)return t[x].sum;
    int mid=(t[x].l+t[x].r)>>;
    if (r<=mid)return queryq(x<<1,l,r);
    else if (l>mid)return queryq(x<<1|1,l,r);
    else
    {
        int sum=queryq(x<<1,l,mid)+queryq(x<<1|1,mid+1,r);
        if (t[x<<].rc==t[x<<|].lc)sum--;
        return sum;
    }
}
inline int find(int x,int pos)
{
    pushlazy(x);
    if (t[x].sum==)return t[x].lc;
    int mid=(t[x].l+t[x].r)>>;
    if (pos<=mid)return find(x<<,pos);
    else find(x<<|,pos);
}
inline int solveq(int x,int y)
{
    int sum=;
    while (top[x]!=top[y])
    {
        if (dep[top[x]]<dep[top[y]])swap(x,y);
        sum+=queryq(1,pos[top[x]],pos[x]);
        if (find(,pos[top[x]])==find(,pos[fa[top[x]]]))sum--;//注意合并的时候相邻颜色相同sum- 
        x=fa[top[x]];
    }
        if (dep[x]>dep[y])swap(x,y);
    sum+=queryq(1,pos[x],pos[y]);
    return sum;
}
int main()
{
    scanf("%d%d",&n,&m);
    fo(i,,n)scanf("%d",&a[i]);
    fo(i,,n-)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dep[]=;
    dfs1(,);
    dfs2(,);
    build(,,n);
    char ch[];
    fo(i,,n)
    queryc(,pos[i],pos[i],a[i]);
    fo(i,,m)
    {
        scanf("%s",ch);
        if (ch[]=='C')
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            solvec(x,y,z);
        }
        else
        {
            int x,y;
            scanf("%d%d",&x,&y);
            printf("%d\n",solveq(x,y));
        }
    }
}