CF620E New Year Tree(线段树+二进制)
2024-10-01 18:46:22
题解
弱智题,二进制表示位数。合并时用|
就是被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 ;
}
最新文章
- 实时控制软件设计第一周作业-汽车ABS软件系统案例分析
- Windows Server 2008 任务计划无法自动运行的解决办法
- 在UWP应用中加入Cortana语音指令集
- OFFSET IN 使用举例
- 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 1 Estimating unknown parameters
- JavaScript笔记——this的取值
- Android调用蓝牙打印机
- ubutun中安装nginx
- 面试常考的数据结构Java实现
- 利用函数索引优化<;>;
- gitosis随记
- 《linux就该这么学》第十四节课:第13章,部署DNS域名解析服务(bind服务)
- 关系型数据库 VS NOSQL
- docker 安装hadoop
- RunAsDate v1.36 突破软件试用30天的工具
- 【springboot+easypoi】一行代码搞定excel导入导出
- JAVA面向对象编程课程设计——项目部署
- 设计模式笔记-观察者(Observer)
- lintcode-392-打劫房屋
- pcl知识