洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
【题解】
蜜汁强制在线。。。
每个点开一个从它到根的可持久化权值线段树。查询的时候利用差分的思想在树上左右横跳就好了。
#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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- 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个节点的树,每个点 ...
- 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 ...
- 主席树 || 可持久化线段树 || 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) ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- ●BZOJ 2588 Spoj 10628. Count on a tree
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- 关于js-cookie使用出现兼容性问题以及js-cookie的如何使用
最近使用vue开发的项目,开发过程引入了js-cookie插件,在PC端以及移动端网页调试都没出现问题,但是打包成APP在安卓手机调试发现使用js-cookie保存的数据失效了,然后只能使用local ...
- 使用IntelliJ IDEA 配置JDK(入门)
一.JDK下载 首先要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击 ...
- E20170612-sl
tampon n. 卫生棉塞; 止血棉塞; sanitary n. 公共厕所; adj. 卫生的; 清洁的; belonging n. 附属品,附件,属性; ...
- P4097 [HEOI2013]Segment
传送门 简单来说就是对于每条线段,先把它拆成\(O(logn)\)条,然后对于每一条再\(O(logn)\)判断在所有子区间的优劣程度 //minamoto #include<bits/stdc ...
- lodop打印图片
LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM')); //LODOP. ...
- [ZJOI2006]Book书架
Description Sally有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号.Sally在看书的时候,每次取出一本书,看完后放回书柜 ...
- service: no such service mysqld 与MySQL的开启,关闭和重启
1.问题原因与解决办法 因为修改了MySQL临时文件的目录后,使用service mysqld restart重启MySQL出现如下错误: service: no such service mysql ...
- xcode 制作静态库文件(.a)
参考: http://www.jb51.net/article/37853.htm 摘要: 1. 获取.a文件的信息 lipo -info /Users/pjk1129/De ...
- Jquery 全选、反选问题的记录
<div id="list"> <ul id="choseList" > <li><input type=" ...
- Echarts和Quartz简介
一.Echarts ECharts 是由百度前端团队开发的一款开源的基于 js 图形报表组件,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大 ...