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 ...
随机推荐
- Win10命令提示符git log中文乱码的解决方案
在系统环境变量中新建一个名为LESSCHARSET的变量 其值为utf-8 新建完毕后应用,git log就不会出现乱码的问题了^_^ 参考博文:git- win10 cmd git log 中文乱码 ...
- C++单纯的指针传参的问题
C++指针传参也是单纯的复制一份地址,如下代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace st ...
- nginx i.com.conf
server { listen 9090; server_name i.com; root /Users/chong/Documents/www; # Load configuration files ...
- css---3链接伪类与动态伪类
链接伪类link:表示作为超链接,并指向一个未访问的地址的所有锚 链接伪类不可以加在div上 <!DOCTYPE html> <html> <head> <m ...
- input判断输入值是否合法
1.判断input输入的值是否合法有很多办法,我这里使用的是在onchange时进行判断,代码如下:[所有主要浏览器都支持] <input type="text" name= ...
- Ubuntu安装Windows软件
https://www.cnblogs.com/chendeqiang/p/10177530.html Windows系列软件 安装Deepin封装好的框架 git clone https://git ...
- MySQL安全查询模式的问题
在学习mysql中的简单sql语句的执行.在用到update语句的时候,总提示如下错误: 15:08:00 update students t set t.tel="156626488 ...
- iOS开发使用UIScrollView随笔
1.scrollview滚动到固定偏移量contenOffset - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)anim ...
- Delphi XE10百集视频教程计划
1. 前言 本人现在的职业是Java程序员,一直想学习一个做桌面应用的编程语言,几年前无意中接触到Delphi,比VB功能强大,比C++语法更容易理解,加上Oracle的PL/SQL的底子,最终决定学 ...
- HBase 面向列的存储