HDU5266 LCA

Description

给一棵 n 个点的树,Q 个询问 [L,R] : 求点 L , 点 L+1 , 点 L+2 …… 点 R 的 LCA.

Input

多组数据.

The following line contains an 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

solution

这题其实就是求[l,r]区间内的公共lca。

既,

\[ans(l,r)=LCA(a_l,a_{l+1},a_{l+2}\cdots a_r)
\]

这里有一个显而易见的结论

\[LCA(x,y,z)=LCA\bigl(LCA(x,y),z\bigr)=LCA\bigl(LCA(z,y),x\bigr)=LCA\bigl(LCA(z,x),y\bigr)
\]

所以我们在这里考虑建一棵线段树,每次pushup向上更新lca,我们可以用树剖来求lca,这样我们就可以求出区间lca了

这是代码

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
const int maxn=300001,inf=0x7fffffff;
int n,m,tot,root,nxt[maxn<<1],to[maxn<<1],head[maxn],lca[maxn<<2],dep[maxn],siz[maxn],top[maxn],fa[maxn],son[maxn];
bool check[maxn]; void addedge(int x,int y){
nxt[++tot]=head[x];
head[x]=tot;
to[tot]=y;
} void dfs1(int u,int f) {
dep[u]=dep[fa[u]=f]+(siz[u]=1);
for(int i=head[u];i;i=nxt[i]) {
int v=to[i];
if(v==f)continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
} void dfs2(int u,int topf){
top[u]=topf;
if(!son[u])return;
dfs2(son[u],topf);
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(v==fa[u] or v==son[u])continue;
dfs2(v,v);
}
} int Lca(int x,int y) {
register int u=x,v=y;
while(top[u]!=top[v]) {
if(dep[top[u]]<dep[top[v]])swap(u,v);
u=fa[top[u]];
}
return dep[u]<=dep[v]?u:v;
} void pushup(int o){
lca[o]=Lca(lca[o<<1],lca[o<<1|1]);
} void build(int o,int l,int r){
if(l==r){
lca[o]=l;
return;
}
int mid=l+r>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
pushup(o);
} int query(int o,int l,int r,int x,int y){
if(x<=l and r<=y){
return lca[o];
}
int mid=l+r>>1,ans1=-1,ans2=-1;
if(x<=mid)ans1=query(o<<1,l,mid,x,y);
if(mid+1<=y)ans2=query(o<<1|1,mid+1,r,x,y);
if(ans1!=-1 and ans2!=-1)return Lca(ans1,ans2);
if(ans1!=-1)return ans1;
if(ans2!=-1)return ans2;
} int main(){
while(~scanf("%d",&n)){
tot=0;
memset(fa,0,sizeof(fa));
memset(son,0,sizeof(son));
memset(head,0,sizeof(head));
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v),addedge(v,u);
}
dfs1(1,0);
dfs2(1,1);
build(1,1,n);
scanf("%d",&m);
while(m--){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(1,1,n,l,r));
}
}
return 0;
}

HDU5266 LCA 树链剖分LCA 线段树的更多相关文章

  1. bzoj 3626 [LNOI2014]LCA(离线处理+树链剖分,线段树)

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1272  Solved: 451[Submit][Status ...

  2. 【BZOJ5507】[GXOI/GZOI2019]旧词(树链剖分,线段树)

    [BZOJ5507][GXOI/GZOI2019]旧词(树链剖分,线段树) 题面 BZOJ 洛谷 题解 如果\(k=1\)就是链并裸题了... 其实\(k>1\)发现还是可以用类似链并的思想,这 ...

  3. 【洛谷5439】【XR-2】永恒(树链剖分,线段树)

    [洛谷5439][XR-2]永恒(树链剖分,线段树) 题面 洛谷 题解 首先两个点的\(LCP\)就是\(Trie\)树上的\(LCA\)的深度. 考虑一对点的贡献,如果这两个点不具有祖先关系,那么这 ...

  4. bzoj 4034 [HAOI2015] T2(树链剖分,线段树)

    4034: [HAOI2015]T2 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1536  Solved: 508[Submit][Status] ...

  5. bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 10677  Solved: 4313[Submit ...

  6. poj 3237 Tree(树链剖分,线段树)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 7268   Accepted: 1969 Description ...

  7. bzoj 2243 [SDOI2011]染色(树链剖分,线段树)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4637  Solved: 1726[Submit][Status ...

  8. HDU 4366 Successor(树链剖分+zkw线段树+扫描线)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...

  9. 【BZOJ3531】旅行(树链剖分,线段树)

    [BZOJ3531]旅行(树链剖分,线段树) 题面 Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教 ...

  10. [bzoj4196][Noi2015]软件包管理器_树链剖分_线段树

    软件包管理器 bzoj-4196 Noi-2015 题目大意:Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件 ...

随机推荐

  1. robotframework - selenium 分层思路

    前言: 对于每一条用例来说,调用“百度搜索”关键字,输入搜索内容,输入预期结果即可.不同关心用例是如何执行的.如果百度输入框的定位发生了变化,只用去修改“百度搜索”关键字即可,不用对每一条用例做任何修 ...

  2. [NOI2004]cashier 郁闷的出纳员

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...

  3. 题解报告:poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  4. ACM_错排(递推dp)

    RPG的错排 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今年暑假GOJ集训队第一次组成女生队,其中有一队叫RPG,但做为集训 ...

  5. .net引用System.Data.SQLite操作SQLite

    之所以要做这个笔记,是因为在.NET中使用System.Data.SQLite的时候,遇到了些问题,这些问题是相对于引用其他dll没有遇到过的,所以作个笔记,记录一下. 简单起见,首先建立一个控制台项 ...

  6. MongoDB学习笔记~复杂条件拼接和正则的使用

    在大叔lind框架里有日志组件logger,而在日志实现方式上有file,mongodb,sql,json等方式,对分布式日志处理上大叔推荐使用mongodb进行存储,除了它的高效写入,灵活的结构外, ...

  7. 待销售分拣单App数据推送

    管理待分拣商品的App的显示操作

  8. 2105. [NOIP2015] 信息传递

    ★☆   输入文件:2015message.in   输出文件:2015message.out   简单对比 时间限制:1 s   内存限制:256 MB [题目描述] 有n个同学(编号为1到n)正在 ...

  9. PAT 甲级1135. Is It A Red-Black Tree (30)

    链接:1135. Is It A Red-Black Tree (30) 红黑树的性质: (1) Every node is either red or black. (2) The root is ...

  10. tp登陆注册(转)

    登录时,更新用户数据,登录ip和登录时间,以及登录次数+1,此实现方便不知是否合适,待验证.源码地址:https://github.com/grh0812/thinkphp-login-registe ...