题目链接 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(线段树+位运算)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. hdu 5023 线段树+位运算

    主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...

  4. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  5. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

  6. poj 3225 线段树+位运算

    略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...

  7. POJ 2777 Count Color(线段树+位运算)

    题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...

  8. poj_2777线段树+位运算

    第一次没想到用位运算,不出意料的T了,,, PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦-- #include<iostream> #include<cstdio> # ...

  9. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

随机推荐

  1. python入门:简单模拟登陆时UTF-8转换成GBK编码

    #!/usr/bin/env python # -*- coding:utf-8 -*- """ 给变量x赋值为字符串‘请输入用户名:’ 变量x_unicode的赋值等于 ...

  2. Vue基础指令集锦

    v-model双向绑定数据 <input type="text" v-model="msg"> {{msg}} ###v-on 事件 <div ...

  3. 【windows】win7 sp1 系统语言中英文切换

    注:Windows 7 Ultimate and Windows 7 Enterprise (旗舰版和企业版) 可以直接在控制面板/地区和语言中修改显示语言,其他系统不行 进入网站下载相关的MUI包安 ...

  4. ubuntu中卸载没有安装完全的软件包

    sudo apt-get autoclean sudo apt-get clean sudo apt-get autoremove

  5. pandas处理较大数据量级的方法 - chunk,hdf,pkl

    前情提要: 工作原因需要处理一批约30G左右的CSV数据,数据量级不需要hadoop的使用,同时由于办公的本本内存较低的缘故,需要解读取数据时内存不足的原因. 操作流程: 方法与方式:首先是读取数据, ...

  6. Python使用gevent实现协程

    Python中多任务的实现可以使用进程和线程,也可以使用协程. 一.协程介绍 协程,又称微线程.英文名Coroutine.协程是Python语言中所特有的,在其他语言中没有. 协程是python中另外 ...

  7. 牛客网 Wannafly挑战赛21 灯塔

    Z市是一座港口城市,来来往往的船只依靠灯塔指引方向.在海平面上,存在n个灯塔.每个灯塔可以照亮以它的中心点为中心的90°范围.特別地, 由于特殊限制,每个灯塔照亮范围的角的两条边必须要么与坐标轴平行要 ...

  8. LOFTER 迁移

    title: LOFTER 迁移 date: 2018-09-01 16:41:02 updated: tags: [其他] description: keywords: comments: imag ...

  9. linux随笔四

    1.ps -ef     : -e   显示系统上运行的所有进程,-f 显示一些有用的信息列 UID:负责启动进程的用户 PID:进程的ID PPID:父进程的PID(某个进程由另一个进程启动) C: ...

  10. 【转载】用OCTAVE实现一元线性回归的梯度下降算法

    原文地址:http://www.cnblogs.com/KID-XiaoYuan/p/7247481.html STEP1 PLOTTING THE DATA 在处理数据之前,我们通常要了解数据,对于 ...