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. 自动化jenkins报:ModuleNotFoundError: No module named 'common'

    直接执行脚本是没有问题,报如下错误: 你已经在run.py脚本加路径了为什么还会报这个错呢,就是你加的路径,应该在所有的包上面,才不会报这个错,如下: 注:以下是我的解决方法仅作参考.如果我的发表的内 ...

  2. Python之元类详解

    一.引子 元类属于Python面向对象编程的深层魔法,99%的人都不得要领,一些自以为搞明白元类的人其实也是自圆其说,点到为止,从队元类的控制上来看就破绽百出,逻辑混乱: 二.什么是元类 一切源自于一 ...

  3. mysql 优化之 doublewrite buffer 机制

    是什么? doublewrite buffer是mysql 系统表空间的一块存储区域. 有什么用? 在Innodb将数据页写到数据存储文件之前,存储从Innodb缓存池刷过来的数据页.且只有将数写入d ...

  4. Netty源码分析第5章(ByteBuf)---->第9节: ByteBuf回收

    Netty源码分析第五章: ByteBuf 第九节: ByteBuf回收 之前的章节我们提到过, 堆外内存是不受jvm垃圾回收机制控制的, 所以我们分配一块堆外内存进行ByteBuf操作时, 使用完毕 ...

  5. Redis源码阅读(一)事件机制

    Redis源码阅读(一)事件机制 Redis作为一款NoSQL非关系内存数据库,具有很高的读写性能,且原生支持的数据类型丰富,被广泛的作为缓存.分布式数据库.消息队列等应用.此外Redis还有许多高可 ...

  6. React Native移动开发实战-3-实现页面间的数据传递

    React Native使用props来实现页面间数据传递和通信.在React Native中,有两种方式可以存储和传递数据:props(属性)以及state(状态),其中: props通常是在父组件 ...

  7. nodejs ejs模板数据库操作

    1. 建立数据连接setting.js var settings = {}; settings.db = { host: 'localhost', user: 'root', password: '1 ...

  8. [zabbix] zabbix检测mysql主从状态

    环境说明: zabbix-proxy 172.16.2.95(zabbix-server同理) zabbix-agent111 172.16.2.111 mysql从机 1.mysql从机添加用户权限 ...

  9. Daily Scrum9 11.13

    昨天的任务已完成. 今日任务: 姓名 今日任务 时长 徐钧鸿 测试SQL包里的代码 2h 张艺 继续搭建还没搭建完的框架 修复bug 2h 黄可嵩 继续进行搜索响应编写 2h 徐方宇 搭建框架 修改b ...

  10. 团队博客作业Week1 --- 团队成员简介

    团队博客作业Week1 团队作业1 我们团队是一个以功能团队模式组建而成的团队,我们总共有5位队员,分别是:李剑锋.陈谋.卢惠明.潘成鼎.仉伯龙. 中间的那位就是李剑锋,我们的PM(项目经理).性格热 ...