Codeforces 620E New Year Tree(线段树+位运算)
题目链接 New Year Tree
考虑到$ck <= 60$,那么用位运算统计颜色种数
对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询。
用线段树来维护。
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i(a); i <= (b); ++i)
struct node{
long long num, lazy;
} tree[ << ];
struct Node{
int l, r;
} e[];
vector <int> v[];
int n, m;
long long val[], c[];
int Time;
bool vis[];
long long ans, cover;
int op;
int x, y;
void dfs(int x, int fa){
e[x].l = ++Time;
val[Time] = c[x];
vis[x] = true;
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x);
}
e[x].r = Time;
}
inline void pushup(int i){
tree[i].num = tree[i << ].num | tree[i << | ].num;
}
inline void pushdown(int i){
if (tree[i].lazy){
tree[i << ].num = tree[i << | ].num = (1LL << tree[i].lazy);
tree[i << ].lazy = tree[i << | ].lazy = tree[i].lazy;
tree[i].lazy = ;
}
}
void build(int i, int l, int r){
tree[i].lazy = ;
if (l == r){
tree[i].num = (1LL << val[l]);
return ;
}
int mid = (l + r) >> ;
build(i << , l, mid);
build(i << | , mid + , r);
pushup(i);
}
void update(int i, int L, int R, int l, int r, long long cover){
if (l <= L && R <= r){
tree[i].lazy = cover;
tree[i].num = (1LL << cover);
return ;
}
int mid = (L + R) >> ;
pushdown(i);
if (l <= mid) update(i << , L, mid, l, r, cover);
if (r > mid) update(i << | , mid + , R, l, r, cover);
pushup(i);
}
void solve(int i, int L, int R, int l, int r){
if (l <= L && R <= r){
ans |= tree[i].num;
return;
}
pushdown(i);
int mid = (L + R) >> ;
if (l <= mid) solve(i << , L, mid, l, r);
if (r > mid) solve(i << | , mid + , R, l, r);
}
int main(){
scanf("%d%d", &n, &m);
rep(i, , n) v[i].clear();
rep(i, , n) scanf("%lld", c + i);
rep(i, , n - ){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
memset(vis, , sizeof vis); Time = ;
dfs(, );
build(, , n);
rep(i, , m){
scanf("%d%d", &op, &x);
if (op == ){
scanf("%lld", &cover);
update(, , n, e[x].l, e[x].r, cover);
}
else{
ans = ;
solve(, , n, e[x].l, e[x].r);
int ret = ;
for (; ans; ans -= ans & -ans) ++ret;
printf("%d\n", ret);
}
}
return ;
}
Codeforces 620E New Year Tree(线段树+位运算)的更多相关文章
- 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 ...
- Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...
- hdu 5023 线段树+位运算
主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- poj 3225 线段树+位运算
略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...
- POJ 2777 Count Color(线段树+位运算)
题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...
- poj_2777线段树+位运算
第一次没想到用位运算,不出意料的T了,,, PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦-- #include<iostream> #include<cstdio> # ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
随机推荐
- 图解Disruptor框架(一):初识Ringbuffer
图解Disruptor框架(一):初识Ringbuffer 概述 1. 什么是Disruptor?为什么是Disruptor? Disruptor是一个性能十分强悍的无锁高并发框架.在JUC并发包中, ...
- graph-Dijkstra's shortest-path alogorithm
直接贴代码吧,简明易懂. 后面自己写了测试,输入数据为: a b c d e 0 1 4 0 2 2 1 2 3 1 3 2 1 4 3 2 1 1 2 3 4 2 4 5 4 3 1 也就是课本上1 ...
- __vet_atags
参考:atags--__vet_atags标签 arch/arm/include/asm/setup.h /* * linux/include/asm/setup.h * * Copyright ...
- LeetCode(138) Copy List with Random Pointer
题目 A linked list is given such that each node contains an additional random pointer which could poin ...
- RQNOJ:PID30 / [stupid]愚蠢的矿工☆(树形背包)
PID30 / [stupid]愚蠢的矿工☆ 背景 Stupid 家族得知在HYC家的后花园里的中央花坛处,向北走3步,向西走3步,再向北走3步,向东走3步,再向北走6步,向东走3步,向南走12步,再 ...
- python网络编程相关
什么是网络套接字socket?简述基于tcp协议的套接字的通信流程. 为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了称为套接字 (Socket)的接口,区分 ...
- luogu3389 【模板】高斯消元法
#include <algorithm> #include <iostream> #include <cstdio> #include <cmath> ...
- SVN 删除所有目录下的“.svn”文件夹,让文件夹脱离SVN控制
SVN 删除所有目录下的“.svn”文件夹,将如下语句拷备到记事本,并保存为 *.reg,双击导入注册表,在文件夹右键中就多了一条“Delete SVN Folders”,点击就可以删处此目录下的所有 ...
- python - 接口自动化测试实战 - case1 - 优化版
题目: 基于以下两个接口和数据完成接口自动化测试,并生成测试报告: '''登录 login='http://47.107.168.87:8080/futureloan/mvc/api/member/l ...
- 当网络中断的时候,JTA全局事务管理,究竟会不会回滚???
前言:有人问了我一个问题,就是说在网络中断的时候,JTA的全局事务管理,会不会回滚?当时说会回滚,但没给对方说清楚理由,也不太认同我的观点.现在总结一下. 今天一天都在看文档(也查了一些博客和网站), ...