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. ng-repeat 中的 track by $index

    用ng-repeat指令遍历一个javascript数组,当数组中有重复元素的时候,angularjs会报错,这是因为ng-Repeat不允许collection中存在两个相同Id的对象. 对于数字或 ...

  2. Xcode7 下导入第三方库 图文介绍

    网上没有很好的图文介绍,干脆我自己写一个好了,方便新手入门. 这里以导入著名的第三方网络库AFNetWorking v3.0.4和数据库FMDB v2.6.2为例进行说明. 好,下面开始. 下载源文件 ...

  3. laravel报错:Unable to detect application namespace.

    使用报错:Unable to detect application namespace. 是conposer.json格式不对

  4. BZOJ 4817 [SDOI2017]树点涂色 (LCT+线段树维护dfs序)

    题目大意:略 涂色方式明显符合$LCT$里$access$操作的性质,相同颜色的节点在一条深度递增的链上 用$LCT$维护一个树上集合就好 因为它维护了树上集合,所以它别的啥都干不了了 发现树是静态的 ...

  5. MBR和GPT分区学习

    1.MBR 早期的windows和linux都采用的MBR的方法来处理开机引导程序和分区表,对于linux的MBR分区来讲,0柱面0磁道1扇区用于引导驱动程序,第一扇区有512个字节,前446字节存放 ...

  6. Tire树总结(模板+例题)

    题目来自<算法竞赛设计指南> Tire树是一种可以快速查找字符串的数据结构 模板 #include<cstdio> #include<algorithm> #inc ...

  7. AT1145 ホリドッグ

    洛谷的题解区里竟然没有O(1)做法详解-- 题面就是要判断\(1+2+\dots+n\)是不是素数 很容易让人想到上面的式子事实上等于\(n(n+1)/2\) 根据质数的定义,质数只能被1和自身整除 ...

  8. JDK1.7中的ThreadPoolExecutor源代码剖析

    JDK1. 7中的ThreadPoolExecutor 线程池,顾名思义一个线程的池子,池子里存放了非常多能够复用的线程,假设不用线程池相似的容器,每当我们须要创建新的线程时都须要去new Threa ...

  9. QQ在线人数统计图数据解析

    转载请注明出处:http://blog.csdn.net/xiaoy_h/article/details/27980851 我相信非常多人一定去过这个地方: http://im.qq.com/onli ...

  10. UVA 436 - Arbitrage (II)(floyd)

    UVA 436 - Arbitrage (II) 题目链接 题意:给定一些国家货币的汇率.问是否能通过不断换货币使钱得到增长 思路:floyd,完事后推断一下有没有连到自己能大于1的情况 代码: #i ...