[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#基础学习(一)
---恢复内容开始--- 1.最近被安排去做C#开发,然后开始一连串的看文档·看视屏,发现学C#给自己补了很多基础,C#每个函数变量什么都要先声名,而python可以直接定义: 一.数据类型 1.整数 ...
- linux netstat-查看Linux中网络系统状态信息
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. 语法 netstat(选项) ...
- xphrof性能分析线上部署实践
说明 将xhprof部署在线上环境,在特定情况下进行性能分析,方便快捷的排查线上性能问题. 通过参数指定及添加代码行触发进入性能分析,并将结果保存入MongoDB. 因为xhprof对性能的影响,只部 ...
- buf.toJSON()
buf.toJSON() 返回:{Object} 返回该 Buffer 实例的 JSON 表达式.当字符串化一个 Buffer 实例时会隐式调用 JSON.stringify() 这个函数. 例子: ...
- 在移动端H5开发中(关于安卓端position:fixed和position:absolute;和虚拟键盘冲突的问题,以及解决方案)
一.在开发移动端webapp时,我们经常会遇到这样的问题,当我们需要在页面底部固定一个logo或者说明时,往往会采用position:fixed进行固定定位或者absolute定位到最底部 这是一个很 ...
- GitHub总结
1) 工作原理 2) 工作流程 clone资源到本地 更新本地资源 新增或修改clone的资源 查看状态 资源推送回github
- PowerShell Tools for Visual Studio 2015
首先要去下载Visual Studio 2015 RC 版本 https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downl ...
- MySQL中间件之ProxySQL_读写分离/查询重写配置
MySQL中间件之ProxySQL_读写分离/查询重写配置 Posted on 2016-12-25 by mark blue, mark Leave a comment MySQL 1.闲扯几句 读 ...
- noip模拟赛 终末
分析:举个例子就能发现:偶数位上的数都必须是0,奇数位上的数可以取0~k-1,这就是一个标准的数位dp了. 这编译器......数组越界了竟然不报错. #include <cstdio> ...
- bzoj 3173 [Tjoi2013]最长上升子序列 (treap模拟+lis)
[Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2213 Solved: 1119[Submit][Status] ...