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 ...
随机推荐
- Python爬虫一
爬虫 什么是爬虫? 网络爬虫(又被称为网页蜘蛛,网络机器人)就是模拟客户端发送网络请求,接收请求响应, 一种按照一定的规则,自动地抓取互联网信息的程序. 原则上,只要是浏览器(客户端)能做的事情,爬虫 ...
- leetcode-26-exercise_linked-list
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. 解题思路: 需要检查before和afte ...
- STM32三种启动模式 boot0 boot1
STM32三种启动模式对应的存储介质均是芯片内置的,它们是: 1)用户闪存=芯片内置的Flash.2)SRAM=芯片内置的RAM区,就是内存啦.3)系统存储器=芯片内部一块特定的区域,芯片出厂时在这个 ...
- cf 1017C
C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Linux学习-备份要点
备份资料的考虑 老实说,备份是系统损毁时等待救援的救星!因为你需要重新安装系统时, 备份的好坏会影响到你 系统复原的进度!事实上,系统有可能由于不预期的伤害而导致系统发生错误! 什么是不预期的伤害呢? ...
- Cookie和Session的作用和工作原理
一.Cookie详解 (1)简介 因为HTTP协议是无状态的,即服务器不知道用户上一次做了什么,这严重阻碍了交互式Web应用程序的实现.在典型的网上购物场景中,用户浏览了几个页面,买了一盒饼干和两饮料 ...
- Uiautomator ---(1) 封装代码
http://www.cnblogs.com/by-dream/p/4996000.html 上面是别人的写法 我自己的写法: package qq.test; import android.con ...
- maven项目打包jar,含有依赖jar
在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...
- 修改mysql数据的字符集校验规则使其区分大小写
mysql 使用utf8字符集默认的校验规则collate为utf8_general_ci,不区分数据的大小写 测试如下 ::) character set utf8 collate utf8_bin ...
- Xcode中断点的使用
注:本文由破船译自:albertopasca.本文由HoNooD在iosfeed站点上做了推荐. 这里先推荐两篇Xcode相关的文章: Xcode Code Snippets iOS调试 — 基本技巧 ...