主席树+LCA【p2633 (bzoj2588】 Count on a tree
Description
给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。
Input
第一行两个整数N,M。
第二行有N个整数,其中第i个整数表示点i的权值。
后面N-1行每行两个整数(x,y),表示点x到点y有一条边。
最后M行每行两个整数(u,v,k),表示一组询问。
Output
M行,表示每个询问的答案。
虽然能看出来是一个树上套主席树的板子题.但是不知道公式的话,真的难写此题。
树上第\(k\)大问题与序列上的第\(k\)大问题不同.
树上第\(k\)大问题是在父亲节点的基础上新建树,而序列上的第\(k\)大问题则是在上一位置的基础上建树.
这里直接放公式(我也没搞清楚.qwq)
\]
因此直接在\(dfs\)的时候建树,并顺便预处理出来我们的倍增数组即可.
刚开始还以为要树剖套主席树,显然对我来说不可做 qwq.
代码
#include<cstdio>
#include<cctype>
#include<iostream>
#include<cstring>
#include<algorithm>
#define N 100008
#define R register
using namespace std;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int n,m,new_n=1,head[N],tot;
int a[N],b[N],cnt,ans;
int root[N*35],lson[N*35],rson[N*35],sum[N*35];
struct cod{int u,v;}edge[N*2];
inline void add(int x,int y)
{
edge[++tot].u=head[x];
edge[tot].v=y;
head[x]=tot;
}
void build(int lastroot,int &nowroot,int l,int r,int pos)
{
nowroot=++cnt;
sum[nowroot]=sum[lastroot]+1;
lson[nowroot]=lson[lastroot];
rson[nowroot]=rson[lastroot];
if(l==r)return;
int mid=(l+r)>>1;
if(pos<=mid)build(lson[lastroot],lson[nowroot],l,mid,pos);
else build(rson[lastroot],rson[nowroot],mid+1,r,pos);
}
int query(int x,int y,int la,int lca_fa,int l,int r,int k)
{
if(l>=r)return l;
int tmp=sum[lson[x]]+sum[lson[y]]-sum[lson[la]]-sum[lson[lca_fa]];
int mid=(l+r)>>1;
if(tmp>=k) return query(lson[x],lson[y],lson[la],lson[lca_fa],l,mid,k);
else return query(rson[x],rson[y],rson[la],rson[lca_fa],mid+1,r,k-tmp);
}
int depth[N],fath[N][21];
void dfs1(int u,int fa)
{
depth[u]=depth[fa]+1;
build(root[fa],root[u],1,new_n,a[u]);
fath[u][0]=fa;
for(R int i=1;(1<<i)<=depth[u];i++)
fath[u][i]=fath[fath[u][i-1]][i-1];
for(R int i=head[u];i;i=edge[i].u)
{
if(edge[i].v==fa)continue;
dfs1(edge[i].v,u);
}
}
inline int lca(int x,int y)
{
if(depth[x]>depth[y])swap(x,y);
for(R int i=17;i>=0;i--)
if(depth[x]+(1<<i)<=depth[y])
y=fath[y][i];
if(x==y)return y;
for(R int i=17;i>=0;i--)
{
if(fath[x][i]==fath[y][i])continue;
x=fath[x][i],y=fath[y][i];
}
return fath[x][0];
}
int main()
{
in(n),in(m);
for(R int i=1;i<=n;i++)in(a[i]),b[i]=a[i];
sort(b+1,b+n+1);
for(R int i=2;i<=n;i++)
if(b[new_n]!=b[i])
b[++new_n]=b[i];
for(R int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+new_n+1,a[i])-b;
for(R int i=1,x,y;i<n;i++)
{
in(x),in(y);
add(x,y),add(y,x);
}
dfs1(1,0);
for(R int i=1,x,y,k,la;i<=m;i++)
{
in(x),in(y),in(k);
x^=ans;la=lca(x,y);
ans=b[query(root[x],root[y],root[la],root[fath[la][0]],1,new_n,k)];
printf("%d\n",ans);
}
}
主席树+LCA【p2633 (bzoj2588】 Count on a tree的更多相关文章
- 主席树+dfs SPOJ BZOJ2588 Count on a tree
这道题我由于智障错误导致一直错. 在树上建主席树,加上lca思想,很简单. #include<bits/stdc++.h> using namespace std; ; struct no ...
- 洛谷P2633/bzoj2588 Count on a tree (主席树)
洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...
- Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)
Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...
- 【bzoj2588/P2633】count on a tree —— LCA + 主席树
(以下是luogu题面) 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问 ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- [bzoj2588][count on a tree] (主席树+lca)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- [Bzoj2588]Count on a tree(主席树+LCA)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- [BZOJ2588]Count on a tree(LCA+主席树)
题面 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个询问 ...
- 【BZOJ2588】Count on a tree 题解(主席树+LCA)
前言:其实就是主席树板子啦……只不过变成了树上的查询 -------------------------- 题目链接 题目大意:求树上$u$到$v$路径第$k$大数. 查询静态区间第$k$大肯定是用主 ...
随机推荐
- 【题解】SCOI2009围豆豆
很久之前就很想做的一道题,一直思考到今天才下定决心看题解.这道题中,很关键的一点就在于:如何判断一个点是否在一个多边形内?其实如果计算几何基本功扎实的话,应该是可以很快给出答案的(可惜我完全不行):由 ...
- Laravel中Redis的使用
安装 laravel中使用redis首先需要你通过 Composer 安装 predis/predis 包: composer require predis/predis 配置 redis的配置文件是 ...
- Jlink下载u-boot
由于各种原因我的fl2440无启动代码,无任何程序,这时要通过jlink来烧录相关boot程序. 准备工作: 1.Jlink使用jlink commander工具 2.初始化sdram的程序boot. ...
- win10 update orchestratorservere禁用
1 Windows 10系统中有一项Update Orchestrator Service(更新协调器办事),在当地办事窗口中,我们发现 Update Orchestrator Service 办 ...
- [hdu 4417]树状数组+离散化+离线处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 把数字离散化,一个查询拆成两个查询,每次查询一个前缀的和.主要问题是这个数组是静态的,如果带修改 ...
- [poj 2274]后缀数组+最长公共子串
题目链接:http://poj.org/problem?id=2774 后缀数组真的太强大了,原本dp是O(nm)的复杂度,在这里只需要O(n+m). 做法:将两个串中间夹一个未出现过的字符接起来,然 ...
- HDU3829:Cat VS Dog(最大独立集)
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...
- java基础学习(一)hashcode
hashcode的作用 hashCode()方法是从Object类继承过来的,Object类中的hashCode()方法返回的是对象在内存中地址转换成的int值,如果对象没有重写hashCode()方 ...
- 网络流专题练习Day2
04/17 目前做了:题 由于目前六道都是1A感觉非常爽... BZOJ1412: [ZJOI2009]狼和羊的故事 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向 ...