[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 中的一个数 做法分析 先这样考 ...
随机推荐
- 树莓派 -- 输入设备驱动 (key) 续2: 转载 Setting up a GPIO-Button “keyboard” on a Raspberry Pi
使用device-tree (DT) overlay应该是更方便的方法: http://blog.gegg.us/2017/01/setting-up-a-gpio-button-keyboard-o ...
- 全国高校json数据包(复python解析代码)
由于这段时间需要有关学校的三级联动插件,找了很久没有找到合适的,所以去教育部官网下载了一份全国普通高校名单(2019年), 这里附上解析该xls文件的代码 import xlrd import jso ...
- 牛客网补题 New Game!(原Wannafly summer camp day2原题)
思路:这个题在秦皇岛的时候好像没有写出来,反正我是没有写出来,题解是听懂了:把直线和圆都看做一个结点,圆和直线用点到直线的距离与半径差求出来,圆和圆之间用点和点之间的距离和半径差表示,最后最短路跑一遍 ...
- get方法和set方法
定义一个类,该类有一个私有成员变量,通过构造方法将其进行赋初值,并提供该成员的getXXX()和setXXX()方法 提示:假设有private String name;则有 public void ...
- window.onload 函数不执行处理
Google Chrome window.onload = function(){} 函数不执行处理 function pageOnLoad(){} window.onload = pageOnL ...
- multi cookie & read bug
js cookie multi cookie & read bug document.cookie; // "access_token_test=eyJhbGciOiJIUzI1Ni ...
- codeforces 363B
#include<stdio.h> #include<string.h> #define inf 999999999 #define N 151000 int a[N],c[N ...
- VIM使用技巧15
在vim的插入模式下,有时需要插入寄存器中的文本: 1.使用<C-r>{register} 2.使用<C-r><C-p>{register} 3.使用<C-r ...
- ci框架(codeigniter)Email发送邮件、收件人、附件、Email调试工具
ci框架(codeigniter)Email发送邮件.收件人.附件.Email调试工具 Email 类 CodeIgniter 拥有强大的 Email 类来提供如下的功能: 多 ...
- 转 POJ分类
OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...