LINK


题目大意

给你一棵树

让你支持子树染色,子树查询颜色个数,颜色数<=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【线段树傻逼题】的更多相关文章

  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 #303 (Div. 2) D. Queue 傻逼题

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  3. SPOJ 3261 (树套树傻逼题)

    As another one of their crazy antics, the N (1 ≤ N ≤ 100,000) cows want Farmer John to race against ...

  4. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  5. BZOJ4644: 经典傻逼题【线段树分治】【线性基】

    Description 这是一道经典傻逼题,对经典题很熟悉的人也不要激动,希望大家不要傻逼. 考虑一张N个点的带权无向图,点的编号为1到N. 对于图中的任意一个点集 (可以为空或者全集),所有恰好有一 ...

  6. Codeforces 1500E - Subset Trick(线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 一道线段树的套路题(似乎 ycx 会做这道题?orzorz!!11) 首先考虑什么样的 \(x\) 是"不合适"的,我 ...

  7. HDU 4578 Transformation --线段树,好题

    题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...

  8. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

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

随机推荐

  1. 04_Storm编程上手_WordCount集群模式运行

    1. 要解决的问题:代码打包 前一篇的代码,在IDEA中通过maven工程创建,通过IDEA完成代码打包 1)File -> Project Structure  2) 选择Artifacts, ...

  2. LA 5846 霓虹灯广告牌(单色三角形问题)

    https://vjudge.net/problem/UVALive-5846 题意: 圆周上有n个点,两两相连,只能涂红色或蓝色.求单色三角形的个数. 思路: 这个问题在训练指南105页有详细讲解. ...

  3. Excel转化成DataTable实现:NPOI和OLEDb

    使用两种方式实现的excel数据转化成DataSet,再结合前一篇的DataTable转化为实体,就可以解决excel到实体之间的转化. 代码如下: 首先定义一个接口: public interfac ...

  4. Nginx与PHP(FastCGI)的安装、配置

    摘自:http://www.linuxde.net/2012/03/9130.html 一.什么是 FastCGI FastCGI是一个可伸缩地.高速地在HTTP server和动态脚本语言间通信的接 ...

  5. webstorm拉取git代码

    在webstorm中VCS → git → clone → url就是你的git代码地址,parent Directory(你要放到的目录),Directiory Name(起一个项目名称)

  6. [mybatis]Mapper XML 文件——statementType

    statementType:STATEMENT,PREPARED 或 CALLABLE(存储过程) 的一个.这会让 MyBatis 分别使用 Statement,PreparedStatement 或 ...

  7. spring mvc:输出json,输出多个json

    spring mvc:输出xml/输出json 用到的注解@ResponseBody @ResponseBody用来输出json/xml等格式数据(非html) controller输出用到的类 or ...

  8. 三十一 Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行、scrapy-splash、splinter

    1.chrome谷歌浏览器无界面运行 chrome谷歌浏览器无界面运行,主要运行在Linux系统,windows系统下不支持 chrome谷歌浏览器无界面运行需要一个模块,pyvirtualdispl ...

  9. Project facet Java version 1.8 is not supported.

    Eclipse中添加项目到Servers中时提示“Project facet Java version 1.8 is not supported.” 解决方案:方法一:选中项目,右键roperties ...

  10. VMware虚拟机创建安装之后不出现VMnet1和VMnet8虚拟网卡

    大家可能遇到过安装虚拟机之后,不出现这两张虚拟网卡,造成一系列的网络问题 VMware虚拟机无法将网络改为桥接状态 本人亲试可行的解决办法 首先把你之前安装的VMware虚拟机卸载,清理得一干二净: ...