个人感觉是道不错的题。。很好的帮我理解了dfs序与链剖之间的关系。。两者都是把树结构转化成区间的方法,并且通常需要配合数据结构加以维护。但dfs序侧重与顺序遍历,能够处理子树问题;链剖侧重重链处理,能够处理树中2点之间的链。看来dfs序有必要好好重视一番。。
求dfs序后转化成区间,然而这次已经不是单点修改了。。而是子树一起修改,那么就只能用线段树进行维护了。。
一开始的思路是记录关键值然后进行区间合并。。然而不是很能理解为什么只给了60种颜色。。然后看题解才知道可以转为称位运算,合并时进行或运算就能表示出颜色种数了。。好神奇orz!!!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1e9
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 400005
#define nm 800005
#define pi 3.141592653
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m,f[NM],_x,_y,tot,in[NM],out[NM];
ll a[NM],b[NM],_t;
struct edge{int t;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
void dfs(int x){
in[x]=++tot;
link(x)if(!f[j->t]){f[j->t]=x;dfs(j->t);}
out[x]=tot;
}
struct info{
ll s,tag;int size;
info operator+(const info&o){
info f;f.s=s|o.s;f.tag=0;f.size=size+o.size;
return f;
}
}T[4*NM];
void build(int i,int x,int y){
int t=x+y>>1;
if(x==y){T[i].size=1;T[i].s=a[x];return;}
build(i<<1,x,t);build(i<<1|1,t+1,y);
T[i]=T[i<<1]+T[i<<1|1];
}
void pushdown(int i){
if(T[i].size>1&&T[i].tag){
T[i<<1].s=T[i<<1|1].s=T[i<<1].tag=T[i<<1|1].tag=T[i].tag;
T[i].tag=0;
}
}
void mod(int i,int x,int y){
int t=x+y>>1;
if(_y<x||y<_x)return;
pushdown(i);
if(_x<=x&&y<=_y){T[i].s=_t;T[i].tag=_t;return;}
mod(i<<1,x,t);mod(i<<1|1,t+1,y);
T[i]=T[i<<1]+T[i<<1|1];
}
ll query(int i,int x,int y){
int t=x+y>>1;
if(_y<x||y<_x)return 0;
pushdown(i);
if(_x<=x&&y<=_y)return T[i].s;
return query(i<<1,x,t)|query(i<<1|1,t+1,y);
}
void solve(ll x){
int s=0;
while(x)s++,x-=lowbit(x);
printf("%d\n",s);
}
int main(){
//freopen("data.in","r",stdin);
n=read();m=read();
inc(i,1,n)b[i]=read()-1;
inc(i,1,n-1){_x=read();_y=read();add(_x,_y);add(_y,_x);}
dfs(f[1]=1);
inc(i,1,n)a[in[i]]=(ll)1<<b[i];
build(1,1,n);
while(m--){
_t=read();_x=read();
if(_t==1){
_t=read();_y=out[_x];_x=in[_x];_t=(ll)1<<(_t-1);
mod(1,1,n);
}else{
_y=out[_x];_x=in[_x];
solve(query(1,1,n));
}
}
return 0;
}
The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited his best friends Kerim and Gural to help him to redecorate the New Year tree.
The New Year tree is an undirected tree with n vertices and root in the vertex 1.
You should process the queries of the two types:
- Change the colours of all vertices in the subtree of the vertex v to the colour c.
- Find the number of different colours in the subtree of the vertex v.
The first line contains two integers n, m (1 ≤ n, m ≤ 4·105) — the number of vertices in the tree and the number of the queries.
The second line contains n integers ci (1 ≤ ci ≤ 60) — the colour of the i-th vertex.
Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the vertices of the j-th edge. It is guaranteed that you are given correct undirected tree.
The last m lines contains the description of the queries. Each description starts with the integer tk (1 ≤ tk ≤ 2) — the type of the k-th query. For the queries of the first type then follows two integers vk, ck (1 ≤ vk ≤ n, 1 ≤ ck ≤ 60) — the number of the vertex whose subtree will be recoloured with the colour ck. For the queries of the second type then follows integer vk (1 ≤ vk ≤ n) — the number of the vertex for which subtree you should find the number of different colours.
For each query of the second type print the integer a — the number of different colours in the subtree of the vertex given in the query.
Each of the numbers should be printed on a separate line in order of query appearing in the input.
7 10 1 1 1 1 1 1 1 1 2 1 3 1 4 3 5 3 6 3 7 1 3 2 2 1 1 4 3 2 1 1 2 5 2 1 1 6 4 2 1 2 2 2 3
2 3 4 5 1 2
23 30 1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10 4 11 6 12 6 13 7 14 7 15 7 16 8 17 8 18 10 19 10 20 10 21 11 22 11 23 2 1 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 4 1 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 20 1 1 21 1 1 22 1 1 23 1 2 1 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 4
6 1 3 3 2 1 2 3 5 5 1 2 2 1 1 1 2 3