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的过程中建树 然后就直接查 ...
随机推荐
- Thinking-Bear magic (计算几何)
---- 点我 ---- 题目大意: 给你一个正n边形及边长 a和一个正整数L, 求正多边形的面积s,若s大于L,则连接相邻两边的中点,形成新的正多边形,重复这个操作直至s小于L:如图: 正多边形的面 ...
- mybatis oracle -批量插入,存在则更新
<insert id="batchUpdatePBWUserInfo" parameterType="java.util.List"> MERGE ...
- Python 第四阶段 学习记录之----多线程
多线程 多线程例子, 注释部份即为多线程的使用 #-*- coding: utf-8 -*- # Wind clear raise # 2017/3/5 下午2:34 import socket im ...
- Spark学习之路 (十四)SparkCore的调优之资源调优JVM的GC垃圾收集器
一.概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 MIT 的 Lisp 语言,经过半个多世纪,目前已经十分成熟了. jvm 中,程序计数器.虚拟机栈.本 ...
- gene Ontology (基因本体论)
gene ontology为了查找某个研究领域的相关信息,生物学家往往要花费大量的时间,更糟糕的是,不同的生物学数据库可能会使用不同的术语,好比是一些方言一样,这让信息查找更加麻烦,尤其是使得机器查找 ...
- python 正则re.search
re.search 扫描整个字符串并返回第一个成功的匹配. 上码: import re line = "Cats are smarter than dogs"; searchObj ...
- Linux环境 vi/vim ESC无法退出原因
原因是输入模式是中文的,需要切换成英文半角符号输入命令!
- window下安装cross-env解决NODE_ENV ts-node 不是内部或外部命令,也不是可运行的程序 或批处理文件 问题
window下安装cross-env解决NODE_ENV ts-node 不是内部或外部命令,也不是可运行的程序 或批处理文件 问题 在git bash上启动无法进行调试,采用cross-env后可以 ...
- HTML5 canvas游戏工作原理
HTML5已经不是一个新名词.它看上去很cool,有很多feature,大多数人普遍看好它的发展.对于我来说,最感兴趣的是它的canvas标签,可以结合Javascript来绘制游戏画面. 我们可以在 ...
- js条件判断if-else和switch、循环for和while
条件判断和循环都使用{ }将代码块括起来,如果代码块只有一行,则可省略{ }. 在循环中,continue表示跳过当前循环继续进行下一次循环,break表示跳出整个循环. 1.条件判断if-else, ...