2243: [SDOI2011]染色
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 8002 Solved: 3005
[ 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,表示x和y之间有一条无向边。
下面 行每行描述一个操作:
“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
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
1
2
HINT
数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。
昨天听取了WA声一片啊,WA了一发又一发气死,刚学树剖还不够熟练啊。
昨天状态不是很好调了一晚上没有调出来,今天感觉昨天改的代码太丑了,就重敲了一发,后来发现是死在了运算符上。
重载之后的"+"前加和后加是有区别的,今早才发现,然后就愉快的A了,运算符重载是在QAQ大佬那学来的,刚开始用,所以不够熟练,这个坑,除了我没几个人会踩吧,也是没谁了,分享下AC代码。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+10;
int N,M,edge_cnt,tot;
int value[maxn],head[maxn];
struct Edge{
int to,next;
}edge[maxn<<1];
void ADD(int u,int v){
edge[edge_cnt].to = v;
edge[edge_cnt].next = head[u];
head[u] = edge_cnt++;
}
int size[maxn],son[maxn],fa[maxn],deep[maxn];
void dfs1(int now,int father,int Deep){
size[now] = 1;
fa[now] = father;
deep[now] = Deep;
son[now] = 0;
for(int i = head[now]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v != father){
dfs1(v,now,Deep+1);
size[now] += size[v];
if(size[son[now]] < size[v])
son[now] = v;
}
}
}
int ID[maxn],Rank[maxn],top[maxn];
void dfs2(int now,int Top){
top[now] = Top;
ID[now] = ++tot;
Rank[ID[now]] = now;
if(son[now])
dfs2(son[now],Top);
for(int i = head[now]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v != fa[now] && v!= son[now]){
dfs2(v,v);
}
}
}
struct Info{
int l,r;
int siz,lazy;
int lcolor,rcolor;
Info(){};
Info(int l1,int r1,int siz1,int lcolor1,int rcolor1){
l = l1;
r = r1;
siz = siz1;
lcolor = lcolor1;
rcolor = rcolor1;
lazy = -1;
}
void Init(int val){
siz = 1;
lcolor = rcolor = val;
}
void clear(){
siz = -1;
}
};
Info rev(Info v){
return Info(v.l,v.r,v.siz,v.rcolor,v.lcolor);
}
Info operator + (Info a, Info b){
if(b.siz == -1)return a;
if(a.siz == -1)return b;
Info ret(a.l,b.r,a.siz+b.siz,a.lcolor,b.rcolor);
if(a.rcolor == b.lcolor)
ret.siz--;
return ret;
}
struct SGT{
Info tree[maxn<<2];
void pushDown(int now){
int temp = tree[now].lazy;
tree[now<<1].Init(temp);
tree[now<<1|1].Init(temp);
tree[now<<1].lazy = tree[now<<1|1].lazy = temp;
tree[now].lazy = -1;
}
void build(int now,int l,int r){
tree[now].l = l;
tree[now].r = r;
tree[now].lazy = -1;
if(l == r){
tree[now].Init(value[Rank[l]]);
return;
}
int Mid = (l+r)>>1;
build(now<<1,l,Mid);
build(now<<1|1,Mid+1,r);
tree[now] = tree[now<<1]+tree[now<<1|1];
}
void update(int now,int l,int r,int val){
if(l <= tree[now].l && tree[now].r <= r){
tree[now].Init(val);
tree[now].lazy = val;
return;
}
if(tree[now].lazy != -1)
pushDown(now);
int Mid = (tree[now].l + tree[now].r)>>1;
if(l <= Mid)
update(now<<1,l,r,val);
if(Mid < r)
update(now<<1|1,l,r,val);
tree[now] = tree[now<<1] + tree[now<<1|1];
}
Info query(int now,int l,int r){
if(l <= tree[now].l && tree[now].r <= r){
return tree[now];
}
if(tree[now].lazy != -1)
pushDown(now);
int Mid = (tree[now].l + tree[now].r)>>1;
Info ret;
ret.clear();
if(l <= Mid)
ret = query(now<<1,l,r) + ret;
if(Mid < r)
ret = ret + query(now<<1|1,l,r);
return ret;
}
};
SGT sgt;
void work_update(int u,int v,int val){
int tpu = top[u];
int tpv = top[v];
while(tpu != tpv){
if(deep[tpu] < deep[tpv]){
swap(u,v);
swap(tpu,tpv);
}
sgt.update(1,ID[tpu],ID[u],val);
u = fa[tpu];
tpu = top[u];
}
if(deep[u] > deep[v])
swap(u,v);
sgt.update(1,ID[u],ID[v],val);
}
Info work_query(int u,int v){
Info U,V;
U.clear();
V.clear();
int tpu = top[u];
int tpv = top[v];
while(tpu != tpv){
if(deep[tpu] < deep[tpv]){
swap(u,v);
swap(tpu,tpv);
swap(U,V);
}
U = sgt.query(1,ID[tpu],ID[u])+U;
u = fa[tpu];
tpu = top[u];
}
if(deep[u] > deep[v]){
swap(u,v);
swap(U,V);
}
Info ans;
ans.clear();
ans = ans + rev(V);
ans = ans + rev(sgt.query(1,ID[u],ID[v]));
ans = ans + U;
return ans;
}
int main(){
while(~scanf("%d %d",&N,&M)){
memset(head,-1,sizeof(head));
edge_cnt = tot = 0;
for(int i = 1; i <= N; i++)
scanf("%d",&value[i]);
int u,v,val;
for(int i = 1; i < N; i++){
scanf("%d %d",&u,&v);
ADD(u,v);
ADD(v,u);
}
dfs1(1,0,0);
dfs2(1,1);
sgt.build(1,1,N);
char op[5];
while(M--){
scanf("%s %d %d",op,&u,&v);
if(op[0] == 'Q')
printf("%d\n",work_query(u,v).siz);
else{
scanf("%d",&val);
work_update(u,v,val);
}
}
}
return 0;
}