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 ...
随机推荐
- Houdini技术体系大纲
Houdini for UE4 Pipeline的系列教程,前言等想好再写吧
- SpringMVC工作原理 : HandlerMapping和HandlerAdapter
一.HandlerMapping 作用是根据当前请求的找到对应的 Handler,并将 Handler(执行程序)与一堆 HandlerInterceptor(拦截器)封装到 HandlerExecu ...
- PHP 通过构造器进行依赖注入 demo
class A{ public $b; public $f; function __construct( B $b , $f = 1 ){ $this->b = $b; $this->f ...
- C++将时间格式转换成秒数
#include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> ...
- java 的访问权限控制
package test06; public class PermissionModel { private int age; public String name; public int getAg ...
- nginx_ssl_tomcat配置
<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" ...
- [Manthan, Codefest 18][Codeforces 1037F. Maximum Reduction]
题目链接:1037F - Maximum Reduction 题目大意:给出一段代码,给你一个长度为n的数组和数字k,求程序运行结果,mod 1e9+7输出 简单翻译下代码的意思,初始定义一个空数组b ...
- 关于 systemctl --user status 报错的问题
关于 systemctl --user enable mpd 报错: Failed to connect to bus: No such file or directory 因为arch脚本中,sys ...
- cmder 常用配置(包括默认管理员运行和解决中文乱码)
简介 cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令. 下载 官网地址:http://cmder.net/ 下载的时候,会有 ...
- 用SQL快速删除U8账套
一.问题提出 通过"系统管理"来删除999账套,首先要求你备份然后才能删除.头痛的是: 1)备份需要发费很长的时间,特别是账套数据文件比较大时. 2)备份时,你的本本基本处于死机状 ...