Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream> using namespace std; void SetIO(string a){
string in = a + ".in";
freopen(in.c_str(),"r",stdin);
} void debug(){
cout << 233 << endl;
} const int maxn = 100000 + 5; int n, m; int val[maxn]; int Sorted[maxn]; inline void Disperse(){
sort(Sorted + 1, Sorted + 1 + n);
for(int i = 1;i <= n; ++i)
val[i] = lower_bound(Sorted + 1, Sorted + 1 + n, val[i]) - Sorted;
} int head[maxn << 1], to[maxn << 1], nex[maxn << 1], edges; inline void add_edge(int u, int v){
nex[++edges] = head[u];
head[u] = edges;
to[edges] = v;
} inline void Read(){
scanf("%d%d",&n, &m);
for(int i = 1;i <= n; ++i){
scanf("%d",&val[i]), Sorted[i] = val[i];
} for(int i = 1;i < n; ++i){
int a, b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
} const int Tree_const = 50; int root[maxn]; struct Chair_Tree{
int cnt_node; int sumv[maxn * Tree_const], lson[maxn * Tree_const], rson[maxn * Tree_const]; void build(int l, int r, int &o){
if(l > r) return ;
o = ++ cnt_node;
if(l == r) return ;
int mid = (l + r) >> 1;
build(l, mid, lson[o]);
build(mid + 1, r, rson[o]);
} int insert(int l, int r, int o, int pos){
int oo = ++cnt_node;
lson[oo] = lson[o];
rson[oo] = rson[o];
sumv[oo] = sumv[o] + 1; if(l == r) return oo; int mid = (l + r) >> 1;
if(pos <= mid) lson[oo] = insert(l, mid, lson[o], pos);
else rson[oo] = insert(mid + 1, r, rson[o], pos);
return oo;
} int query(int l, int r, int u, int v, int lca, int lca_fa, int k){
if(l == r) return l;
int lsum = sumv[lson[u]] + sumv[lson[v]] - sumv[lson[lca]] - sumv[lson[lca_fa]];
int mid = (l + r) >> 1;
if(k <= lsum) return query(l, mid, lson[u], lson[v], lson[lca], lson[lca_fa], k);
else return query(mid + 1, r, rson[u], rson[v], rson[lca], rson[lca_fa], k - lsum);
} }Tree; const int logn = 20; int f[23][maxn]; int dep[maxn]; void dfs(int u, int fa, int depth){ root[u] = Tree.insert(1, n, root[fa], val[u]);
dep[u] = depth;
f[0][u] = fa; for(int v = head[u]; v ; v = nex[v]){
if(to[v] == fa) continue;
dfs(to[v], u, depth + 1);
}
} inline void get_ancester(){
for(int i = 1;i <= logn; ++i){
for(int j = 1;j <= n; ++j)
f[i][j] = f[i - 1][f[i - 1][j]];
}
} inline int get_lca(int a, int b){
if(dep[a] > dep[b]) swap(a,b);
if(dep[a] != dep[b]){
for(int i = logn;i >= 0;--i){
if(dep[f[i][b]] >= dep[a]) b = f[i][b];
}
}
if(a == b) return a;
for(int i = logn;i>=0;--i)
if(f[i][a] != f[i][b]) a = f[i][a], b = f[i][b];
return f[0][a];
} inline void Build(){
Tree.build(1, n, root[0]);
dfs(1, 0, 1);
get_ancester();
} inline void Init(){
Read();
Disperse();
Build();
} inline void Work(){ int lastans = 0; while(m--){
int u, v, k;
scanf("%d%d%d",&u, &v, &k);
// u ^= lastans; int lca = get_lca(u, v); lastans = Tree.query(1, n, root[u], root[v], root[lca], root[f[0][lca]], k);
lastans = Sorted[lastans]; printf("%d\n", lastans);
}
} int main(){
SetIO("input");
Init();
Work();
return 0;
}

  

SP10628 COT - Count on a tree 主席树的更多相关文章

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

  2. spoj COT - Count on a tree(主席树 +lca,树上第K大)

    您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...

  3. SPOJ Count on a tree(主席树+LCA)

    一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to  ...

  4. 【BZOJ-2588】Count on a tree 主席树 + 倍增

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 3749  Solved: 873[ ...

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

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

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

  7. 洛谷P2633/bzoj2588 Count on a tree (主席树)

    洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...

  8. 洛谷P2633 Count on a tree(主席树上树)

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

  9. 洛谷 P2633 Count on a tree 主席树

    在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$  ,建立 $i$  到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...

随机推荐

  1. 关于app夜间模式那点事

    大半年没写过代码了 一直在忙一些其他的事情  这几天想起来看了一点  心惊肉跳的 发现好陌生  所以打算今后慢慢的拾起来  往深度和广度去发展 发现好久之前写的一个微博项目  有一个夜间模式的功能没有 ...

  2. 转载:【学习之家】Python中__init__.py文件的作用

    Python中__init__.py文件的作用详解 Python中__init__.py文件的作用详解 来源:学习之家 作者:xuexi110 人气:357 发布时间:2016-09-29 摘要:__ ...

  3. sklearn学习4----预处理(1)标准化

    一.[标准化]scale: 1.导入模块  from sklearn.preprocessing import scaler 2.作用:直接将给定数据进行标准化 3.使用代码 X_scaled=sca ...

  4. django patch 解决 ["'15428560000' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

    __init__.py import datetime from django.apps import AppConfig from django.db.models.fields import Da ...

  5. uboot的readme导读

    UBOOT的移植其实并没有想象中的难,这主要归功于众多的工程师已经将常见的平台代码写入了UBOOT,我们所要做的就是一点小小的更改,在网上看了很多相关的移植,也听到有人说其实看了UBOOT的readm ...

  6. python_三级字典

    data = { "北京":{ "昌平":{ "沙河":["oldboy","test"], &qu ...

  7. jQuery 事件流的概念

    jQuery  事件流的概念 什么是事件流 DOM事件流 1. 事件捕获阶段; 2.处于目标阶段; 3.事件冒泡阶段; <!DOCTYPE html> <html lang=&quo ...

  8. 优化VR体验的7个建议

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/50392607 作者:ca ...

  9. 题解 CF1051F 【The Shortest Statement】

    这道题思路比较有意思,第一次做完全没想到点子上... 看到题目第一反应是一道最短路裸题,但是数据范围1e5说明完全不可能. 这个时候可以观察到题目给出了一个很有意思的条件,就是说边最多比点多20. 这 ...

  10. 译:MySQL性能优化的21条最佳经验

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...