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. mven pom.xml Overriding managed version 问题解决详解

    问题原因:在于默认的parent中的版本springboot有固定的指定 删除指定版本 <dependency> <groupId>junit</groupId> ...

  2. js怎么上传文件夹

    1 背景 用户本地有一份txt或者csv文件,无论是从业务数据库导出.还是其他途径获取,当需要使用蚂蚁的大数据分析工具进行数据加工.挖掘和共创应用的时候,首先要将本地文件上传至ODPS,普通的小文件通 ...

  3. 配置文件:android:inputType参数类型说明

    输入字符 android:inputType="none"  --输入普通字符  android:inputType="text" --输入普通字符  andr ...

  4. Spring Boot教程(十四)快速入门

    快速入门 本章主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单.开发快速的特性. 系统要求: ...

  5. windows powershell的常用命令

    cmd开启3389 如何用CMD开启3389与查看3389端口 开启 REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal /f 查端口 net ...

  6. spring boot: Whitelabel Error Page的解决方案

    http://blog.csdn.net/u014788227/article/details/53670112

  7. Mac OS下Flutter环境搭建记录,VS Code开发

    安装Flutter 获取FlutterSDK 终端cd进入SDK安装目录,比如 cd ~/FlutterSDK 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时 ...

  8. c++11多线程---线程操作

    1.等待线程执行完成 join() 方法数会阻塞主线程直到目标线程调用完毕,即join会直接执行该子线程的函数体部分. 2.暂停线程(线程休眠) 使用std::this_thread::sleep_f ...

  9. fedora14 - 22安装yum源的最终所有唯一文档

    yum的配置包括3个地方 /etc/yum中主要是yum的插件: /etc/yum/pluginconf.d 目录下配置yum的插件的启用或禁用等... /etc/yum.conf这个是yum的主要配 ...

  10. 阶段3 1.Mybatis_09.Mybatis的多表操作_2 完成account表的建立及实现单表查询

    mybatis中的多表查询:         示例:用户和账户             一个用户可以有多个账户             一个账户只能属于一个用户(多个账户也可以属于同一个用户)    ...