acm 2015北京网络赛 F Couple Trees 主席树+树链剖分
题意:给了两棵树,他们的跟都是1,然后询问,u,v 表 示在第一棵树上在u点往根节点走 , 第二棵树在v点往根节点走,然后求他们能到达的最早的那个共同的点
解:
我们将第一棵树进行书链剖,然后第二棵树采用主席树,他的信息来自他的父亲节点,每个点存他在第一棵树 树链剖分后的位置,这样我们每次查询uv的时候我们只要 我们选取u和top[u]这段区间在主席树v这颗树上找,在这个区间能取到的最大值,一旦存在,这个最大值就我们要的,这个点保存着他到根节点这条路上所有点在第一棵树剖分后的位置
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string.h>
using namespace std;
const int maxn=;
const int MMAXN=*;
int H[maxn],to[maxn*],nx[maxn*],numofE;
int son[maxn],depth[maxn],fa[maxn],num[maxn],top[maxn];
int p[maxn],fp[maxn],pos,sizoftree;
int Ls[MMAXN],Rs[MMAXN],Mav[MMAXN],T[maxn],depth2[maxn];
void init(int n)
{
sizoftree=pos=numofE=;
for(int i=; i<=n; i++)H[i]=;
T[]=Ls[]=Rs[]=Mav[]=sizoftree=;
}
void add(int u,int v)
{
numofE++;
to[numofE]=v;
nx[numofE]=H[u];
H[u]=numofE;
} void dfs(int cur, int per, int dep)
{ depth[cur]=dep;
son[cur]=-;
fa[cur]=per;
num[cur]=;
for(int i=H[cur]; i; i=nx[i])
{
int tt=to[i];
if(tt==per)continue;
dfs(tt,cur,dep+);
num[cur]+=num[tt];
if(son[cur]==- || num[ son[cur] ]<num[tt]) son[cur]=tt;
}
}
void finde(int cur, int per, int tp)
{
top[cur]=tp;
pos++;
p[cur]=pos;
fp[pos]=cur;
if(son[cur]!=-) finde(son[cur],cur,tp);
for(int i=H[cur]; i; i=nx[i])
{
int tt=to[i];
if(tt==per||tt==son[cur])continue;
finde(tt,cur,tt);
}
}
void insert(int L, int R, int K,int pre ,int &x)
{
x=++sizoftree;
Ls[x]=Ls[pre];
Rs[x]=Rs[pre];
Mav[x]=max( Mav[pre],K);
if(L==R)return ;
int mid=(L+R)>>;
if(K<=mid)insert(L,mid,K,Ls[pre],Ls[x]);
else insert(mid+,R,K,Rs[pre],Rs[x]);
}
int cL,cR;
int query(int L, int R,int root)
{
if(cL<=L&&R<=cR)return Mav[root];
if(Mav[root]==)return ;
int mid=(L+R)>>;
int a1=,a2=;
if(cL<=mid)a1=query(L,mid,Ls[root]);
if(cR>mid)a2=query(mid+,R,Rs[root]);
return max(a1,a2);
}
int solve(int u, int v,int n)
{
int fu=top[u];
int ret;
while(true)
{
cL=p[fu];cR=p[u];
ret=query(,n,T[v]);
if(ret)break;
u=fa[fu];
fu=top[u];
}
return fp[ret];
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
init(n);
for(int i=; i<=n; i++)
{
int u;
scanf("%d",&u);
add(i,u); add(u,i);
}
dfs(,,);
finde(,,);
depth2[]=;
insert(,n,,T[],T[]);
for(int i=; i<=n; i++)
{
int u;
scanf("%d",&u);
depth2[i]=depth2[u]+;
insert(,n,p[i],T[u],T[i]);
}
int ans=;
for(int i=; i<m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
u=(u+ans)%n+;
v=(v+ans)%n+;
ans=solve(u,v,n);
printf("%d %d %d\n",ans,depth[u]-depth[ans]+,depth2[v]-depth2[ans]+);
} }
return ;
}
acm 2015北京网络赛 F Couple Trees 主席树+树链剖分的更多相关文章
- acm 2015北京网络赛 F Couple Trees 树链剖分+主席树
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
- 2015北京网络赛 F Couple Trees 暴力倍增
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
- (中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。
"Couple Trees" are two trees, a husband tree and a wife tree. They are named because they ...
- Hiho 1232 北京网络赛 F Couple Trees
给两颗标号从1...n的树,保证标号小的点一定在上面.每次询问A树上的x点,和B树上的y点同时向上走,最近的相遇点和x,y到这个点的距离. 比赛的时候想用倍增LCA做,但写渣了....后来看到题解是主 ...
- 2015 北京网络赛 E Border Length hihoCoder 1231 树状数组 (2015-11-05 09:30)
#1231 : Border Length 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Garlic-Counting Chicken is a special spe ...
- 2015北京网络赛 Couple Trees 倍增算法
2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道. 解法来自 q ...
- 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT
2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...
随机推荐
- Elasticsearch学习笔记——分词
1.测试Elasticsearch的分词 Elasticsearch有多种分词器(参考:https://www.jianshu.com/p/d57935ba514b) Set the shape to ...
- Oracle_lhr_CentOS 7.3 ECS上搭建RAC 18c+单实例DG+EMCC+DG的FSFO快速故障转移配置
Oracle_lhr_CentOS 7.3 ECS上搭建RAC 18c+单实例DG+EMCC+DG的FSFO快速故障转移配置 [大型连续免费公开课]Oracle 18c rac+dg+13.3的em ...
- Fiddler抓包手机代理配置
参考链接:https://i.wanz.im/2013/04/30/debugging_http_request_with_fiddler/ http://www.hangge.com/blog/ca ...
- Linux下批量Kill多个进程
ps -ef|grep php|grep -v grep|cut -c 9-15|xargs kill -9 管道符"|"用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令 ...
- Openwrt配置小记
手中有台Netgear WNR2000v3,一直想尝试Openwrt,于是刷机.官方最新固件的稳定版本为15.05.1,该版本自带luci,BUT,不能保存配置,上网查了很久,得出结论,是由于WNR2 ...
- monit介绍和配置
1.介绍 monit监控和管理进程.程序.文件.目录和Unix系统的文件的工具.可以进行自动维护和修理,在错误的情况下执行有意义的因果关系的行动.比如,某个进程没有运行启动它:没有响应重启它:占用太多 ...
- vue filter方法-时间格式化
plugins/filter.js import Vue from 'vue' // 时间格式化 // 用法:<div>{{data | dataFormat('yyyy-MM-dd hh ...
- iOS - 记住用户登录状态保存用户名密码
我们在使用APP时常用的一个功能:用户第一次进入APP时自动进入登录注册页,提示用户注册登录,用户登录成功后才进入主页,再次进入APP时,不用再次登录就直接进到主页了,就算杀掉该APP进程再次进入,依 ...
- Spark连接MongoDB之Scala
MongoDB Connector for Spark Spark Connector Scala Guide spark-shell --jars "mongo-spark-connect ...
- 点击app分享链接,js判断手机是否安装某款app,有就尝试打开,没有就下载
html: <h1 class="downlink"> 前往 </h1> js: document.addEventListener('DOMContent ...