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 ...
随机推荐
- 【java工具类】生成Zip压缩包
多文件生成压缩包,返回压缩包生成位置的路径. FileUtil.java /** * 文件打压缩包 * @param files * @param Name * @return * @throws E ...
- java agent问题
Error occurred during initialization of VMagent library failed to init: instrumentobjc[36987]: Class ...
- sqli-lab(8)
布尔型单引号GET盲注 发现加个单引号跟没加显示不一样,加了单引号连you are in都不显示了,没有报错,所以只能用盲注判断了 0X01爱之先了解 盲注需要掌握一些MySQL的相关函数:lengt ...
- [LeetCode]-DataBase-Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
- springCloud分布式事务实战(一)案例需求及实现步骤
本文不对分布式事务原理进行探索,而是通过一个案例来说明如何使用分布式事务 案例需求:创建2个基于springCloud的微服务,分别访问不同的数据库:然后创建一个整合服务,调用微服务实现数据的保存到2 ...
- 模板引擎ejs的include方法
html无法include header.ejs footer.ejs 最后用 user.ejs在首尾include
- Python 写 ACM 题目的一些技巧
目录 输入输出 input() 输入 split() 用于输入 strip() 输入清理 print() 输入 sort 排序 输入输出 input() 输入 Python3 中 input() 函数 ...
- http://www.malware-traffic-analysis.net/2018/index.html
http://www.malware-traffic-analysis.net/2018/index.html
- win 10 hosts文件不生效
win 10 hosts文件不生效 windows 10 hosts文件修改了,但是怎么都无法在浏览器中进行解析.一直都在等待,直到链接超时. 最后解决办法: 把hosts文件内容复制出来 ...
- 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_3 Mybatis的CRUD-修改和删除操作
增加更新操作 更新用户的配置 测试类 删除的操作 这里的parpameterType值可以是:Integer.INTEGER.int.java.lang.Integer 讲到typeAliases标签 ...