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. 8. ClustrixDB 监控

    一. 列出集群中当前会话 sql> select * from system.sessions\G 二. 显示CPU利用率.磁盘读/写利用率和缓冲区缓存失误率 MySQL [system]> ...

  2. 更改pip源地址为阿里云

    1.在用户名目录创建pip目录,在pip目录下创建pip.ini. 2.pip.ini中输入: [global] index-url = http://mirrors.aliyun.com/pypi/ ...

  3. (48)LINUX应用编程和网络编程之三Linux获取系统信息

    3.3.1.关于时间的概念 3.3.1.1.GMT时间 (1)GMT是格林尼治时间,也就是格林尼治地区的当地之间. (2)GMT时间的意义?[用格林尼治的当地时间作为全球国际时间],用以描述全球性的事 ...

  4. yield return的使用。。。

    因为要取两个集合不同的元素,所以写了个拓展方法,用到了yield这个关键字,然后就学习了一波.先上代码 public static IEnumerable<T> NoRetainAll&l ...

  5. lombok效率神奇使用

    Lombok效率神器 标签(空格分隔): Java Lombok简介及使用 Lombok 是一种 Java实用工具,可用来帮助开发人员消除Java的冗长,尤其是对于简单的Java对象(POJO), 它 ...

  6. C++引用与传参

    # include <iostream> using namespace std; void Swap(int *pa, int *pb) { int t = *pa; *pa = *pb ...

  7. 一、Robotframework安装步骤

    1.安装python并验证安装成功 a.安装python-2.7.14.amd64------默认路径安装即可 b.添加环境变量path:C:\Python27; C:\Python27\Script ...

  8. 平时碰到系统CPU飙高和频繁GC,你会怎么排查?

    处理过线上问题的同学基本上都会遇到系统突然运行缓慢,CPU 100%,以及Full GC次数过多的问题.当然,这些问题的最终导致的直观现象就是系统运行缓慢,并且有大量的报警.本文主要针对系统运行缓慢这 ...

  9. JAVA中对null进行强制类型转换(null可以强转为任意对象,并执行对象的静态方法)

    今天很好奇,对null进行强转会不会抛错.做了如下测试得到的结果是, 如果把null强转给对象,是不会抛异常的,本身对象是可以为null的. 但是如果是基本类型,比如 int i = (Integer ...

  10. Python基本语法_集合set/frozenset_内建方法详解

    目录 目录 前言 软件环境 可变集合Set set函数创建集合 创建空集合 集合元素的唯一性 集合推导式 set类型对象的内置方法 add增加一个元素 remove删除一个元素 pop随机删除并返回一 ...