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的这条路径上的权值线段树 ----------------------- ...
随机推荐
- RHEL 6.5----SCSI存储
主机名 IP master 192.168.30.130 node-1 192.168.30.131 node-2 192.168.30.132 安装并启动 [root@master ~]# ll / ...
- 序列化pickle模块
1.pickle模块 pickle.dumps() 和pickle.loads() import pickle f = open('112.pkl','w') a = {'name':2,2:3,3: ...
- CF962D Merge Equals
思路: 不必每次都找最小的值进行合并,从前往后扫一遍的过程中能合并就一直合并. 实现: #include <bits/stdc++.h> using namespace std; type ...
- flex和box兼容性写法
display: -webkit-box; /* Chrome 4+, Safari 3.1, iOS Safari 3.2+ */ display: -moz-box; /* Firefox 17- ...
- 测试当前C环境的栈帧增长方向以及传递参数时的压栈顺序
前文链接:上次由于一个很常见的printf-bug(下文有提及)引发了我对栈的思考,并写下了一点总结.这次就尝试对不同的C环境进行实践,检测其传递参数的一些性质. 这是今天写的检查C环境的一段程序.能 ...
- 字符串翻转(java)
1 递归,二分 private static String reverse(String s) { int N = s.length(); if(N <= 1) return s; String ...
- Android Studio集成crashlytics后无法编译的问题
http://blog.csdn.net/zhuobattle/article/details/50555393 问题描述: 在用fabric集成后编译出现如下错误, Error:Cause: hos ...
- java程序在一个电脑上只启动一次,只开一个进程
方案1: 单进程程序可以用端口绑定.程序启动的时候可以尝试看该端口是否已经被占用,如果占用则程序已经启动. 方案2:你可以在java程序中创建一个隐藏文件,程序退出的时候删除这个文件.这样在程序启动的 ...
- 搜索 || BFS || POJ 2157 Maze
走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...
- 小程序02 wxml和wxss
微信小程序的排版就跟wxml和wxss有关,它们两者相当于HTML和CSS,其中wxml指定了界面的框架结构,而wxss指定了界面的框架及元素的显示样式. 一.wxml 界面结构wxmL比较容易理解, ...