HDU 5266 pog loves szh III 线段树,lca
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of the tree.Then Szh choose some nodes from the tree. He wants Pog helps to find the least common ancestor (LCA) of these node.The question is too difficult for Pog.So he decided to simplify the problems.The nodes picked are consecutive numbers from lili to riri ([li,ri])([li,ri]).
Input
Several groups of data (no more than 3 groups,n≥10000n≥10000 or Q≥10000Q≥10000).
The following line contains ans integers,n(2≤n≤300000)n(2≤n≤300000).
AT The following n−1n−1 line, two integers are bibi and cici at every line, it shows an edge connecting bibi and cici.
The following line contains ans integers,Q(Q≤300000)Q(Q≤300000).
AT The following QQ line contains two integers li and ri(1≤li≤ri≤n1≤li≤ri≤n).
Output
For each case,output QQ integers means the LCA of [li,ri][li,ri].
Sample Input
5
1 2
1 3
3 4
4 5
5
1 2
2 3
3 4
3 5
1 5
Sample Input
1
1
3
1
1
一颗树以1为根节点,给出区间范围,求范围内所有点的lca公共祖先,之前学的lca算法只能求两个点的,但考虑到是区间内进行操作,故考虑线段树。这道题如果了解线段树的话可以说没什么难度了...
做这道题这道题之前,没学过线段树...搞了好久才弄清楚,在网上抄了个线段树模板,弄清楚后没有想象中的难,实际上就是简单的lca+线段树组合罢了,可以说是模板题也不为过,收获就是学会了bfs树上倍增和搞清楚了线段树吧,
这个数据结构挺神奇的。
该题G++提交会TLE,C++提交如果不手动扩栈就会爆栈,当然也可以用BFS倍增就不用担心爆栈了。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <cstdio>
#include <queue>
#pragma warning ( disable : 4996 )
#pragma comment(linker, "/STACK:102400000,102400000") //防止爆栈 using namespace std; int Max( int x, int y ) { return x>y?x:y; }
int Min( int x, int y ) { return x>y?y:x; } const int inf = 0x3f3f3f3f;
const int vspot = 3e5 + ;
const int espot = 1e5 + ; struct node {
int from, to, next;
}e[vspot<<]; int N, Q, cnt, logn;
int lca[vspot<<], linjie[vspot];
int fa[vspot][], depth[vspot]; void addedge(int u, int v)
{
e[cnt].from = u; e[cnt].to = v;
e[cnt].next = linjie[u]; linjie[u] = cnt++;
e[cnt].from = v; e[cnt].to = u;
e[cnt].next = linjie[v]; linjie[v] = cnt++;
} void init()
{
cnt = ;
logn = ;
memset( linjie, -, sizeof(linjie) );
memset( lca, , sizeof(lca) );
memset( depth, , sizeof(depth) );
memset( fa, , sizeof(fa) );
} void dfs( int x )
{
for( int i = ; i <= logn; i++ )
if ( fa[x][i-] )
fa[x][i] = fa[fa[x][i-]][i-];
else
break;
for ( int i = linjie[x]; i+; i = e[i].next )
if ( fa[x][] != e[i].to )
{
fa[e[i].to][] = x;
depth[e[i].to] = depth[x]+;
dfs(e[i].to);
}
} void bfs( int x )
{
queue<int> Q;
Q.push(x); while (!Q.empty())
{
int run = Q.front();
Q.pop();
for( int i = linjie[run]; i+; i = e[i].next )
if ( fa[run][] != e[i].to )
{
fa[e[i].to][] = run;
depth[e[i].to] = depth[run] + ;
Q.push(e[i].to);
}
}
} void init2()
{
for ( int i = ; i <= logn; i++ )
for ( int x = ; x <= N; x++ )
fa[x][i+] = fa[fa[x][i]][i];
} int Lca( int u, int v )
{
if ( depth[u] < depth[v] )
swap(u,v);
int d = depth[u] - depth[v];
for ( int i = ; i <= logn; i++ )
if( (<<i)&d )
u = fa[u][i]; if ( u == v ) return u;
for ( int i = logn; i >= ; i-- )
if ( fa[u][i] != fa[v][i] )
{
u = fa[u][i];
v = fa[v][i];
}
return fa[u][];
} void pushUp( int pos ) { lca[pos] = Lca(lca[pos<<], lca[pos<<|]); } void build( int lhs, int rhs, int id )
{
if ( lhs == rhs )
{
lca[id] = lhs;
return;
}
int mid = ( lhs + rhs ) >> ;
build( lhs, mid, id<< );
build( mid+, rhs, id<<| ); pushUp(id);
} //lhs, rhs表示当前节点的区间,l,r表示要操作的区间
int query( int lhs, int rhs, int l, int r, int id )
{
if ( l <= lhs && r >= rhs )
return lca[id];
int mid = (lhs+rhs)>>; int llca = -, rlca = -;
if ( l <= mid ) llca = query( lhs, mid, l, r, id<< );
if ( r > mid ) rlca = query( mid+, rhs, l, r, id<<| ); if ( llca == - || rlca == - )
return Max(llca, rlca);
else
return Lca(llca, rlca);
} int main()
{
while ( ~scanf("%d", &N) )
{
init();
int x, y;
for( int i = ; i < N; i++ )
{ scanf("%d %d", &x, &y ); addedge(x,y); }
bfs();
init2();
// dfs(1); 用dfs并去掉上面两行也行
build(,N,); cin >> Q;
int ans;
for ( int i = ; i <= Q; i++ )
{
scanf( "%d %d", &x, &y );
ans = query( , N, x, y, );
printf( "%d\n", ans );
}
}
return ;
}
HDU 5266 pog loves szh III 线段树,lca的更多相关文章
- HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- HDU 5266 pog loves szh III(区间LCA)
题目链接 pog loves szh III 题意就是 求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )
pog loves szh III Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- HDU 5266 pog loves szh III
题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...
- HDU 5266 pog loves szh III (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...
- hdu 5265 pog loves szh II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 pog loves szh II Description Pog and Szh are pla ...
- hdu 5264 pog loves szh I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5264 pog loves szh I Description Pog has lots of stri ...
- hdu 5264 pog loves szh I 水题
pog loves szh I Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
随机推荐
- 腾讯bugly接入插件(CocosCreator)
下载: plugin-bugly.zip (1.4 MB) 插件开源地址: https://github.com/tidys/CocosCreatorPlugins/tree/master/packa ...
- 2019-5-21-Total-Commander-显示文件包含文件名扩展
title author date CreateTime categories Total Commander 显示文件包含文件名扩展 lindexi 2019-5-21 11:37:6 +0800 ...
- (转)NodeJS - 第一个应用程序Hello World
安装NodeJs 在创建实际的“Hello,World!”应用之前,我们应该先安装NodeJS,安装NodeJS可以访问NodeJS官网,下载相应系统的NodeJS的安装包,进行安装. 程序组件 关于 ...
- Ubuntu vi命令
最近在使用ubuntu,在linux下,要编辑文件或者其他的文本文件,哪那么一个ubuntu linux下的强大的文本编辑工具就不得不提了,那就是VI编辑器.下面把VI常用到的命令行贴出来. :w ...
- SpringCloud搭建分布式配置中心(基于git)
1.简介 Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用 ...
- COGS 2479. [HZOI 2016] 偏序 (CDQ套CDQ)
传送门 解题思路 四维偏序问题,模仿三维偏序,第一维排序,第二维CDQ,最后剩下二元组,发现没办法处理,就继续嵌套CDQ分治.首先把二元组的左右两边分别打上不同的标记,因为统计答案时只统计左边对右边的 ...
- Java英语面试题(核心知识篇)
Java英语面试题(核心知识篇) Question: What is transient variable?Answer: Transient variable can't be serialize. ...
- 模板——Treap
不得不说平衡树博大精深,除了Treap,还有splay,非旋Treap和可持久化数据结构,今天先讲讲Treap,也很感谢这位大佬的博客给予我帮助:http://www.360doc.com/conte ...
- java线程池的使用学习
目录 1. 线程池的创建 2. 线程池的运行规则 3. 线程池的关闭 4. 线程池的使用场合 5. 线程池大小的设置 6 实现举例 1. 线程池的创建 线程池的创建使用ThreadPoolExecut ...
- freemarker 嵌套循环 (导出word时,修改ftl模板)
1.循环 (循环输出reportList列表的每行的姓名) <#list reportList as report> ${report.name} </$list> 2.嵌套循 ...