每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]])

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct poi{int size,lt,rt;}tree[maxn*];
struct zs{int too,pre;}e[maxn<<];
int n,m,x,y,z,sz,tot,N;
int d[maxn],f[maxn][],last[maxn],root[maxn],v[maxn],b[maxn],num[maxn];
inline void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
inline void add(int x,int y){e[++tot].too=y;e[tot].pre=last[x];last[x]=tot;}
void insert(int &x,int l,int r,int cx)
{
tree[++sz]=tree[x];tree[sz].size++;x=sz;
if(l==r)return;
int mid=(l+r)>>;
if(cx<=mid)insert(tree[x].lt,l,mid,cx);
else insert(tree[x].rt,mid+,r,cx);
}
int query(int a,int b,int c,int d,int l,int r,int k)
{
if(l==r)return l;
int mid=(l+r)>>;
int t1=tree[a].lt,t2=tree[b].lt,t3=tree[c].lt,t4=tree[d].lt;
int tmp=tree[t1].size+tree[t2].size-tree[t3].size-tree[t4].size;
if(tmp>=k)return query(t1,t2,t3,t4,l,mid,k);
return query(tree[a].rt,tree[b].rt,tree[c].rt,tree[d].rt,mid+,r,k-tmp);
}
void dfs(int x,int fa)
{
root[x]=root[fa];insert(root[x],,N,v[x]);
d[x]=d[fa]+;f[x][]=fa;
for(int i=last[x];i;i=e[i].pre)
if(e[i].too!=fa)dfs(e[i].too,x);
}
inline int lca(int x,int y)
{
if(d[x]<d[y])swap(x,y);
for(int i=;i>=;i--)
if(d[f[x][i]]>=d[y])x=f[x][i];
if(x==y)return x;
for(int i=;i>=;i--)
if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
return f[x][];
}
int main()
{
read(n);read(m);
for(int i=;i<=n;i++)read(v[i]),b[i]=v[i];N=n;
sort(b+,b++N);N=unique(b+,b++N)-b-;
for(int i=;i<=n;i++)x=lower_bound(b+,b++N,v[i])-b,num[x]=v[i],v[i]=x;
for(int i=;i<n;i++)
read(x),read(y),add(x,y),add(y,x);
dfs(,);for(int j=;j<;j++)for(int i=;i<=n;i++)f[i][j]=f[f[i][j-]][j-];
int lan=;
for(int i=;i<=m;i++)
{
read(x);read(y);read(z);x^=lan;int fq=lca(x,y);
printf("%d",lan=num[query(root[x],root[y],root[fq],root[f[fq][]],,N,z)]);
if(i!=m)puts("");
}
}

bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)的更多相关文章

  1. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  2. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  3. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  4. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  5. Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)

    题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...

  6. spoj COT - Count on a tree (树上第K小 LCA+主席树)

    链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...

  7. BZOJ2588: Spoj 10628. Count on a tree

    传送门 刚开始看错题以为是dfs序瞎搞.. 后来看清题了开始想用树剖瞎搞... 感觉要滚粗啊.. 对于每个点到根的路径建立线段树,暴力建MLE没跑,上主席树,然后$(x,y)$的路径就可以先求出来$L ...

  8. 【主席树】bzoj2588 Spoj 10628. Count on a tree

    每个点的主席树的root是从其父转移来的.询问的时候用U+V-LCA-FA(LCA)即可. #include<cstdio> #include<algorithm> using ...

  9. 主席树初探--BZOJ2588: Spoj 10628. Count on a tree

    n<=100000的点权树,有m<=100000个询问,每次问两个点间的第k小点权,保证有解,强制在线. 主席上树啦!类似于之前的序列不带修改询问的前缀表示法,现在只要把前缀当成某点到根的 ...

随机推荐

  1. uvaoj455Periodic Strings(枚举)

    A character string is said to have period k if it can be formed by concatenating one or more repetit ...

  2. 网站端测试常见BUG

    1.翻页 翻页时,没有加载数据为空,第二页数据没有请求 翻页时,重复请求第一页的数据 翻页时,没有图片的内容有时候会引用有图片的内容 2.图片数据为空 图片数据为空时,会保留为空的图片数据位置 3.链 ...

  3. linux部署maven

    1.下载安装包 https://maven.apache.org/download.cgi 2.解压,并配置环境变量 vim /etc/profile export MAVEN_HOME=maven目 ...

  4. 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域

    此篇文章是对上一篇文章(http://www.ifiero.com/index.php/archives/611)的进一步补充,主要说明如何适配Apple的最新三款手机iPhoneXs.iPhoneX ...

  5. Linux的基础预备知识

       Linux下一切皆文件 1.root@mk-virtual-machine:/home/mk#   root:该位置表示当前终端登录的用户名 mk-virtual-machine:/home/m ...

  6. django启动创建用户失败

    a django应用启动 b 访问127.0.0.1:8000,报错信息如下,原因为没有这个用户需要创建下用户 c 创建用户过程中报错原因是因为添加了app需要告诉django,这个 模型发生了改变, ...

  7. 简单DP

      1.一只小蜜蜂   有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input输入数据的第一行是一个整数N,表 ...

  8. a3

    队名 massivehard 组员一(组长:晓辉) 今天完成了哪些任务 .整理昨天的两个功能,补些bug 写了一个初步的loyaut 还剩哪些任务: 后台的用来处理自然语言的服务器还没架. 推荐算法还 ...

  9. iOS中UIButton控件的用法及部分参数解释

    在UI控件中UIButton是极其常用的一类控件,它的类对象创建与大多数UI控件使用实例方法init创建不同,通常使用类方法创建: + (id)buttonWithType:(UIButtonType ...

  10. IIS7,IIS7.5 URL重写模块工具

    URL 重写模块 2.0 提供基于规则的重写机制,可在 Web 服务器处理请求的 URL 之前对其进行更改,以及在向 HTTP 客户端提供响应内容之前修改响应内容. 注意:使用环境为IIS7.0(x6 ...