P2486 [SDOI2011] staining (chain split tree)

P2486 [SDOI2011] staining

analysis:

The number of colors used segment tree maintenance segment, recording the leftmost and rightmost color color, every time the combined, if there are overlapping intermediate color, the number of segments will -.

Note that when the chain of the same cross-sectional query endpoint color where the two chains.

Because the chain is a jump alternate process, after the record about to jump dance through which the right and left colors are: ans1 x corresponding to the left, right ANS2 corresponding to y.

Each time when the x and y exchanged depth, will also exchange ans1 with ans2 ensure corresponding relationship.

#include<bits/stdc++.h>
using namespace std;
#define ri register int 
#define N 100005
#define mid ((l+r)>>1)
int Lc,Rc,lc[N*4],rc[N*4],sum[N*4],add[N*4],n;
int fa[N],siz[N],son[N],id[N],dfn[N],cnt=0,dep[N],col[N],topp[N];
vector<int> e[N];
void dfs1(int u,int ff)
{
    siz[u]=1;
    for(ri i=0;i<e[u].size();++i){
        int v=e[u][i];
        if(v==ff) continue; fa[v]=u;
        dep[v]=dep[u]+1; dfs1(v,u); siz[u]+=siz[v];
        if(siz[son[u]]<siz[v]) son[u]=v;
    }
}
void dfs2(int u,int tp)
{
    dfn[++cnt]=u; id[u]=cnt; topp[u]=tp;
    if(son[u]) dfs2(son[u],tp);
    for(ri i=0;i<e[u].size();++i){
        int v=e[u][i];
        if(v==fa[u] || v==son[u]) continue;
        dfs2(v,v);
    }
}
void update(int s)
{
    sum[s]=sum[s<<1]+sum[s<<1|1]-(rc[s<<1]==lc[s<<1|1]);
    lc[s]=lc[s<<1]; rc[s]=rc[s<<1|1];
}
void build(int s,int l,int r)
{
    if(l==r) { sum[s]=1; add[s]=0; lc[s]=rc[s]=col[dfn[l]]; return ; }
    build(s<<1,l,mid); build(s<<1|1,mid+1,r);
    update(s);
}
void pushdown(int s)
{
    if(!add[s]) return ;
    int v=add[s];
    add[s<<1]=add[s<<1|1]=lc[s<<1]=lc[s<<1|1]=rc[s<<1]=rc[s<<1|1]=v;
    sum[s<<1]=sum[s<<1|1]=1;
    add[s]=0;
}
void modify(int s,int l,int r,int L,int R,int v)
{
    if(L<=l && r<=R) { add[s]=v; sum[s]=1; lc[s]=rc[s]=v; return;  }
    pushdown(s);
    if(L<=mid) modify(s<<1,l,mid,L,R,v);
    if(R>mid)  modify(s<<1|1,mid+1,r,L,R,v);
    update(s);
}
int query(int s,int l,int r,int L,int R)
{
    if(l==L) Lc=lc[s]; if(r==R) Rc=rc[s];
    if(L<=l && r<=R) return sum[s];
    pushdown(s);
    int ans=0,fl=0;
    if(L<=mid) ans+=query(s<<1,l,mid,L,R),fl++;
    if(R>mid)  ans+=query(s<<1|1,mid+1,r,L,R),fl++;
    if(fl==2) ans-=(rc[s<<1]==lc[s<<1|1]);
    return ans;
}
void modify_link(int x,int y,int c)
{
    while(topp[x]!=topp[y]){
        if(dep[topp[x]]<dep[topp[y]]) swap(x,y);
        modify(1,1,n,id[topp[x]],id[x],c);
        x=fa[topp[x]];
    }
    if(dep[x]<DEP [Y]) the swap (X, Y); 
    Modify ( . 1 , . 1 , n-, ID [Y], ID [X], C); 
} 
int query_link ( int X, int Y) 
{ 
    int ANS = 0 , ANS1 = - . 1 , ANS2 = - . 1 ;
     the while (Topp [X] =! Topp [Y]) {
         IF (DEP [Topp [X]] <DEP [Topp [Y]]) the swap (X, Y), the swap ( ANS1, ANS2); // X corresponds ans1 y corresponds ANS2 
        ANS + = Query ( . 1 , . 1 , n-, ID [Topp [X]], ID [X]);
         IF (Rc of ANS1 ==) ans--; // Rc is recorded this time if the lower end of the chain and the upper end of the primary colors on the same - 
        ANS1 = Lc of; X =FA [Topp [x]]; 
    } 
    IF (DEP [x]> DEP [Y]) the swap (x, Y), the swap (ANS1, ANS2); // !! ANS1 and corresponding to x must be so that if the following with ANS1 use it to ensure that the comparison Lc x above 
    ANS + = Query ( . 1 , . 1 , n-, ID [x], ID [Y]);
     IF (ANS1 == Lc) ans-- ;
     IF (Rc of ANS2 == ) ans-- ;
     return ANS; 
} 
int main () 
{ 
    int A, B, C, m; 
    Scanf ( " % D% D " , & n-, & m);
     for (I = RI . 1 ; I <= n-; I ++) Scanf ( " % D " , & COL [I]);
     for(ri i=1;i<=n-1;++i) scanf("%d%d",&a,&b),e[a].push_back(b),e[b].push_back(a);
    dep[1]=1; dfs1(1,0); dfs2(1,1);
    build(1,1,n);
    char op[2]; 
    for(ri i=1;i<=m;++i){
        scanf("%s%d%d",op,&a,&b);
        if(op[0]=='C') scanf("%d",&c),modify_link(a,b,c);
        else printf("%d\n",query_link(a,b));
    }
    return 0;
}
/*
6 100
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 4
Q 1 5
Q 6 5
Q 6 4
C 1 4 1
Q 6 4


Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5
*/
View Code

 

Guess you like

Origin www.cnblogs.com/mowanying/p/11823448.html