http://codeforces.com/contest/620/problem/E
题意:给出一棵树,给个节点有不同的颜色(<60) 现有两种操作
1.将x节点的子树的颜色全部变为c
2.查询x节点子树有多少种不同的颜色
思路:首先dfs序维护线段树,主要难点是如何储存颜色的信息,这里询问的是由多少种不同的颜色,并且这里颜色数量很小,考虑二进制0,1,表示每个颜色,子树的父亲节点对子节点取或运算即可累加所有答案
并且处理标记,每次更新之前或者查询之前pushdown标记,更新结束后pushup
最后查询的时候检查每一位,统计答案即可
#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define fi first
#define se second
#define show(a) cout<<"Here is "<<a<<endl;
#define show2(a,b) cout<<"Here is "<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<"Here is "<<a<<" "<<b<<" "<<c<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 3e6 + 10;
const ll mod = 10007;
const int base=131;
ll n,block,m,id,t,x,y;
int num[N],cnt;
ll a[N],k;
ll ans,in[N],out[N];
ll tree[N],tag[N];
vector<int> v[N];
//tr1::unordered_map<ll,int> num;
void dfs(int x,int fa)
{
in[x]=++cnt;
num[cnt]=x;
for(int to:v[x])
{
if(to==fa) continue;
dfs(to,x);
}
out[x]=cnt;
}
void pushup(int rt)
{
tree[rt]=tree[rt<<1]|tree[rt<<1|1];
}
void pushdown(int rt)
{
tree[rt<<1|1]=tree[rt<<1]=tag[rt];
tag[rt<<1|1]=tag[rt<<1]=tag[rt];
tag[rt]=0;
}
void build(int l,int r,int rt)
{
if(l==r)
{
//show3("col",l,a[num[l]])
tree[rt]=(1LL<<a[num[l]]);
tag[rt]=0;
return ;
}
int m=l+r >> 1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushup(rt);
}
void update(int L,int R,int x,int l,int r,int rt)
{
//show3("now ",l,r)
if(L<=l&&r<=R)
{
tree[rt]=(1LL<<x);
tag[rt]=(1LL<<x);
return ;
}
if(tag[rt]) pushdown(rt);
int m=l+r>>1;
if(L<=m) update(L,R,x,l,m,rt<<1);
if(R>=m+1) update(L,R,x,m+1,r,rt<<1|1);
pushup(rt);
}
void query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
//show3(l,r,tree[rt])
x|=tree[rt];
return ;
}
if(tag[rt]) pushdown(rt);
int m=l+r>>1;
if(L<=m) query(L,R,l,m,rt<<1);
if(R>=m+1) query(L,R,m+1,r,rt<<1|1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<n;i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1,0);
build(1,n,1);
while(m--)
{
cin>>t;
if(t==1)
{
cin>>k>>x;
//show2(in[k],out[k]);
update(in[k],out[k],x,1,n,1);
}
else
{
cin>>k;
x=ans=0;
query(in[k],out[k],1,n,1);
for(int i=1;i<=60;i++)
if( (x>>i) & 1)
{
ans++;
}
cout<<ans<<endl;
}
}
}