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 ...
随机推荐
- 20.multi_case04
import aiohttp import asyncio import ssl async def fetch(session, url): async with session.get(url,s ...
- c# 多态实现_虚方法
实现方法: 虚方法, 抽象类, 接口 1.虚方法 将父类的方法标记为虚方法,使用关键字virtual,这个方法可以被子类重新写一遍. 在父类的方法前面加上一个virtual,在子类的方法前面加上一个o ...
- object_detection/protos/*.proto: No such file or directory
1 背景 使用TensorFlow Object Detection API的时,在object_detection/protos/中,可以看到一些proto 文件,需要使用protoc程序将这些pr ...
- php中heredoc使用方法
Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术.但是现在的一些论坛程序,和部分文章系统,都巧妙的使用heredoc技术,来部分的实 ...
- PHP网络请求优化
目录 1. 设置超时时间 2. 将串行请求并行化 1. 设置超时时间 连接超时:200ms 读超时: 800ms 写超时: 500ms 2. 将串行请求并行化 使用curl_multi_*() 使用s ...
- 踩坑记-java mysql 新增获取主键、DIY主键、UUID
java mysql 获取主键.DIY主键.UUID,简单粗暴,代码如下: mapper.xml insert id="add" parameterType="com.x ...
- 判断JS对象是否为空的几种方式
.将json对象转化为json字符串,再判断该字符串是否为"{}" var data = {}; var b = (JSON.stringify(data) == "{} ...
- 【转】IOS获取屏窗高度踩坑之window.outerHeight
近日本人在直接使用window.outerHeight获取屏窗高度时 在iphone 6中出现等于0的情况,从而导致页面发生错误 后找遍代码,测试无数,终于让我逮住了这个该死兼容 window.out ...
- LUOGU P1505 [国家集训队]旅游 (树链剖分+线段树)
传送门 解题思路 快被调死的码农题,,,其实就是一个边权下放到点权的线段树+树剖. #include<iostream> #include<cstdio> #include&l ...
- php中$_REQUEST、 $_GET、 $_POST、 $_COOKIE 的关系和区别
看到REQUEST可以通吃GET .POST .COOKIE 后 感觉这个$_REQUEST太强大了是不是其他的几个超级变量就没有用了,下面对他们整体做个比较: 1.安全性 post>get 2 ...