hdu 5266 pog loves szh III(lca + 线段树)
Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Description
Hint : You should be careful about stack overflow !
Input
The following line contains ans integers,n (2 \leq n \leq 300000).
AT The following n-1 line, two integers are b_i and c_i at every line, it shows an edge connecting b_i and c_i.
The following line contains ans integers,Q (Q \leq 300000).
AT The following Q line contains two integers li and ri(1 \leq li \leq ri \leq n).
Output
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
/*
hdu 5266 pog loves szh III(lca + 线段树) problem:
给你一棵树,然后查询节点l~r的最小公共祖先 solve:
如果用在线算法,查询的时候可以直接O(1)实现,然后查询节点l~r的最小公共祖先感觉很像区间最值
而且可以发现 如果知道a~b和c~d的最小公共祖先,那么a~d的lca 就是a~b的lca和c~d的lca的最小公共祖先
所以考虑用线段树解决查询问题 hhh-2016-08-08 16:58:09
*/
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
typedef long long ll;
const int maxn=300000 + 500;
const int INF=0x3f3f3f3f;
const int mod = 1e9+7;
int n,tot,cnt;
int head[maxn],rmq[maxn];
int flag[maxn];
int vis[maxn];
int P[maxn];
int F[maxn<<1]; struct Edge
{
int from,to,next;
} edge[maxn << 1]; int fin(int x)
{
if(F[x] == -1) return x;
return F[x] = fin(F[x]);
} void unio(int a,int b)
{
int ta= fin(a);
int tb= fin(b);
if(ta != tb)
F[ta] = tb;
} void add_edge(int u,int v)
{
edge[tot].from = u,edge[tot].to = v,edge[tot].next=head[u],head[u] = tot++;
} struct ST
{
int m[maxn << 1];
int dp[maxn << 1][20];
void ini(int n)
{
m[0] = -1;
for(int i = 1; i <= n; i++)
{
m[i] = ((i&(i-1)) == 0)? m[i-1]+1:m[i-1];
dp[i][0] = i;
}
for(int j = 1; j <= m[n]; j++)
{
for(int i = 1; i+(1<<j)-1 <= n; i++)
dp[i][j] = rmq[dp[i][j-1]] < rmq[dp[i+(1<<(j-1))][j-1]] ?
dp[i][j-1] : dp[i+(1 << (j-1))][j-1];
}
}
int query(int a,int b)
{
if(a > b)
swap(a,b);
int k = m[b-a+1];
return rmq[dp[a][k]] <= rmq[dp[b-(1<<k)+1][k]] ?
dp[a][k]:dp[b-(1<<k)+1][k];
}
}; ST st; void dfs(int u,int pre,int dep)
{
F[++cnt] = u;
rmq[cnt] = dep;
P[u] = cnt; for(int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if(v == pre)
continue;
dfs(v,u,dep+1);
F[++cnt] = u;
rmq[cnt] = dep;
}
} int query_lca(int a,int b)
{
return F[st.query(P[a],P[b])];
}
void ini()
{
memset(flag,0,sizeof(flag));
memset(head,-1,sizeof(head));
tot =0;
cnt = 0;
} struct node
{
int l,r;
int lca;
int mid()
{
return (l+r)>>1;
}
} tree[maxn << 2]; void push_up(int i)
{
tree[i].lca = query_lca(tree[lson].lca,tree[rson].lca);
// cout << tree[lson].lca << " " <<tree[rson].lca <<endl;
// cout << tree[i].l<< " " << tree[i].r << " " <<tree[i].lca <<endl;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
if(l == r)
{
tree[i].lca = l;
// cout << tree[i].l<< " " << tree[i].r << " " <<tree[i].lca <<endl;
return ;
}
int mid = tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
push_up(i);
} int query(int i,int l,int r)
{
if(tree[i].l >= l && tree[i].r <= r)
{
return tree[i].lca;
}
int mid = tree[i].mid();
if(r <= mid)
return query(lson,l,r);
else if(l > mid)
return query(rson,l,r);
else
return query_lca(query(lson,l,mid),query(rson,mid+1,r));
push_up(i);
} int main()
{
int n,m,k;
int a,b,c;
// freopen("in.txt","r",stdin);
while(scanf("%d",&n) != EOF)
{
ini(); for(int i = 1; i < n; i++)
{
scanf("%d%d",&a,&b);
add_edge(b,a);
add_edge(a,b);
flag[b] = 1;
}
dfs(1,1,0);
st.ini(2*n-1);
scanf("%d",&m);
build(1,1,n);
// printf("1 2 %d\n",query_lca(1,2));
for(int i = 1; i <= m; i++)
{
scanf("%d%d",&a,&b);
printf("%d\n",query(1,a,b));
//printf("%d\n",query_lca(a,b));
}
}
return 0;
}
hdu 5266 pog loves szh III(lca + 线段树)的更多相关文章
- 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/Oth ...
- 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)
题目链接 pog loves szh III 题意就是 求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...
- 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 (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- HDU 5266 pog loves szh III
题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...
- HDU5266---pog loves szh III (线段树+LCA)
题意:N个点的有向树, Q次询问, 每次询问区间[L, R]内所有点的LCA. 大致做法:线段树每个点保存它的孩子的LCA值, 对于每一次询问只需要 在线段树查询即可. #include <bi ...
- 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 ...
随机推荐
- 《Language Implementation Patterns》之 符号表
前面的章节我们学会了如何解析语言.构建AST,如何访问重写AST,有了这些基础,我们可以开始进行"语义分析"了. 在分析语义的一个基本方面是要追踪"符号",符号 ...
- equalsignorecase 和equals的区别
equals方法来自于Object类equalsIgnoreCase方法来自String类equals对象参数是Object 用于比较两个对象是否相等equals在Object类中方法默然比较对象内存 ...
- Android webview Mixed Content无法显示图片解决
转自:http://blog.csdn.net/crazy_zihao/article/details/51557425 前言 在使用WebView加载https资源文件时,如果认证证书不被Andro ...
- 调用WCF时,调用已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定。
解决方案: 其实只要在客户端配置文件中加上如下紫色粗体属性( maxReceivedMessageSize): <?xml version="1.0" encoding=&q ...
- java的<<左移,>>右移,>>>无符号右移
>>右移 右移,道在二进制中,假设用一个32位的Int表示一个64,那么高位就都是0,所以当我们把整个二进制数右移,如0100000 >> 2 = 0001000,可以看到右移 ...
- 从PRISM开始学WPF(九)交互(完结)
0x07交互 Notification xaml: <Window x:Class="UsingPopupWindowAction.Views.MainWindow" xml ...
- SQL Server(MySql)中的联合主键(联合索引) 索引分析
最近有人问到这个问题,之前也一直没有深究联合索引具体使用逻辑,查阅多篇文章,并经过测试,得出一些结论 测试环境:SQL Server 2008 R2 测试结果与MySql联合索引查询机制类似,可以认为 ...
- SpringCloud的Bus(一)消息中间件的概念和用途
一.概念与定义 1.Message Broker Message Broker是一种消息验证.消息转换.消息路由的架构模式,用于如: 消息路由到一个或多个目的地 消息转化为其他的表现方式 执行消息的聚 ...
- api-gateway实践(08)新服务网关 - 云端发布和日志查看
一.发布应用 1.新建应用空间 1.1.新建应用空间 1.2.新建应用 1.3.上传程序包 2.创建应用引擎服务 3.发布应用 3.1.为应用容器绑定Web运行环境(应用引擎服务) 3.2.发布应用( ...
- mongodb 索引的基本命令
mongodb的索引: 在数据量超大的时候,能够极大的增快查询速率,但是会降低更新效率.建立索引: db.集合.ensureIndex({属性:1}) //1代表升序 -1代表降序 db.集合.ens ...