[Tjoi2016&Heoi2016] 树
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4551
[算法]
树链剖分
时间复杂度 : O(QlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 struct Node
{
int l , r;
bool flg;
} Tree[MAXN << ];
struct edge
{
int to , nxt;
} e[MAXN << ]; int n , q , timer , tot;
int size[MAXN],top[MAXN],dfn[MAXN],head[MAXN],son[MAXN],father[MAXN],rev[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;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline void dfs1(int u)
{
size[u] = ;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == father[u]) continue;
father[v] = u;
dfs1(v);
size[u] += size[v];
if (size[v] > size[son[u]]) son[u] = v;
}
}
inline void dfs2(int u,int tp)
{
dfn[u] = ++timer;
rev[timer] = u;
top[u] = tp;
if (son[u]) dfs2(son[u],tp);
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v != son[u] && v != father[u]) dfs2(v,v);
}
}
inline void update(int index)
{
Tree[index].flg = Tree[index << ].flg | Tree[index << | ].flg;
}
inline void build(int index,int l,int r)
{
Tree[index].l = l;
Tree[index].r = r;
Tree[index].flg = false;
if (l == r)
{
if (l == ) Tree[index].flg = true;
return;
}
int mid = (l + r) >> ;
build(index << ,l,mid);
build(index << | ,mid + ,r);
update(index);
}
inline void modify(int index,int pos)
{
if (Tree[index].l == Tree[index].r)
{
Tree[index].flg = true;
return;
}
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= pos) modify(index << ,pos);
else modify(index << | ,pos);
update(index);
}
inline int query(int index,int l,int r)
{
if (!Tree[index].flg) return ;
if (Tree[index].l == Tree[index].r) return l;
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
{
int tmp = query(index << | ,mid + ,r);
if (tmp == ) tmp = query(index << ,l,mid);
return tmp;
}
} int main()
{ scanf("%d%d",&n,&q);
for (int i = ; i < n; i++)
{
int u , v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs1();
dfs2(,);
build(,,n);
while (q--)
{
char op[];
int x;
scanf("%s%d",&op,&x);
if (op[] == 'Q')
{
int tx = top[x] , pos = ;
while (true)
{
pos = query(,dfn[tx],dfn[x]);
if (pos != ) break;
x = father[tx]; tx = top[x];
}
printf("%d\n",rev[pos]);
} else modify(,dfn[x]);
} return ; }
[Tjoi2016&Heoi2016] 树的更多相关文章
- BZOJ 4551: [Tjoi2016&Heoi2016]树
4551: [Tjoi2016&Heoi2016]树 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 748 Solved: 394[Subm ...
- BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树
BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...
- [BZOJ4551][TJOI2016&&HEOI2016]树(并查集)
4551: [Tjoi2016&Heoi2016]树 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1746 Solved: 800[Sub ...
- 【BZOJ4551】[Tjoi2016&Heoi2016]树 并查集
[BZOJ4551][Tjoi2016&Heoi2016]树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两 ...
- BZOJ4551——[Tjoi2016&Heoi2016]树
1.题意: 给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个 结点,可以打多次标记.)2. 询问操作:询问某个 ...
- [bzoj4551][Tjoi2016][Heoi2016]树
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心. 现在她想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作: 1. 标记操作:对某个结点打上标记(在最开始,只有结点1有 ...
- BZOJ4551: [Tjoi2016&Heoi2016]树
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标 ...
- [bzoj4551][Tjoi2016&Heoi2016]树-树链剖分
Brief Description 给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个 结点,可以打多次标记.) ...
- BZOJ 4551[Tjoi2016&Heoi2016]树(树链剖分+二分)
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记 ...
- BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树
题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...
随机推荐
- 数据库连接 Mysqli
//数据库连接 Mysqli $db= new Mysqli("localhost","root","root","asd_808 ...
- google的三篇论文
文章:MapReduce/GFS/BigTable三大技术资料 文章:Google File System(中文翻译) 文章:MapReduce:超大机群上的简单数据处理 文章:Google's Bi ...
- Postman调试依赖登录接口的3种方法
在接口测试种, 我们经常会遇到有些接口登录后才能访问.我们在使用Postman调试这种接口时一般有3种方法: 依次请求 如果有登录接口的文档,或者通过抓包比较容易抓出登录请求的参数和格式,可以先使用P ...
- 数据库备份与还原c#.net实现
原文发布时间为:2008-10-25 -- 来源于本人的百度文章 [由搬家工具导入] 数据库备份与还原c#.net实现: 页面上面有 备份,还原,下拉菜单(浏览备份文件夹下面的所有文件名),删除(删除 ...
- jQuery根据属性模糊匹配元素
1.查看带有指定属性的元素: [attribute] 例如: $("div[id]") 2.查看属性值是某个特定值的元素: [attribute=value] 例如: $(&quo ...
- 收集的一些Redis操作技巧教程
redis(1).redis入门 redis(2).redis数据类型 redis(3).基于jedis.spring-data-redis 连接操作redis redis(4).基于redis 构建 ...
- Effective Java Profiling With Open Source Tools
https://www.infoq.com/articles/java-profiling-with-open-source
- java的计时:毫秒、纳秒
System.currentTimeMillis()获取毫秒值,但是其精度依赖操作系统 想实现较为精确的毫秒,可以采用 System.nanoTime()/1000000L System.nanoTi ...
- [Angular] Modify User Provided UI with Angular Content Directives
If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the con ...
- form怎样正确post文件
form在HTML中,是用于收集用户输入的,基本全部浏览器都支持form.给form加入method属性.就能实现将用户在form内控件输入的信息POST到制定地址.或发送GET请求. 写了以下一段代 ...