luogu2633 Count on a tree
主席树放到树上而已
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
int n, m, lstans, uu, vv, a[100005], b[100005], rnk[100005], rem, cnt;
int lson[2200005], rson[2200005], sum[2200005], rot[100005], qwq, ww;
int fa[100005][19], dep[100005], hea[100005], tt[5];
struct Edge{
int too, nxt;
}edge[200005];
void add_edge(int fro, int too){
edge[++qwq].nxt = hea[fro];
edge[qwq].too = too;
hea[fro] = qwq;
}
int build(int l, int r){
int rt=++cnt;
int mid=(l+r)>>1;
if(l==r) return rt;
if(l<=mid) lson[rt] = build(l, mid);
if(mid<r) rson[rt] = build(mid+1, r);
return rt;
}
int update(int pre, int l, int r, int x){
int rt=++cnt;
int mid=(l+r)>>1;
lson[rt] = lson[pre]; rson[rt] = rson[pre]; sum[rt] = sum[pre] + 1;
if(l<r){
if(x<=mid) lson[rt] = update(lson[pre], l, mid, x);
if(mid<x) rson[rt] = update(rson[pre], mid+1, r, x);
}
return rt;
}
void dfs(int x, int f){
fa[x][0] = f;
dep[x] = dep[f] + 1;
rot[x] = update(rot[f], 1, rem, rnk[x]);
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f) dfs(t, x);
}
}
int getLca(int uu, int vv){
if(dep[uu]<dep[vv]) swap(uu, vv);
for(int i=16; i>=0; i--)
if(dep[fa[uu][i]]>=dep[vv])
uu = fa[uu][i];
if(uu==vv) return vv;
for(int i=16; i>=0; i--)
if(fa[uu][i]!=fa[vv][i]){
uu = fa[uu][i];
vv = fa[vv][i];
}
return fa[uu][0];
}
int query(int l, int r, int k){
int tmp=0;
int mid=(l+r)>>1;
tmp -= sum[lson[tt[3]]] + sum[lson[tt[4]]];
tmp += sum[lson[tt[1]]] + sum[lson[tt[2]]];
if(l==r) return l;
if(k<=tmp){
for(int i=1; i<=4; i++)
tt[i] = lson[tt[i]];
return query(l, mid, k);
}
else{
for(int i=1; i<=4; i++)
tt[i] = rson[tt[i]];
return query(mid+1, r, k-tmp);
}
}
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
}
for(int i=1; i<n; i++){
scanf("%d %d", &uu, &vv);
add_edge(uu, vv);
add_edge(vv, uu);
}
sort(b+1, b+1+n);
rem = unique(b+1, b+1+n) - (b + 1);
for(int i=1; i<=n; i++)
rnk[i] = lower_bound(b+1, b+1+rem, a[i]) - b;
rot[0] = build(1, rem);
dfs(1, 0);
for(int i=1; i<=16; i++)
for(int j=1; j<=n; j++)
fa[j][i] = fa[fa[j][i-1]][i-1];
while(m--){
scanf("%d %d %d", &uu, &vv, &ww);
uu ^= lstans;
int lca=getLca(uu, vv);
tt[1] = rot[uu]; tt[2] =rot[vv];
tt[3] = rot[lca]; tt[4] = rot[fa[lca][0]];
lstans = b[query(1, rem, ww)];
printf("%d\n", lstans);
}
return 0;
}
luogu2633 Count on a tree的更多相关文章
- 「luogu2633」Count on a tree
「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...
- 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 ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
- 【BZOJ】【2588】COT(Count On a Tree)
可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...
- 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 ...
- spoj cot: Count on a tree 主席树
10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...
- 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的这条路径上的权值线段树 ----------------------- ...
随机推荐
- PowerShell~发布你的mvc网站
通过使用ps加上msbuild可以方便的编译你的.net应用程序,并且可以把它发布到你的磁盘上,部署非常方例! 我们在c盘添加一个hello网站,解决方案名是hello.sln,它的网站是hello. ...
- spring boot使用jpa查询mysql数据库的视图时不报错,但查询结果数据总是重复第一条
问题描述: 在数据库里查询到的结果是正常显示的 在程序中返回的结果: 解决方法: 添加行号作为主键: 解决! 我明明是前端啊前端,为啥在搞后台....,总感觉我要在向全栈进发,希望自己有朝一日真的能成 ...
- sybase sql anywhere 5.0 安装后sybase central中无法打开视图等的解决办法
无法打开的原因初步分析要用英文版的xp,后来在如下处发现问题,是sql anywhere的版本太旧了, 可能没有使用Unicode编码,设置一下如下选项可以解决问题.
- 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- 如何用Chrome自带的截屏功能截取超过一个屏幕的网页
提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...
- leetcode_935. Knight Dialer_动态规划_矩阵快速幂
https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...
- 在Vue中遇到的各种坑 及性能提升
Vue: (1) 没有再模板里引用data数据,会不会引起update.beforeUpdate生命周期函数的执行? 不会 (2)组件改成异步 (3)v-once (4)如果不用template ...
- iview modal 弹框 模板
iview modal 弹框 模板 <!-- * @description 上传图片 * @fileName camera.vue * @author 彭成刚 * @date // :: * @ ...
- js 根据指定个数切割数组
Part.1 问题 写项目时,遇到需要前端做 假分页 的问题:后端会将数据全部返回,前端自己做分页 Part.2 思路 拿到后端全部返回的数据后,按照 产品需求 进行分页,如每页显示 10 条数据为 ...
- Visual Odometry
http://www.cvlibs.net/datasets/kitti/eval_odometry.php