P2633 Count on a tree
思路
运用树上差分的思想,转化成一个普通的主席树模型即可求解
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct Node{
int lson,rson,sz;
}pt[100100*30];
const int MAXlog=19;
int dep[100100],jump[100100][MAXlog],lastans=0,n,m,u[100100<<1],Nodecnt,v[100100<<1],w_p[100100],ax[100100],nx,fir[100100],nxt[100100<<1],cnt,root[100100];
void addedge(int ui,int vi){
++cnt;
u[cnt]=ui;
v[cnt]=vi;
nxt[cnt]=fir[ui];
fir[ui]=cnt;
}
void insert(int L,int R,int pos,int &o){
pt[++Nodecnt]=pt[o];
o=Nodecnt;
pt[o].sz++;
if(L==R)
return;
int mid=(L+R)>>1;
if(pos<=mid)
insert(L,mid,pos,pt[o].lson);
else
insert(mid+1,R,pos,pt[o].rson);
}
int lca(int x,int y){
if(dep[x]<dep[y])
swap(x,y);
for(int i=MAXlog-1;i>=0;i--)
if(dep[x]-(1<<i)>=dep[y])
x=jump[x][i];
if(x==y)
return x;
for(int i=MAXlog-1;i>=0;i--)
if(jump[x][i]!=jump[y][i])
x=jump[x][i],y=jump[y][i];
return jump[x][0];
}
int query(int L,int R,int k,int rootx,int rooty,int rootlca,int falca){
if(L==R)
return L;
int lch=pt[pt[rootx].lson].sz+pt[pt[rooty].lson].sz-pt[pt[rootlca].lson].sz-pt[pt[falca].lson].sz;
int mid=(L+R)>>1;
if(lch<k)
return query(mid+1,R,k-lch,pt[rootx].rson,pt[rooty].rson,pt[rootlca].rson,pt[falca].rson);
else
return query(L,mid,k,pt[rootx].lson,pt[rooty].lson,pt[rootlca].lson,pt[falca].lson);
}
int query(int u,int v,int k){
u^=lastans;
int Lca=lca(u,v);
return lastans=ax[query(1,n,k,root[u],root[v],root[Lca],root[jump[Lca][0]])];
}
void init(void){
sort(ax+1,ax+n+1);
nx=unique(ax+1,ax+n+1)-(ax+1);
for(int i=1;i<=n;i++)
w_p[i]=lower_bound(ax+1,ax+nx+1,w_p[i])-ax;
}
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
jump[u][0]=fa;
for(int i=1;i<MAXlog;i++)
jump[u][i]=jump[jump[u][i-1]][i-1];
root[u]=root[fa];
insert(1,n,w_p[u],root[u]);
for(int i=fir[u];i;i=nxt[i]){
if(v[i]==fa)
continue;
dfs(v[i],u);
}
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&w_p[i]),ax[i]=w_p[i];
init();
for(int i=1;i<=n-1;i++){
int a,b;
scanf("%d %d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs(1,0);
for(int i=1;i<=m;i++){
int ux,vx,kx;
scanf("%d %d %d",&ux,&vx,&kx);
printf("%d\n",query(ux,vx,kx));
}
return 0;
}
P2633 Count on a tree的更多相关文章
- [luogu P2633] Count on a tree
[luogu P2633] Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点 ...
- 洛谷 P2633 Count on a tree
P2633 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中last ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- 洛谷P2633 Count on a tree(主席树上树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- 洛谷 P2633 Count on a tree 主席树
在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$ ,建立 $i$ 到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...
- 洛谷 P2633 Count on a tree 题解
题面 对于每个点建立一颗主席树: 然后按照树上差分的思想统计主席树的前缀和: lca+主席树+前向星存表就可以了: #include <bits/stdc++.h> #define inc ...
- 洛谷P2633 Count on a tree(主席树,倍增LCA)
洛谷题目传送门 题目大意 就是给你一棵树,每个点都有点权,每次任意询问两点间路径上点权第k小的值(强制在线). 思路分析 第k小......又是主席树了.但这次变成树了,无法直接维护前缀和. 又是树上 ...
- ☆ [洛谷P2633] Count on a tree 「树上主席树」
题目类型:主席树+\(LCA\) 传送门:>Here< 题意:给出一棵树.每个节点有点权.问某一条路径上排名第\(K\)小的点权是多少 解题思路 类似区间第\(K\)小,但放在了树上. 考 ...
- 洛谷P2633 Count on a tree 主席树
传送门:主席树 解题报告: 传送门! umm这题我还麻油开始做 所以 先瞎扯一波我的想法,如果错了我就当反面教材解释这种典型错误,对了我就不管了QwQ 就直接dfs,在dfs的过程中建树 然后就直接查 ...
随机推荐
- STL算法中函数对象和谓词
函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特征,就是通过“对象名+(参数列 ...
- HighCharts插件学习(二)
HighCharts属性 credits: {版权信息} data: {数据功能模块} drilldown: {钻取} exporting: {导出} labels: {标签} legend: ...
- Linux 进程间通讯
一.Linux 下进程间通讯方式 1)管道(Pipe)及有名管道(named pipe): 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- 蒙特卡洛(Monte Carlo)法求定积分
https://blog.csdn.net/baimafujinji/article/details/53869358
- 深入理解Node.js基于事件驱动的回调
回调和异步调用的关系 首先明确一点,回调并非是异步调用,回调是一种解决异步函数执行结果的处理方法.在异步调用,如果我们希望将执行的结果返回并且处理时,可以通过回调的方法解决.为了能够更好的区分回调和异 ...
- 1069: 统计字符gets函数
题目描述 编制程序,统计文本stdin中字符$出现的次数,并将结果写入文件stdout 输入 字符文本 输出 $次数 样例输入 as$dfkjhkjkjdhf asdfkj$lskdfj werijw ...
- 利用vue写一个复选框的组件
HTML <multicheck :source=tlist :busValue='objInfo.tt' @getTt="getTtInfo"></multic ...
- spring框架入门之一
一.什么是Spring框架 1.什么是Spring Spring框架是个一个全栈的框架.意思就是使用Spring的框架可以开发web层,service层还有dao层. 本质:Spring就是一个对象的 ...
- mycat的下载和安装
1.下载. 网址:http://dl.mycat.io/ 2.安装. 解压:tar zxf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz ...
- 基于 SSL 的 Nginx 反向代理
基于 SSL 的 Nginx 反向代理 描述: 线上zabbix因机房网络问题,外网接口无法对外访问,因此采用同机房的另外一台服务器做反向代理. 线上用于zabbix提供web访问的Nginx,采用h ...