题解

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

就是被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. Calender

    public static void main(String[] args) { // TODO 自动生成的方法存根 Calendar c = new GregorianCalendar(); c., ...

  2. xBIM 基础05 3D墙案例

    系列目录    [已更新最新开发文章,点击查看详细]  使用编码的形式去生成一堵墙的模型需要做很多的工作. using System; using System.Collections.Generic ...

  3. kettle工具的设计原则

    不多说,直接上干货! Kettle工具在设计初,就考虑到了一些设计原则.这些原则里借鉴了以前使用过的其他一些ETL工具积累下的经验和教训. 易于开发:作为数据仓库和ETL开发者,你只想把时间用在创建B ...

  4. Git 连接细节

    Git 连接细节 首先创建Git 账号 https://github.com/ 下载Git https://git-scm.com/downloads 新建远程仓库 SSH keys : 打开 Git ...

  5. Install the high performance Nginx web server on Ubuntu

    Look out Apache, there's a web server – Nginx (pronounced Engine X) – that means to dismantle you as ...

  6. BootStrap学习(二)——重写首页之topbar

    1.布局容器 帮助文档:http://v3.bootcss.com/css/#overview-container BootStrap需要为页面内容和栅栏系统包裹一个.container容器.提供的两 ...

  7. http扩展请求头中的x-Forwarded-For

    X-Forwarded-For格式: X-Forwarded-For: client-ip, proxy1-ip, proxy2-ip 客户端请求服务器的过程中没经过一个代理层(代理中使用了xff)那 ...

  8. 优动漫PAINT-简单的树、叶教学

    如题,简单.好用:其实说的还是一个观察的事.看你是否足够细心,对于树叶的生长.枝桠和树干的关系是否了解咯. 对于这样的树枝丫和叶子完全可以使用优动漫PAINT完成,简单又快捷,软件下载:www.don ...

  9. ueditor 编辑器,自定义图片上传

    <div> <h1>完整demo</h1> <form method="post" name="form"> & ...

  10. Python 中多线程之 _thread

    _thread模块是python 中多线程操作的一种模块方式,主要的原理是派生出多线程,然后给线程加锁,当线程结束的 时候取消锁,然后执行主程序 thread 模块和锁对象的说明 start_new_ ...