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行,表示每个询问的答案。最后一个询问不输出换行符

Sample Input

8 5

105 2 9 3 8 5 7 7

1 2

1 3

1 4

3 5

3 6

3 7

4 8

2 5 1

0 5 2

10 5 3

11 5 4

110 8 2

Sample Output

2

8

9

105

7

HINT

N,M<=100000

Solution

一个典型的树上差分,然后维护第\(k\)大。。主席树嘛

我们主席树的每个版本是从它的父亲处继承过来的,那么它维护的就是这个节点到根节点路径上的信息

那么我们的差分就是

\[ans=find(T_{u}+T_{v}-T_{lca}-T_{lca'fa})
\]

查询就在这个差分后的树上找就行了

#include<bits/stdc++.h>
#define ll long long
#define db double
#define ld long double
#define Mid ((l+r)>>1)
#define lson l,Mid
#define rson Mid+1,r
const int MAXN=100000+10;
int n,m,A[MAXN],Jie[MAXN][20],dep[MAXN],e,beg[MAXN],nex[MAXN<<1],to[MAXN<<1],ans;
std::vector<int> V;
std::map<int,int> M;
struct ChairMan_Tree{
int cnt,lc[MAXN<<5],rc[MAXN<<5],sum[MAXN<<5],root[MAXN];
inline void init()
{
cnt=0;
memset(lc,0,sizeof(lc));
memset(rc,0,sizeof(rc));
memset(sum,0,sizeof(sum));
}
inline void Build(int &rt,int l,int r)
{
rt=++cnt;
sum[rt]=0;
if(l==r)return ;
Build(lc[rt],lson);
Build(rc[rt],rson);
}
inline void Insert(int &rt,int l,int r,int last,int pos)
{
rt=++cnt;
sum[rt]=sum[last]+1;
lc[rt]=lc[last];
rc[rt]=rc[last];
if(l==r)return ;
else
{
if(pos<=Mid)Insert(lc[rt],lson,lc[last],pos);
else Insert(rc[rt],rson,rc[last],pos);
}
}
inline int Query(int left,int right,int gfa,int grand,int l,int r,int k)
{
if(l==r)return l;
else
{
int t=sum[lc[left]]+sum[lc[right]]-sum[lc[gfa]]-sum[lc[grand]];
if(k<=t)return Query(lc[left],lc[right],lc[gfa],lc[grand],lson,k);
else return Query(rc[left],rc[right],rc[gfa],rc[grand],rson,k-t);
}
}
};
ChairMan_Tree T;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(c!='\0')putchar(c);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y)
{
to[++e]=y;
nex[e]=beg[x];
beg[x]=e;
}
inline void discre()
{
sort(V.begin(),V.end());
V.erase(unique(V.begin(),V.end()),V.end());
for(register int i=1;i<=n;++i)
{
int pre=A[i];
A[i]=lower_bound(V.begin(),V.end(),A[i])-V.begin()+1;
M[A[i]]=pre;
}
}
inline void dfs(int x,int f,int d)
{
T.Insert(T.root[x],1,n,T.root[f],A[x]);
dep[x]=d;
Jie[x][0]=f;
for(register int i=beg[x];i;i=nex[i])
if(to[i]==f)continue;
else dfs(to[i],x,d+1);
}
inline void init()
{
for(register int j=1;j<20;++j)
for(register int i=1;i<=n;++i)Jie[i][j]=Jie[Jie[i][j-1]][j-1];
}
inline int LCA(int u,int v)
{
if(dep[u]<dep[v])std::swap(u,v);
if(dep[u]>dep[v])
for(register int i=19;i>=0;--i)
if(dep[Jie[u][i]]>=dep[v])u=Jie[u][i];
if(u==v)return u;
for(register int i=19;i>=0;--i)
if(Jie[u][i]!=Jie[v][i])u=Jie[u][i],v=Jie[v][i];
return Jie[u][0];
}
int main()
{
read(n);read(m);
for(register int i=1;i<=n;++i)
{
read(A[i]);
V.push_back(A[i]);
}
discre();
for(register int i=1;i<n;++i)
{
int u,v;
read(u);read(v);
insert(u,v);
insert(v,u);
}
T.init();
T.Build(T.root[0],1,n);
dfs(1,0,1);
init();
while(m--)
{
int u,v,k,lca;
read(u);read(v);read(k);
u^=ans;
lca=LCA(u,v);
ans=M[T.Query(T.root[u],T.root[v],T.root[Jie[lca][0]],T.root[lca],1,n,k)];
write(ans,'\n');
}
return 0;
}

【刷题】BZOJ 2588 Spoj 10628. Count on a tree的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...

  4. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  5. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

  6. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 7669  Solved: 1894[Submi ...

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

  8. ●BZOJ 2588 Spoj 10628. Count on a tree

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...

  9. BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

    分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...

随机推荐

  1. LintCode——A+B问题

    A+B问题:给出两个整数a和b,求他们的和,但不能使用+等数学运算符. 注意事项: 1.A与B是32位整数 2.可使用位运算符 样例:如果 a=1 并且 b=2,返回3 一.非递归 public cl ...

  2. JDK 和 JRE 有什么区别

    JDK是Java开发工具包(Java Development Kit),JRE是Java运行环境(Java Runtime Environment),JDK包含了JRE,搭建Java环境的时候,安装J ...

  3. Python中的常规习题

    循环总结 while 语句 for 语句 - 字符串 - range() 函数 break 语句 continue 语句 学习笔记传送门 列表学习 # 练习: # 输入一个整数n, 判断这个整数是否是 ...

  4. 使用tensorflow进行mnist数字识别【模型训练+预测+模型保存+模型恢复】

      import sys,os sys.path.append(os.pardir) import numpy as np from tensorflow.examples.tutorials.mni ...

  5. 软件RAID

    软件RAID也必须在多磁盘系统中才能实现.实现RAID1最少要拥有两块硬盘,而实现RAID5则最少要拥有三块硬盘.通常情况下,操作系统所在磁盘采用RAID1,而数据所在磁盘采用RAID5.   卷的类 ...

  6. shutdown命令详解

    基础命令学习目录 原文链接:http://www.cnblogs.com/qlqwjy/p/7746364.html 我 们在操作Linux v/服务器的时候肯定会有需要重启系统,或者关闭系统等操作. ...

  7. Vue 实例详解与生命周期

    Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...

  8. 关于php的array_diff和array_diff_assoc的使用总结

    关于php的array_diff和array_diff_assoc的使用总结 2015-11-07 17:01 184人阅读 评论(0) 收藏 举报  分类: php学习感想(1)  版权声明:本文为 ...

  9. Daily Scrum (2015/11/4)

    因为距离部署的时间临近,而之前我们的进度偏慢.这天晚上我们大多数成员几乎所有时间都用在了这个项目上,成果还算令人满意. 成员 今日任务 时间 明日任务 符美潇 1.修复了一个BUG,此BUG会导致所爬 ...

  10. Scrum Meeting 11.1

    成员 今日任务 明日计划 用时 徐越 学习利用servlet上传下载文件 代码迁移 4h 赵庶宏 数据库的连接及代码学习 数据库连接 2h 武鑫 设计界面;尝试写一些初步的代码,独立完成一些简单界面 ...