[Codeforces Education Round 6E] New Year Tree
[题目链接]
https://codeforces.com/contest/620/problem/E
[算法]
显然 , 一棵子树的DFS序必然为连续的一段
用线段树维护颜色数即可
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + ; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , m , tot , timer;
int a[MAXN],dfn[MAXN],pos[MAXN],size[MAXN],head[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
struct SegmentTree
{
struct Node
{
int l , r , tag;
long long value;
} Tree[MAXN << ];
inline void update(int index)
{
Tree[index].value = Tree[index << ].value | Tree[index << | ].value;
}
inline void build(int index,int l,int r)
{
Tree[index].l = l;
Tree[index].r = r;
Tree[index].tag = -;
Tree[index].value = ;
if (l == r)
{
Tree[index].value = 1ll * << a[pos[l]];
return;
}
int mid = (l + r) >> ;
build(index << ,l,mid);
build(index << | ,mid + ,r);
update(index);
}
inline void pushdown(int index)
{
if (Tree[index].tag != -)
{
Tree[index << ].tag = Tree[index << | ].tag = Tree[index].tag;
Tree[index << ].value = Tree[index << | ].value = 1ll * << Tree[index].tag;
Tree[index].tag = -;
}
}
inline void modify(int index,int l,int r,int c)
{
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].value = 1ll * << c;
Tree[index].tag = c;
return;
}
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index << ,l,r,c);
else if (mid + <= l) modify(index << | ,l,r,c);
else
{
modify(index << ,l,mid,c);
modify(index << | ,mid + ,r,c);
}
update(index);
}
inline long long query(int index,int l,int r)
{
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].value;
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query(index << ,l,r);
else if (mid + <= l) return query(index << | ,l,r);
else return query(index << ,l,mid) | query(index << | ,mid + ,r);
}
} T; inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline void dfs(int u,int fa)
{
dfn[u] = ++timer;
pos[timer] = u;
size[u] = ;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
dfs(v,u);
size[u] += size[v];
}
}
inline int calc(long long x)
{
int ret = ;
while (x > )
{
ret += x & ;
x >>= ;
}
return ret;
} int main()
{ read(n); read(m);
for (int i = ; i <= n; i++)
{
read(a[i]);
a[i]--;
}
for (int i = ; i < n; i++)
{
int x , y;
read(x); read(y);
addedge(x,y);
addedge(y,x);
}
dfs(,-);
T.build(,,n);
while (m--)
{
int op;
read(op);
if (op == )
{
int v , col;
read(v); read(col);
T.modify(,dfn[v],dfn[v] + size[v] - ,--col);
} else
{
int v;
read(v);
printf("%d\n",calc(T.query(,dfn[v],dfn[v] + size[v] - )));
}
}
return ; }
[Codeforces Education Round 6E] New Year Tree的更多相关文章
- Codeforces Education Round 11
A(模拟+数学) 题意:在一个数列当中最少添加多少个数可以使它们两两互质,并打印出添加以后的数列 #include <iostream> #include <cstdio> # ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #57 (Div. 2)
Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...
- Codeforces Beta Round 84 (Div. 2 Only)
layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
随机推荐
- random,json,pickle,hashlib,shutil,hmac,shelve 模块
一,复习 ''' 项目开发规范 ATM -- bin: 可执行文件 # run.py import os import sys BASE_DIR = os.path.dirname(os.path.d ...
- (八)python3 迭代
迭代:如果给定一个 list 或 tuple,我们可以通过 for 循环来遍历这个 list 或tuple,这种遍历我们称为迭代(Iteration) 字典: >>> d = {'a ...
- B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~
B. Mr. Kitayuta's Colorful Graph -> Link <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...
- amoeba连接mysql--ERROR 2006 (HY000): MySQL server has gone away
amoeba下载地址:http://sourceforge.net/projects/amoeba/files amoeba version:amoeba-mysql-binary-2.1.0-RC5 ...
- 整体二分初识--POJ2104:K-th Number
n<=100000个数有m<=5000个询问,每次问区间第k大. 方法一:主席树!…… 方法二:整体二分. 整体二分一次性计算半个值域对一个区间的询问的贡献,然后根据“这半边的贡献在某个询 ...
- Codeforces698C. LRU
n<=20种东西,有个大小k<=n的箱子,每次会以固定的概率从所有东西里选一种,若箱子里有空位且这种东西没出现过就丢进去,若箱子满了且这种东西没出现过就把最早访问过的一个丢掉,(只要在每次 ...
- PostgreSQL及PostGIS使用
基础知识 参考文档:http://www.postgis.net/docs/ PostGIS支持的GIS对象是OpenGIS Consortium(OGC)定义的“简单特征”的超集.OpenGIS规范 ...
- Eclipse 搭建tomcat+动态项目完整版
1. Tomcat搭建 1.新加服务器,右击控制台的server目录->new->server->选择本地tomcat 2.配置tomcat属性(如果更改失败,将tomcat下的项目 ...
- AndroidStudio NDK开发、调试测试工程
## 验证内容:1.支持NDK调试 2.支持native方法快速创建jni封装(但是没有加入extern "C"声明,会导致native方法找不到jni,进而报错) 3.支持通过修 ...
- eclipse bug之No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK
解决办法: 1.eclipse菜单 - Window - Preferences- Java - Installed JREs 将配置的JRE定位到JDK,例如JRE home:D:\Program ...