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 ...
随机推荐
- 【CF888G】Xor-MST
题目 也不是很知道为什么这道题要和某\(B\)姓算法扯上关系 首先有一个非常显然基于那个\(B\)姓算法的做法,每次启发式合并\(trie\)即可,复杂度是\(O(n\ logn\ loga_i)\) ...
- shell脚本练习06
######################################################################### # File Name: -.sh # Author ...
- c#窗体开发
奇: 常用控件的使用(期间参杂着VS快捷键/常用设置) 快捷键:引用命名空间:shift+alt+F10 断点:F9 调试:F5 逐句调试(每行代码一次跳转):F11 逐过程调试:F10 重构提取方法 ...
- 亲历者说:Kubernetes API 与 Operator,不为人知的开发者战争
如果我问你,如何把一个 etcd 集群部署在 Google Cloud 或者阿里云上,你一定会不假思索的给出答案:当然是用 etcd Operator! 实际上,几乎在一夜之间,Kubernetes ...
- thinkphp Mongo模型
Mongo模型是专门为Mongo数据库驱动而支持的Model扩展,如果需要操作Mongo数据库的话,自定义的模型类必须继承Think\Model\MongoModel. Mongo模型为操作Mongo ...
- thinkphp 子查询
从3.0版本开始新增了子查询支持,有两种使用方式: 大理石平台检验标准 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如: // 首先构造子 ...
- groupBy 后附加数量和每组百分比
SELECT i_State, n, , ) rat FROM ( SELECT * FROM ( ) n FROM planinfo GROUP BY i_State ) t1 ) s ) t
- 深入浅出 Java Concurrency (27): 并发容器 part 12 线程安全的List/Set[转]
本小节是<并发容器>的最后一部分,这一个小节描述的是针对List/Set接口的一个线程版本. 在<并发队列与Queue简介>中介绍了并发容器的一个概括,主要描述的是Queue的 ...
- 34 N皇后问题Ⅱ
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens-ii/ 34. N皇后问题 II 描述 笔记 数据 评测 讨论区 根据n皇后问题, ...
- iOS开发UIView.h简介
1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...