【题解】

  蜜汁强制在线。。。

  每个点开一个从它到根的可持久化权值线段树。查询的时候利用差分的思想在树上左右横跳就好了。

  

 #include<cstdio>
#include<algorithm>
#define N 100010
#define rg register
#define ls (a[u].l)
#define rs (a[u].r)
using namespace std;
int n,n2,m,tot,root[N],last[N],dep[N],top[N],hvy[N],fa[N],size[N],val[N],b[N];
struct tree{
int sum,l,r;
}a[N*];
struct edge{
int to,pre;
}e[N<<];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline int qlca(int x,int y){
int f1=top[x],f2=top[y];
while(f1!=f2){
if(dep[f1]<dep[f2]) swap(x,y),swap(f1,f2);
x=fa[f1]; f1=top[x];
}
return dep[x]<dep[y]?x:y;
}
void update(int &u,int l,int r,int pos){
a[++tot]=a[u]; a[u=tot].sum++;
if(l==r) return;
int mid=(l+r)>>;
if(pos<=mid) update(ls,l,mid,pos);
else update(rs,mid+,r,pos);
}
int query(int u,int v,int lca,int f,int l,int r,int k){
if(l==r) return l;
int tmp=a[ls].sum+a[a[v].l].sum-a[a[lca].l].sum-a[a[f].l].sum,mid=(l+r)>>;
return k<=tmp?query(ls,a[v].l,a[lca].l,a[f].l,l,mid,k):query(rs,a[v].r,a[lca].r,a[f].r,mid+,r,k-tmp);
}
void dfs1(int x){
size[x]=; dep[x]=dep[fa[x]]+;
root[x]=root[fa[x]]; update(root[x],,n2,val[x]);
for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa[x]){
fa[to]=x; dfs1(to);
size[x]+=size[to];
if(size[to]>size[hvy[x]]) hvy[x]=to;
}
}
void dfs2(int x,int tp){
top[x]=tp;
if(hvy[x]) dfs2(hvy[x],tp);
for(rg int i=last[x],to;i;i=e[i].pre)
if((to=e[i].to)!=fa[x]&&to!=hvy[x]) dfs2(to,to);
}
int main(){
n=read(); m=read();
for(rg int i=;i<=n;i++) val[i]=b[i]=read();
sort(b+,b++n); n2=unique(b+,b++n)-b-;
for(rg int i=;i<=n;i++) val[i]=lower_bound(b+,b++n2,val[i])-b;
for(rg int i=;i<n;i++){
int u=read(),v=read();
e[++tot]=(edge){v,last[u]}; last[u]=tot;
e[++tot]=(edge){u,last[v]}; last[v]=tot;
}
tot=; dfs1(); dfs2(,);
int last=;
while(m--){
int u=read()^last,v=read(),k=read(),l=qlca(u,v),f=fa[l];
printf("%d\n",last=b[query(root[u],root[v],root[l],root[f],,n2,k)]);
}
return ;
}

洛谷 2633 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: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

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

  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 主席树+lca

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

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

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

随机推荐

  1. bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会【树形dp】

    不能用read会TLE!!不能用read会TLE!!不能用read会TLE!! 一开始以为要维护每个点,线段树写了好长(还T了-- 首先dfs一遍,求出点1为集会地点的答案,处理处val[u]为以1为 ...

  2. P3343 [ZJOI2015]地震后的幻想乡

    传送门 给积分大佬跪了 再给状压大佬也跪了 //minamoto #include<bits/stdc++.h> #define rint register int #define ll ...

  3. Android 性能优化(2)性能工具之「Hierarchy Viewer 」Optimizing Your UI:分析哪个view有性能问题,查看屏幕上某像素点的坐标,颜色等

    Optimizing Your UI In this document Using Hierarchy Viewer Running Hierarchy Viewer and choosing a w ...

  4. AppConfig 操作简易封装

    using System; using System.Configuration; namespace HT.IMS.Common { public class ClientConfig { ; pu ...

  5. Spark SQL概念学习系列之Spark SQL入门(八)

    前言 第1章   为什么Spark SQL? 第2章  Spark SQL运行架构 第3章 Spark SQL组件之解析 第4章 深入了解Spark SQL运行计划 第5章  测试环境之搭建 第6章 ...

  6. Oracle数据库数据显示乱码问题解决方法。

    一.问题描述:       在将其它数据库的数据导出文件导入本地新建数据库时,所导入的数据全部是乱码,一般表现为数据表中列的值类似于"?????",即内容大部分为?的表现形式.初步 ...

  7. JavaScript开发心得--如何传递某行数据给下一页

    1, 应用场景 在某个html页面显示一批数据,如20个用户的名称.年龄等,每行都要一个编辑按钮,点击编辑后,将此行数据带入某个专门的编辑页进行显示,修改后保存. 问题是 点击编辑按钮后,如何得知要编 ...

  8. Microsoft SQL Server学习(三)

    1.表:表示一个实体(客观存在的事物或抽象时间),可实现对实体的数据描述和数据操作. 2.表结构:二位平面(行.列) 3.数据类型: 类型名称 类型 整形 bit(只存储0.1) samllint i ...

  9. Probabilistic locking in SQLite

    In SQLite, a reader/writer lock mechanism is required to control the multi-process concurrent access ...

  10. dutacm.club_1087_Common Substrings_(KMP)_(结合此题通俗理解kmp的next数组)

    1087: Common Substrings Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/ ...