pog loves szh III

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 470    Accepted Submission(s): 97

Problem Description
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 li to ri ([li,ri]).

Hint : You should be careful about stack overflow !

 
Input
Several groups of data (no more than 3 groups,n≥10000 or Q≥10000).

The following line contains ans integers,n(2≤n≤300000).

AT The following n−1 line, two integers are bi and ci at every line, it shows an edge connecting bi and ci.

The following line contains ans integers,Q(Q≤300000).

AT The following Q line contains two integers li and ri(1≤li≤ri≤n).

 
Output
For each case,output Q integers means the LCA of [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 Output
1
1
3
3
1

Hint

Be careful about stack overflow.

 

这个题有点裸,以为LCA是满足结合律的,所以直接拿个线段树来搞就可以了~

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
using namespace std ;
typedef long long LL ;
const int N = ;
const int DEG = ; int n , lca[N<<] ; struct Edge
{
int to,next;
}edge[N*]; int head[N],tot; void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void init()
{
tot = ;
memset(head,-,sizeof(head));
} int fa[N][DEG] , deg[N]; void BFS(int root)
{
queue<int>que;
deg[root] = ;
fa[root][] = root;
que.push(root);
while(!que.empty())
{
int tmp = que.front();
que.pop();
for(int i = ;i < DEG;i++)
fa[tmp][i] = fa[fa[tmp][i-]][i-];
for(int i = head[tmp]; i != -;i = edge[i].next)
{
int v = edge[i].to;
if(v == fa[tmp][])continue;
deg[v] = deg[tmp] + ;
fa[v][] = tmp;
que.push(v);
}
}
} int LCA(int u,int v)
{
if(deg[u] > deg[v])swap(u,v);
int hu = deg[u], hv = deg[v];
int tu = u, tv = v;
for(int det = hv-hu, i = ; det ;det>>=, i++)
if(det&)
tv = fa[tv][i];
if(tu == tv)return tu;
for(int i = DEG-; i >= ; i--)
{
if(fa[tu][i] == fa[tv][i])
continue;
tu = fa[tu][i];
tv = fa[tv][i];
}
return fa[tu][];
} #define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1 void Up( int rt ) {
lca[rt] = LCA( lca[lr] , lca[rr] ) ;
} void build( int l , int r , int rt ) {
if( l == r ) {
lca[rt] = l ;
return ;
}
int mid = (l+r) >> ;
build(lson),build(rson);
Up(rt);
} int query( int l , int r , int rt , int L , int R ) {
if( L == l && r == R ) return lca[rt] ;
int mid = (l+r) >> ;
if( R <= mid ) return query( lson , L , R ) ;
else if( L > mid ) return query( rson , L , R ) ;
else return LCA( query( lson , L , mid ) , query( rson , mid + , R ) ) ;
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
while( ~scanf("%d",&n) ) {
init();
for( int i = ; i < n ; ++i ) {
int u , v ; scanf("%d%d",&u,&v);
addedge( u , v ) ;
addedge( v , u ) ;
}
BFS();
build(root) ;
int q ; scanf("%d",&q);
while( q-- ) {
int x , y ; scanf("%d%d",&x,&y);
printf("%d\n",query(root,x,y));
}
}
return ;
}

HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )的更多相关文章

  1. HDU 5266 pog loves szh III (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...

  2. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  3. HDU 5266 pog loves szh III(区间LCA)

    题目链接 pog loves szh III 题意就是  求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...

  4. HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)

    题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...

  5. 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 ...

  6. HDU 5266 pog loves szh III

    题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 11.关于django的content_type表

    ****** Django的contenttype表中存放发的是app名称和模型的对应关系 contentType使用方式 - 导入模块 from django.contrib.contenttype ...

  2. ETL工具之——kettle使用简介

    ETL工具之——kettle使用简介 https://yq.aliyun.com/articles/157977?spm=5176.10695662.1996646101.searchclickres ...

  3. node 的CommonJS的引入

    1.CommonJS 每个文件是一个模块,有自己的作用域 引入:

  4. Linux入门培训教程 linux网络编程socket介绍

    一.概念介绍 网络程序分为服务端程序和客户端程序.服务端即提供服务的一方,客户端为请求服务的一方.但实际情况是有些程序的客户端.服务器端角色不是这么明显,即互为Linux培训 客户端和服务端. 我们编 ...

  5. ExcelUtils

    本ExcelUtils工具类是用poi写的,仅用于线下从excel文件中读取数据.如果生产环境要用的话,建议切换到阿里的easyexcel. 引入poi.jar: <!-- https://mv ...

  6. java文件断点续传的简单实现

    一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...

  7. JUnit——运行多个测试方法

    TDD=Test Drive Development   在实际应用中作用特别大,因为我们会定义很多的类和方法,也会存在很多的更新操作,这样如果用main函数进行测试,则会异常麻烦. 而如果用这种多个 ...

  8. 51nod 1228 序列求和(伯努利数)

    1228 序列求和  题目来源: HackerRank 基准时间限制:3 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 T(n) = n^k,S(n) = T(1 ...

  9. (65)CRC32校验C语言版本

    #include<iostream> # include <stdio.h> # include <string.h> typedef unsigned int u ...

  10. [CSP-S模拟测试]:最小距离(最短路)

    题目传送门(内部题97) 输入格式 第一行三个整数$n,m,p$,第二行$p$个整数$x_1\sim x_p$表示特殊点的编号.接下来$m$行每行三个整数$u,v,w$表示一条连接$u$和$v$,长度 ...