HYSBZ 2243

时间:2023-03-10 02:26:21
HYSBZ 2243
//Accepted    18440 KB    5556 ms
/*
    source:HYSBZ 2243
    time  :2015.5.29
    by    :songt
  */
/*题解:
树链剖分

  */

#include <cstdio>
#include <cstring>

;
struct Edge
{
    int u,v;
    Edge(){}
    Edge(int u,int v):u(u),v(v){}
}edge[*imax_n];
int head[imax_n];
*imax_n];
int tot;
int top[imax_n];
int son[imax_n];
int num[imax_n];
int p[imax_n];
int fp[imax_n];
int deep[imax_n];
int fa[imax_n];
int pos;

void init()
{
    memset(head,-,sizeof(head));
    memset(next,-,sizeof(next));
    tot=;
    memset(son,-,sizeof(son));
    pos=;
}

void addEdge(int u,int v)
{
    edge[tot]=Edge(u,v);
    next[tot]=head[u];
    head[u]=tot++;
}

void dfs1(int u)
{
    num[u]=;
    ;i=next[i])
    {
        int v=edge[i].v;
        if (v!=fa[u])
        {
            fa[v]=u;
            deep[v]=deep[u]+;
            dfs1(v);
            num[u]+=num[v];
             || num[son[u]]<num[v])
                son[u]=v;
        }
    }
}

void dfs2(int u)
{
    p[u]=pos++;
    fp[p[u]]=u;
    ) return ;
    top[son[u]]=top[u];
    dfs2(son[u]);
    ;i=next[i])
    {
        int v=edge[i].v;
        if (v!=son[u] && v!=fa[u])
        {
            dfs2(top[v]=v);
        }
    }
}

struct Tree
{
    int l,r;
    int lc,rc;
    int num;
    int same;
    int color;
}f[imax_n*];
int color[imax_n];

void push_down(int t)
{
    f[*t].same=f[*t+].same=;
    f[*t].color=f[*t+].color=f[t].color;
    f[*t].num=f[*t+].num=;
    f[*t].lc=f[*t].rc=f[t].color;
    f[*t+].lc=f[*t+].rc=f[t].color;
    f[t].same=;
}

void push_up(int t)
{
    f[t].num=f[*t].num+f[*t+].num-(f[*t].rc==f[*t+].lc);
    f[t].lc=f[*t].lc;
    f[t].rc=f[*t+].rc;
}

void build(int t,int l,int r)
{
    f[t].l=l;
    f[t].r=r;
    f[t].same=;
    if (l==r)
    {
        f[t].lc=f[t].rc=color[fp[l]];
        f[t].color=color[fp[l]];
        f[t].num=;
        f[t].same=;
        return ;
    }
    ;
    build(*t,l,mid);
    build(*t+,mid+,r);
    push_up(t);
}

void update(int t,int l,int r,int color)
{
    if (f[t].l==l && f[t].r==r)
    {
        f[t].color=color;
        f[t].num=;
        f[t].lc=f[t].rc=color;
        f[t].same=;
        return ;
    }
    if (f[t].same) push_down(t);
    ;
    *t,l,r,color);
    else
    {
        *t+,l,r,color);
        else
        {
            update(*t,l,mid,color);
            update(*t+,mid+,r,color);
        }
    }
    push_up(t);
}

int query(int t,int l,int r,int &cl,int &rl)
{
    if (f[t].l==l && f[t].r==r)
    {
        cl=f[t].lc;
        rl=f[t].rc;
        return f[t].num;
    }
    if (f[t].same) push_down(t);
    ;
    *t,l,r,cl,rl);
    else
    {
        *t+,l,r,cl,rl);
        else
        {
            int lcl,lcr,rcl,rcr;
            int numl,numr;
            numl=query(*t,l,mid,lcl,lcr);
            numr=query(*t+,mid+,r,rcl,rcr);
            cl=lcl;
            rl=rcr;
            return numl+numr-(lcr==rcl);
        }
    }
}

void swap(int &a,int &b)
{
    int t=a;
    a=b;
    b=t;
}

void OpC(int u,int v,int c)
{
    int f1=top[u],f2=top[v];
    while (f1!=f2)
    {
        //printf("u=%d v=%d top[u]=%d top[v]=%d\n",u,v,f1,f2);
        if (deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        update(,p[f1],p[u],c);
        //printf("update %d %d\n",f1,u);
        u=fa[f1];
        f1=top[u];
    }
    if (deep[u]>deep[v]) swap(u,v);
    update(,p[u],p[v],c);
    //printf("update %d %d\n",u,v);
}

int OpQ(int u,int v)
{
    int f1=top[u],f2=top[v];
    ,cv=-;
    ;
    int lc,rc;
    while (f1!=f2)
    {
        if (deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
            swap(cu,cv);
        }
        ,p[f1],p[u],lc,rc);
        //printf("%d %d num=%d lc=%d rc=%d\n",f1,u,tmp,lc,rc);
        ans+=tmp;
        ans-=(cu==rc);
        cu=lc;
        u=fa[f1];
        f1=top[u];
    }
    if (deep[u]>deep[v])
    {
        swap(u,v);
        swap(cu,cv);
    }
    ,p[u],p[v],lc,rc);
    //printf("%d %d num=%d lc=%d rc=%d\n",u,v,tmp,lc,rc);
    ans+=query(,p[u],p[v],lc,rc);
    ans-=(cu==lc);
    ans-=(cv==rc);
    return ans;
}

int n,m;
];
int u,v,c;

int main()
{
    //while (scanf("%d%d",&n,&m)==2)
    scanf("%d%d",&n,&m);
    {
        init();
        ;i<=n;i++)
        {
            scanf("%d",&color[i]);
        }
        ;i<n-;i++)
        {
            scanf("%d%d",&u,&v);
            addEdge(u,v);
            addEdge(v,u);
        }
        fa[]=;
        deep[]=;
        dfs1();
        dfs2(top[]=);
        build(,,pos-);
        ;i<m;i++)
        {
            scanf("%s",op);
            ]=='Q')
            {
                scanf("%d%d",&u,&v);
                printf("%d\n",OpQ(u,v));
            }
            else
            {
                scanf("%d%d%d",&u,&v,&c);
                OpC(u,v,c);
            }
        }
    }
    ;
}