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 ...
随机推荐
- Java-basic-4-数据类型
Number类 装箱:将内置数据类型作为包装类对象使用:拆箱:相反 public class test{ public static void main(String args[]) { // box ...
- AJAX小练习
/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- 命令行执行Qt程序
原文网址 //helloworld.cpp #include <QApplication> #include <QPushButton> int main(int argc,c ...
- UVA - 11134 Fabled Rooks问题分解,贪心
题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...
- python练手系列-分布式监控
如果我们要写一个监控系统,要注意哪些问题和需求? [1] agent收集数据的时候需要通过系统调用少的方法收集到我们需要数据,一般来说我们优先使用python自带的系统方法,然后是读取/proc 文件 ...
- rsa Round #71 (Div. 2 only)
Replace A Time limit: 1000 msMemory limit: 256 MB You are given a string SS containing only letter ...
- CF878D D. Magic Breeding bitset
D. Magic Breeding time limit per test 4 seconds memory limit per test 1024 megabytes input standard ...
- Codeforces 903F Clear the Matrix
题目大意 考虑一个 $4$ 行 $n$ ($4\le n\le 1000$)列的矩阵 $f$,$f$ 中的元素为 * 或 . . 对 $f$ 进行若干次如下变换: 将一个 $k\times k$($1 ...
- ACM程序设计选修课——1051: Glamor Sequence(YY+求和公式)
1051: Glamor Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: 16 Solved: 5 [Submit][Status] ...
- P1875 佳佳的魔法药水 (最短路,DP)
题目链接 Solution 好题. 一开始一直在想怎么蛇皮建图,但是发现一直蛇不出来... 正解是用类似于 dijkstra 的算法对所有点进行松弛. 对于每个元素记录两个值: \(cost\) 代表 ...