[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 中的一个数 做法分析 先这样考 ...
随机推荐
- C语言学习8
计算某日是该年的第几天 编写一个计算天数的程序,用户从键盘输入年.月.日,在屏幕中输出此日期是该年的第几天. /******************************************** ...
- HTML5中手势原理分析与数学知识的实践
摘要:在这触控屏的时代,人性化的手势操作已经深入了我们生活的每个部分.现代应用越来越重视与用户的交互及体验,手势是最直接且最为有效的交互方式,一个好的手势交互,能降低用户的使用成本和流程,大大提高了用 ...
- Git--使用须知123
详细的篇幅以后补充 安装篇: 设置篇: 由于我们大多数是windows程序员,那么,在使用git的过程前需要做一些设置项. 1.换行符自动转换. 查看:git config --global --li ...
- Quartz.Net 学习之路02 初探Quartz.Net
第二讲:简单的实例,看看Quartz.Net强在哪里? 直接上代码,代码里有注释: using System; using Quartz; using Quartz.Impl; namespace L ...
- 接口测试工具-fiddler的运用
本篇主要介绍一下fiddler的基本运用,包括查看接口请求方式,状态响应码,如何进行接口测试等 一.Fiddler的优点 独立的可以直接抓http请求 小巧.功能完善 快捷.启动就行 代理方便 二.什 ...
- BNUOJ 1589 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on PKU ...
- vagrant的学习 之 基础学习
vagrant的学习 之 基础学习 本文根据慕课网的视频教程练习,感谢慕课网! 慕课的参考文档地址:https://github.com/apanly/mooc/tree/master/vagrant ...
- Elasticsearch5.6搭建及拼音中文混合搜索实现
https://blog.csdn.net/UUfFO/article/details/78154499
- Ubuntu 16.04安装SoapUI工具进行接口测试(Web Service/WSDL/RESTfull)
SoapUI是一个跨平台接口测试工具,官方提供开源版本和商业版本.可以用来测试WSDL/RESTfull等接口. 替代的工具有JMeter. 一般用于WSDL的接口测试比较多,基于XML的形式,且这类 ...
- Ubuntu 16.04使用sudo apt-get -f install解决依赖时的注意事项(重点)
注意:在觉得软件依赖时,一般使用sudo apt-get -f install,但是也是非常危险的,尤其时一些软件需要删除某些依赖时,会导致原有安装的软件全部卸载.所以使用此命令时要时刻注意输出的这条 ...