Codeforces 620E New Year Tree【线段树傻逼题】
题目大意
给你一棵树
让你支持子树染色,子树查询颜色个数,颜色数<=60, 节点数<=4e5
思路
因为颜色数很少,考虑状态压缩变成二进制
然后直接在dfs序上用线段树维护就可以了
//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 4e5 + 10;
ll val[N << 2], tag[N << 2];
#define LD (t << 1)
#define RD (t << 1 | 1)
void pushup(int t) {
val[t] = val[LD] | val[RD];
}
void pushnow(int t, ll vl) {
val[t] = tag[t] = vl;
}
void pushdown(int t) {
if (tag[t]) {
pushnow(LD, tag[t]);
pushnow(RD, tag[t]);
tag[t] = 0;
}
}
void modify(int t, int l, int r, int ql, int qr, ll vl) {
if (ql <= l && r <= qr) {
pushnow(t, vl);
return;
}
pushdown(t);
int mid = (l + r) >> 1;
if (qr <= mid) modify(LD, l, mid, ql, qr, vl);
else if (ql > mid) modify(RD, mid + 1, r, ql, qr, vl);
else {
modify(LD, l, mid, ql, mid, vl);
modify(RD, mid + 1, r, mid + 1, qr, vl);
}
pushup(t);
}
ll query(int t, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return val[t];
pushdown(t);
int mid = (l + r) >> 1;ll ans;
if (qr <= mid) ans = query(LD, l, mid, ql, qr);
else if (ql > mid) ans = query(RD, mid + 1, r, ql, qr);
else ans = query(LD, l, mid, ql, mid) | query(RD, mid + 1, r, mid + 1, qr);
pushup(t);
return ans;
}
struct Edge {
int v, nxt;
} E[N << 1];
int head[N], tot = 0;
int bg[N], ed[N], ind = 0;
int n, m, c[N];
void add(int u, int v) {
E[++tot] = (Edge) {v, head[u]};
head[u] = tot;
}
void dfs(int u, int fa) {
bg[u] = ++ind;
for (int i = head[u]; i; i = E[i].nxt) {
int v = E[i].v;
if (v == fa) continue;
dfs(v, u);
}
ed[u] = ind;
}
int bitcnt(ll a) {
int res = 0;
while (a) {
if (a & 1) ++res;
a >>= 1;
}
return res;
}
int main() {
//freopen("input.txt", "r", stdin);
Read(n), Read(m);
fu(i, 1, n) Read(c[i]);
fu(i, 2, n) {
int u, v;
Read(u), Read(v);
add(u, v);
add(v, u);
}
dfs(1, 0);
fu(i, 1, n) modify(1, 1, n, bg[i], bg[i], 1ll << c[i]);
while (m--) {
int op; Read(op);
switch(op) {
case 1: {
int x, col; Read(x), Read(col);
modify(1, 1, n, bg[x], ed[x], 1ll << col);
break;
}
case 2: {
int x; Read(x);
Write(bitcnt(query(1, 1, n, bg[x], ed[x])));
putchar('\n');
break;
}
}
}
return 0;
}
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 #303 (Div. 2) D. Queue 傻逼题
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- SPOJ 3261 (树套树傻逼题)
As another one of their crazy antics, the N (1 ≤ N ≤ 100,000) cows want Farmer John to race against ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- BZOJ4644: 经典傻逼题【线段树分治】【线性基】
Description 这是一道经典傻逼题,对经典题很熟悉的人也不要激动,希望大家不要傻逼. 考虑一张N个点的带权无向图,点的编号为1到N. 对于图中的任意一个点集 (可以为空或者全集),所有恰好有一 ...
- Codeforces 1500E - Subset Trick(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 一道线段树的套路题(似乎 ycx 会做这道题?orzorz!!11) 首先考虑什么样的 \(x\) 是"不合适"的,我 ...
- HDU 4578 Transformation --线段树,好题
题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...
- poj 3264:Balanced Lineup(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- hdu 1754:I Hate It(线段树,入门题,RMQ问题)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- python之Memcached 安装及操作
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- centos下搭建DNS
一.DNS名词介绍: ( Domain Name System )是“域名系统”的英文缩写 正向解析:通过域名查找IP 反向解析:通过IP查找域名 二.安装BIND: BIND即Berkeley In ...
- [spring]xml配置文件中bean属性的两种写法(p:configLocation <=> <property name="configLocation"/>)
1.当作bean节点的属性:p:configLocation: <!-- mybatis文件配置,扫描所有mapper文件 --> <bean id="sqlSession ...
- js中中括号,大括号使用详解
js中中括号,大括号使用详解 一.总结 一句话总结:{ } 是一个对象,[ ] 是一个数组 1.js大括号{}表示什么意思? 对象 { } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或 ...
- C++ 线程的创建、挂起、唤醒和结束 &&&& 利用waitForSingleObject 函数陷入死锁的问题解决
最近在写一个CAN总线的上位机软件,利用CAN转USB的设备连到电脑上,进行数据的传输.在接收下位机发送的数据的时候采用的在线程中持续接收数据. 1.在连接设备的函数中,开启线程. ,CREATE_S ...
- 微信小程序------小程序初步学习
1:学习微信小程序,首先的会一点前端的基础会比较容易上手,比如:HTML+CSS,JS,HTML5+CSS3: H5+CSS3中的弹性盒子在微信小程序中经常用到,这是必须掌握的.不会的可以去W3C文档 ...
- Windows音频SDK的发展历程
WASAPI is one of several native audio libraries in Windows. PortAudio actually supports five of them ...
- 【hive】在alter修改元数据的时候报错 mismatched input 'xxxxx' expecting KW_EXCHANGE
目的:修改表某个字段属性 语句: 报错信息 错误原因: 在HiveQL中,alter命令不使用与create或select相同的语义 ; 具体来说,您不能使用“ALTER DATABASE.TABLE ...
- 转载:【Oracle 集群】RAC知识图文详细教程(二)--Oracle 集群概念及原理
文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...
- 使用aidl的项目结构以及小的注意事项
在app的build.gradle里面添加: sourceSets{ main{ java.srcDirs = ['src/main/java','src/main/aidl'] } }