bzoj 2243 [SDOI2011]染色(树链剖分,线段树)

2243: [SDOI2011]染色

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 4637  Solved: 1726
[Submit][Status][Discuss]

Description

给定一棵有n个节点的无根树和m个操作,操作有2类:

1、将节点a到节点b路径上所有点都染成颜色c;

2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。

请你写一个程序依次完成这m个操作。

Input

第一行包含2个整数n和m,分别表示节点数和操作数;

第二行包含n个正整数表示n个节点的初始颜色

下面行每行包含两个整数x和y,表示xy之间有一条无向边。

下面行每行描述一个操作:

“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;

“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。

Output

对于每个询问操作,输出一行答案。

Sample Input

6 5

2 2 1 2 1 1

1 2

1 3

2 4

2 5

2 6

Q 3 5

C 2 1 1

Q 3 5

C 5 1 2

Q 3 5

Sample Output

3

1

2

HINT

数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。

Source

【思路】

树链剖分,线段树

线段树维护lc rc c s分别表示线段的左右端颜色 update设置颜色 线段所含颜色数,需要注意的是线段合并以及统计信息时候需要考虑两线段接点是否重色。

Ps:dep比较的是top,T-T    节点信息加上lr倒是个不错的写法 :)

【代码】

 #include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int N = 1e5+1e5+; struct Node {
int l,r,lc,rc,c,s;
}T[N<<];
vector<int> G[N];
int n,m,z,c[N];
char s[]; int w[N],top[N],son[N],siz[N],fa[N],dep[N]; void dfs1(int u) {
siz[u]=; son[u]=;
for(int i=;i<G[u].size();i++) {
int v=G[u][i];
if(v!=fa[u]) {
fa[v]=u; dep[v]=dep[u]+;
dfs1(v);
if(siz[v]>siz[son[u]]) son[u]=v;
siz[u]+=siz[v];
}
}
}
void dfs2(int u,int tp) {
w[u]=++z; top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(int i=;i<G[u].size();i++) {
int v=G[u][i];
if(v!=son[u] && v!=fa[u]) dfs2(v,v);
}
}
void pushdown(int u) {
if(T[u].c!=- && T[u].l<T[u].r) {
int lc=u<< , rc=lc|;
T[lc].c=T[rc].c=T[u].c;
T[lc].lc=T[lc].rc=T[rc].lc=T[rc].rc=T[u].c;
T[lc].s=T[rc].s=;
T[u].c=-;
}
}
void maintain(int u) {
int lc=u<<,rc=lc|;
T[u].lc=T[lc].lc , T[u].rc=T[rc].rc;
T[u].s=T[lc].s+T[rc].s-(T[lc].rc==T[rc].lc);
}
void build(int u,int L,int R) {
T[u].l=L , T[u].r=R , T[u].c=- , T[u].s=;
if(L==R) return ;
int M=(L+R)>>;
build(u<<,L,M) , build(u<<|,M+,R);
}
void update(int u,int L,int R,int x) {
pushdown(u);
if(L==T[u].l && R==T[u].r) { //CC
T[u].c=T[u].lc=T[u].rc=x;
T[u].s=; return ;
}
int M=(T[u].l+T[u].r)>> , lc=u<< , rc=lc|;
if(R<=M) update(lc,L,R,x); //can change
else if(L>M) update(rc,L,R,x);
else
update(lc,L,M,x) , update(rc,M+,R,x);
maintain(u);
}
int query(int u,int L,int R) {
pushdown(u);
if(L==T[u].l && R==T[u].r) return T[u].s;
int M=(T[u].l+T[u].r)>> , lc=u<< , rc=lc|;
if(R<=M) return query(lc,L,R);
else if(L>M) return query(rc,L,R);
else
return query(lc,L,M)+query(rc,M+,R)-(T[lc].rc==T[rc].lc);
}
int find(int u,int r) {
pushdown(u);
if(T[u].l==T[u].r) return T[u].lc;
int M=(T[u].l+T[u].r)>>;
return r<=M? find(u<<,r):find(u<<|,r);
}
void modify(int u,int v,int x) {
while(top[u]!=top[v]) {
if(dep[top[u]]<dep[top[v]]) swap(u,v);
update(,w[top[u]],w[u],x);
u=fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
update(,w[u],w[v],x);
}
int ask(int u,int v) {
int ans=;
while(top[u]!=top[v]) {
if(dep[top[u]]<dep[top[v]]) swap(u,v);
ans+=query(,w[top[u]],w[u]);
if(find(,w[fa[top[u]]])==find(,w[top[u]])) ans--;
u=fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
ans+=query(,w[u],w[v]);
return ans;
} void read(int& x) {
char c=getchar();
while(!isdigit(c)) c=getchar();
x=;
while(isdigit(c))
x=x*+c-'' , c=getchar();
}
int main() {
read(n) , read(m);
FOR(i,,n) read(c[i]);
int u,v,k;
FOR(i,,n-) {
read(u) , read(v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs1() , dfs2(,);
build(,,n);
FOR(i,,n) update(,w[i],w[i],c[i]);
while(m--) {
scanf("%s",s);
read(u) , read(v);
if(s[]=='C')
read(k) , modify(u,v,k);
else
printf("%d\n",ask(u,v));
}
return ;
}
上一篇:bzoj 4034 [HAOI2015] T2(树链剖分,线段树)


下一篇:HDU 4366 Successor(树链剖分+zkw线段树+扫描线)