题解

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

就是被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 ;
}

CF620E New Year Tree(线段树+二进制)的更多相关文章

  1. CF620E New Year Tree 线段树 dfs序

    luogu链接 题目大意: 有一个节点有颜色的树 操作1.修改子树的颜色 操作2.查询子树颜色的种类 注意,颜色种类小于60种 只有子树的操作,dfs序当然是最好的选择 dfs序列是什么,懒得讲了,自 ...

  2. CF620E New Year Tree 线段树+dfs序+bitset

    线段树维护 dfs 序是显然的. 暴力建 60 个线段树太慢,于是用 bitset 优化就好了 ~ code: #include <bits/stdc++.h> #define M 63 ...

  3. POJ - 2777——Count Color(懒标记线段树二进制)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53639   Accepted: 16153 Des ...

  4. CodeForces 620E New Year Tree(线段树的骚操作第二弹)

    The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited h ...

  5. BZOJ.3307.雨天的尾巴(dsu on tree/线段树合并)

    BZOJ 洛谷 \(dsu\ on\ tree\).(线段树合并的做法也挺显然不写了) 如果没写过\(dsu\)可以看这里. 对修改操作做一下差分放到对应点上,就成了求每个点子树内出现次数最多的颜色, ...

  6. HDU 3333 Turing Tree (线段树)

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. HDU 3333 Turing Tree 线段树+离线处理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...

  8. 【UOJ388】配对树(dsu on tree+线段树)

    传送门 题意: 给出一颗含有\(n\)个结点的无根树,之后给出一个长度为\(m\)的序列,每个元素在\([1,n]\)之间. 现在序列中每个长度为偶数的区间的完成时间定义为树上最小配对方法中每对匹配点 ...

  9. 【cf375】D. Tree and Queries(dsu on tree+线段树)

    传送门 题意: 给出一颗以\(1\)为根的有根树,每个结点有个颜色\(c_i\). 之后要回答\(m\)组询问,每组询问包含\(v_i,k_i\),要回答以\(v_i\)为根的子树中,颜色出现次数不小 ...

随机推荐

  1. 17.广度优先遍历bfs

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  2. BZOJ 2301 莫比乌斯函数+分块

    思路: 同BZOJ1101 就是加个容斥 - http://blog.csdn.net/qq_31785871/article/details/54340241 //By SiriusRen #inc ...

  3. SharePoint 使用Expression Web 设计网站

    创建好网站以后可就可以开始发布了 possible causes : 1.The web server may not hava the FrontPage Server Extensions ins ...

  4. Combobox下拉框两级联动

    下拉框的两级联动是我们开发中经常遇到一种情况.比如一个学生管理系统中,根据年级.科目及姓名查询学生考试成绩,年级和科目都是硬盘中的有限数据(数据库)而学生则可以有用户手动指定,这时在数据库中有年级和科 ...

  5. TLCL

    参考阅读:http://billie66.github.io/TLCL/book/chap04.html 绝对路径 An absolute pathname begins with the root ...

  6. QT笔记 -- (1) .ui文件

    刚开始写QT,designer用的不习惯,打开.ui文件看了一下,很容易读的xml文件,记录一下. 大体框架如下 <?xml version="1.0" encoding=& ...

  7. (2016北京集训十三)【xsy1533】mushroom - bitset

    题解: 神题...我看到的时候直接吓懵了... 这是一道STL题...否则可能要写可持久化ETT或者可持久化Toptree? 用bitset来维护每个蘑菇上哪里有杂草,那么 对于操作1和操作2:可以预 ...

  8. 'Upgrade' header is missing

    spring-websocket 握手失败是因为 有拦截器  注释掉拦截器就OK

  9. ajax 不执行

    1.get形式访问: 一个相同的URL 只有一个结果,所以 第二次访问的时候 如果 URL字符串没变化 浏览器是 直接拿出了第一次访问的结果,post则不会 解决办法: 1.url+new Date( ...

  10. AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件

    个人使用记录 1:KEY 授权 chmod 400 VPN.pem 2:连接 ssh -i "VPN.pem" ubuntu@ec2-54-183-119-93.us-west-1 ...