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
暴力自重。。。
 
 
在树上建主席树,统计答案时为避免LCA被减两次,计算sum[x]+sum[y]-sum[lca(x,y)]-sum[fa[lca(x,y)]]。
 #include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int N=1e5+;
int n,m,tot,x,y,rk,cnte,ind,cnt,temp,lastans;
int v[N],tmp[N],first[N],id[N],num[N],root[N];
int deep[N],fa[N][];
struct node{int lc,rc,sum;}tr[N*];
struct edge{int to,next;}e[N*];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void ins(int u,int v){e[++cnte]=(edge){v,first[u]};first[u]=cnte;}
void insert(int u,int v){ins(u,v);ins(v,u);}
void dfs(int x)
{
ind++;id[x]=ind;num[ind]=x;
for(int i=;(<<i)<=deep[x];i++)fa[x][i]=fa[fa[x][i-]][i-];
for(int i=first[x];i;i=e[i].next)
{
if(deep[e[i].to])continue;
fa[e[i].to][]=x;deep[e[i].to]=deep[x]+;dfs(e[i].to);
}
}
int lca(int ri,int rj)
{
if(deep[ri]<deep[rj])swap(ri,rj);
int d=deep[ri]-deep[rj];
for(int i=;(<<i)<=d;i++)if((<<i)&d)ri=fa[ri][i];
if(ri==rj)return ri;
for(int i=;i>=;i--)
if((<<i)<=deep[ri]&&fa[ri][i]!=fa[rj][i])
ri=fa[ri][i],rj=fa[rj][i];
return fa[ri][];
}
void update(int &x,int last,int L,int R,int num)
{
x=++cnt;tr[x].sum=tr[last].sum+;
if(L==R)return;
tr[x].lc=tr[last].lc;tr[x].rc=tr[last].rc;
int mid=(L+R)>>;
if(num<=mid)update(tr[x].lc,tr[last].lc,L,mid,num);
else update(tr[x].rc,tr[last].rc,mid+,R,num);
}
int query(int x,int y,int rk)
{
int a=x,b=y,c=lca(x,y),d=fa[c][];
a=root[id[x]];b=root[id[b]];c=root[id[c]];d=root[id[d]];
int L=,R=tot;
while(L<R)
{
int mid=(L+R)>>;
temp=tr[tr[a].lc].sum+tr[tr[b].lc].sum-tr[tr[c].lc].sum-tr[tr[d].lc].sum;
if(temp>=rk)R=mid,a=tr[a].lc,b=tr[b].lc,c=tr[c].lc,d=tr[d].lc;
else rk-=temp,L=mid+,a=tr[a].rc,b=tr[b].rc,c=tr[c].rc,d=tr[d].rc;
}
return tmp[L];
}
int main()
{
n=read();m=read();
for(int i=;i<=n;i++)v[i]=tmp[i]=read();
sort(tmp+,tmp+n+);tot=unique(tmp+,tmp+n+)-tmp-;
for(int i=;i<=n;i++)v[i]=lower_bound(tmp+,tmp+tot+,v[i])-tmp;
for(int i=;i<n;i++)x=read(),y=read(),insert(x,y);
deep[]=;dfs();
for(int i=;i<=n;i++)
{
temp=num[i];
update(root[i],root[id[fa[temp][]]],,tot,v[temp]);
}
for(int i=;i<=m;i++)
{
x=read();y=read();rk=read();x^=lastans;
lastans=query(x,y,rk);printf("%d",lastans);
if(i!=m)printf("\n");
}
return ;
}

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

  1. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  2. 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树

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

  3. 【BZOJ】【2588】COT(Count On a Tree)

    可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...

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

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

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

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

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

  8. 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个节点的树,每个点 ...

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

随机推荐

  1. 合法括号序列(dp+组合数学)

    键盘上有左括号(,右括号),和退格键-,共三个键. 牛牛希望按键n次,使得输入的字符串恰好一个合法的括号序列. 每按一次左括号(,字符串末尾追加一个左括号( 每按一次右括号),字符串末尾追加一个右括号 ...

  2. 洛谷P4175 网络管理

    题意:链上带修第k大. 这毒瘤题...别看题意只有7个字,能把我吊打死... 介绍其中两种做法好了.其实思想上是一样的. 对于每一个点,建立权值线段树,维护它到根路径上的所有权值. 一条路径上的点集就 ...

  3. A1140. Look-and-say Sequence

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

  4. A1137. Final Grading

    For a student taking the online course "Data Structures" on China University MOOC (http:// ...

  5. 模块---hashlib、configparse、logging

    一.hashlib模块 hashlib模块介绍:hashlib这个模块提供了摘要算法,例如 MD5.hsa1 摘要算法又称为哈希算法,它是通过一个函数,把任意长度的数据转换为一个长度固定的数据串,这个 ...

  6. io系列之其他事项

    二.对IO异常的处理. io操作中,只要涉及到底层操作的就必须进行 io异常处理. IOException 是IO操作中必须处理的异常. 示例: class IOExceptionTest { pub ...

  7. php5.4后htmlspecialchars输出为空的问题

    从旧版升级到php5.4,恐怕最麻烦的就是htmlspecialchars这个问题了!当然,htmlentities也会受影响,不过,对于中文站来说一般用htmlspecialchars比较常见,ht ...

  8. Excel:6种多条件查找方法

    如下图所示,要求根据设备分类和品牌来查找相应的销售数量. 1. 使用VLOOKUP+辅助列进行多条件查找 本例采用的方法是在原表的最前面加一辅助列,辅助列的公式为:=B2&C2 然后再采用VL ...

  9. 数据挖掘的标准流程-CRISP-DM

    1.起源 CRISP-DM (cross-industry standard process for data mining), 即为"跨行业数据挖掘过程标准".此KDD(know ...

  10. HTML常用提交按钮

    1. 标签=元素 disabled(不可操作)  readonly(只读)  placeholder(提示文本) autofocus(自动获焦)  autocomplete=”on(默认.规定启用自动 ...