题解

弱智题,二进制表示位数。合并时用|

就是被1<<x卡了好久。

要写成1ll<<x才行

 #include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int cnt,head[N];
int id[N],size[N],cao[N],tot;
int n,m,a[N];
struct edge{
int to,nxt;
}e[N*];
struct tree{
int l,r,lazy;
long long sum;
}tr[N*];
void add(int u,int v){
cnt++;
e[cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void dfs1(int u,int fa){
id[u]=++tot;
cao[tot]=u;
size[u]=;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa)continue;
dfs1(v,u);
size[u]+=size[v];
}
}
void build(int l,int r,int now){
tr[now].l=l;
tr[now].r=r;
if(l==r){
tr[now].sum=1ll<<a[cao[l]];
return;
}
int mid=(l+r)>>;
build(l,mid,now*);
build(mid+,r,now*+);
tr[now].sum=tr[now*].sum|tr[now*+].sum;
}
void pushdown(int now){
if(tr[now].lazy==)return;
tr[now*].sum=tr[now*+].sum=1ll<<tr[now].lazy;
tr[now*].lazy=tr[now*+].lazy=tr[now].lazy;
tr[now].lazy=;
}
void update(int l,int r,int now,int c){
pushdown(now);
if(tr[now].l==l&&tr[now].r==r){
tr[now].sum=1ll<<c;
tr[now].lazy=c;
return;
}
int mid=(tr[now].l+tr[now].r)>>;
if(l>mid)update(l,r,now*+,c);
else if(r<=mid)update(l,r,now*,c);
else{
update(l,mid,now*,c);
update(mid+,r,now*+,c);
}
tr[now].sum=tr[now*].sum|tr[now*+].sum;
}
long long query(int l,int r,int now){
pushdown(now);
if(tr[now].l==l&&tr[now].r==r){
return tr[now].sum;
}
int mid=(tr[now].l+tr[now].r)>>;
if(l>mid)return query(l,r,now*+);
else if(r<=mid)return query(l,r,now*);
else{
return query(l,mid,now*)|query(mid+,r,now*+);
}
}
int work(long long x){
int ans=;
while(x){
if(x&)ans++;
x>>=;
}
return ans;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs1(,);
build(,n,);
for(int i=;i<=m;i++){
int k;
scanf("%d",&k);
if(k==){
int u,c;
scanf("%d%d",&u,&c);
// cout<<id[u]<<" "<<id[u]+size[u]-1<<endl;
update(id[u],id[u]+size[u]-,,c);
}
else{
int u;
scanf("%d",&u);
printf("%d\n",work(query(id[u],id[u]+size[u]-,)));
}
}
return ;
}

最新文章

  1. 实时控制软件设计第一周作业-汽车ABS软件系统案例分析
  2. Windows Server 2008 任务计划无法自动运行的解决办法
  3. 在UWP应用中加入Cortana语音指令集
  4. OFFSET IN 使用举例
  5. 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 1 Estimating unknown parameters
  6. JavaScript笔记——this的取值
  7. Android调用蓝牙打印机
  8. ubutun中安装nginx
  9. 面试常考的数据结构Java实现
  10. 利用函数索引优化&lt;&gt;
  11. gitosis随记
  12. 《linux就该这么学》第十四节课:第13章,部署DNS域名解析服务(bind服务)
  13. 关系型数据库 VS NOSQL
  14. docker 安装hadoop
  15. RunAsDate v1.36 突破软件试用30天的工具
  16. 【springboot+easypoi】一行代码搞定excel导入导出
  17. JAVA面向对象编程课程设计——项目部署
  18. 设计模式笔记-观察者(Observer)
  19. lintcode-392-打劫房屋
  20. pcl知识

热门文章

  1. mfc进制转换
  2. C++之易混淆知识点二
  3. python 3.x 学习笔记9 (面向对象)
  4. python网页问题
  5. JavaScript学习——JS对象和全局函数
  6. C# 打开文件 保存文件
  7. 使用IDEA在Maven中创建MyBatis逆向工程以及需要注意的问题(入门)
  8. 一、数组---数组中的K-diff数对※※※※※
  9. C语言基础 (3) C语言介绍
  10. SASS 使用(安装)