思路

运用树上差分的思想,转化成一个普通的主席树模型即可求解

代码

#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的更多相关文章

  1. [luogu P2633] Count on a tree

    [luogu P2633] Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点 ...

  2. 洛谷 P2633 Count on a tree

    P2633 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中last ...

  3. 主席树 || 可持久化线段树 || 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) ...

  4. 洛谷P2633 Count on a tree(主席树上树)

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  5. 洛谷 P2633 Count on a tree 主席树

    在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$  ,建立 $i$  到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...

  6. 洛谷 P2633 Count on a tree 题解

    题面 对于每个点建立一颗主席树: 然后按照树上差分的思想统计主席树的前缀和: lca+主席树+前向星存表就可以了: #include <bits/stdc++.h> #define inc ...

  7. 洛谷P2633 Count on a tree(主席树,倍增LCA)

    洛谷题目传送门 题目大意 就是给你一棵树,每个点都有点权,每次任意询问两点间路径上点权第k小的值(强制在线). 思路分析 第k小......又是主席树了.但这次变成树了,无法直接维护前缀和. 又是树上 ...

  8. ☆ [洛谷P2633] Count on a tree 「树上主席树」

    题目类型:主席树+\(LCA\) 传送门:>Here< 题意:给出一棵树.每个节点有点权.问某一条路径上排名第\(K\)小的点权是多少 解题思路 类似区间第\(K\)小,但放在了树上. 考 ...

  9. 洛谷P2633 Count on a tree 主席树

    传送门:主席树 解题报告: 传送门! umm这题我还麻油开始做 所以 先瞎扯一波我的想法,如果错了我就当反面教材解释这种典型错误,对了我就不管了QwQ 就直接dfs,在dfs的过程中建树 然后就直接查 ...

随机推荐

  1. GCD(莫比乌斯+去重)

    题目链接 莫比乌斯反演模板题, 去重即可: 我们可以发现只有在区间重叠部分才会有重复且为cal(e, e, k)/2;(e表示b, d中较小的一个): #include<cstdio> # ...

  2. hdu4784

    题意: 给了一个图 从1号节点走到N号节点,然后,每个地方有买卖盐的差价,然后求 到达N的最大价值,一旦走到N这个点就不能再走了,或者走到不能再别的世界走1和N这两个点,然后接下来 用一个 四维的数组 ...

  3. 多线程实现Thread.Start()与ThreadPool.QueueUserWorkItem两种方式对比

    Thread.Start(),ThreadPool.QueueUserWorkItem都是在实现多线程并行编程时常用的方法.两种方式有何异同点,而又该如何取舍? 写一个Demo,分别用两种方式实现.观 ...

  4. Necklace (全排列 + 匈牙利)

    #include<bits/stdc++.h> using namespace std; ][], Gra[][]; ]; ]; ]; bool dfs(int u, int vN) { ...

  5. uva 10163 Storage Keepers

    题意: 有n个仓库,m个人,一个仓库只能由一个人托管,每个人可以托管多个仓库. 每个人有一个能力值a,如果说他托管了k个仓库,那么这些仓库的安全值都是a/k. 雇佣一个人的花费也是a. 如果一个仓库没 ...

  6. centos 安装arcgis server 10.1

    1.创建新用户,不要在root下面直接安装 [root@localhost ~]# groupadd esri //创建esri组 [root@localhost ~]# useradd ags -g ...

  7. git 新建本地分支后将本地分支推送到远程库, 使用git pull 或者 git push 的时候报错

    是因为本地分支和远程分支没有建立联系  (使用git branch -vv  可以查看本地分支和远程分支的关联关系)  .根据命令行提示只需要执行以下命令即可git branch --set-upst ...

  8. The Little Prince-12/07

    The Little Prince-12/07 "My little man, where do you come from? What is this ‘where I live,‘ of ...

  9. ES6知识整理(3)--函数的扩展

    只有整理过的学习才是有效的学习.也就是学习之后要使用和整理成文,才是真正的学到了... 最近上班有点忙的关系,于是文章更新会慢些.只有晚上加完班之后,空余时间才能学习整理.因此完成一篇也可能要几个晚上 ...

  10. redis-3.2 集群

    目录 简介 集群简介 Redis 集群的数据分片 Redis 集群的主从复制模型 Redis 一致性保证 redis 集群间的通信 环境 安装Ruby 部署 安装Redis略 创建集群 集群节点信息 ...