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/Others)
Total Submission(s): 470 Accepted Submission(s): 97
Hint : You should be careful about stack overflow !
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).
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 )的更多相关文章
- HDU 5266 pog loves szh III (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...
- 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)
题目链接 pog loves szh III 题意就是 求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...
- 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 and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...
- HDU 5266 pog loves szh III
题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...
- 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 ...
随机推荐
- python3---99乘法表
参考: http://www.cnblogs.com/suiy-160428/p/5594389.html #!/usr/bin/env pythonfor i in range(1,10): for ...
- node中controller的get和post方法获取参数
1.get: const body = ctx.query; // get请求 2.post: const body = ctx.request.body; // post请求
- jquery gt选择器 语法
jquery gt选择器 语法 作用::gt 选择器选取 index 值高于指定数的元素.index 值从 0 开始.经常与其他元素/选择器一起使用,来选择指定的组中特定序号之后的元素(如上面的例子) ...
- Tarjan算法初步
一.前置知识: 强连通分量:有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(stron ...
- 也谈HTTP协议
HTTP(HyperText Transfer Protocol,超文转移协议,超文本传输协议的译法并不严谨.) 一.网络基础 TCP/IP 1.1 TCP/IP 协议族 TCP/IP 协议族是互联网 ...
- [BZOJ2669][CQOI2012]局部极小值:DP+容斥原理
分析 题目要求有且只有一些位置是局部极小值.有的限制很好处理,但是只有嘛,嗯...... 考虑子集反演(话说这个其实已经算是超集反演了吧还叫子集反演是不是有点不太合适),枚举题目给出位置集合的所有超集 ...
- Jmeter -- 上下文关联(JSON提取器)
目标: 将请求A响应数据的部分内容提取出来,保存成变量供后续请求使用(用在返回格式为json的HTTP请求中) 步骤: 1. 添加JSON Extractor后置处理器 add --> post ...
- 使用mysql以及连接数据库
MySQL Table of Contents 1. 安装与配置 2. 数据库与账户 3. 用户跟权限 4. 常用命令 5. 表的创建 6. 数据类型 7. 主键约束 8. 表的修改 9. 引擎(En ...
- 正则表达式中 group groups区别
先看代码instance: 1 >>> a="123abc456" 2 >>> import re 3 >>> print(r ...
- spring session 加载的时候一些配置问题
启动springboot时候的错误信息: An attempt was made to call the method org.springframework.boot.autoconfigure.s ...