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. DataReader相关知识点

    C#中提供的DataReader可以从数据库中每次提取一条数据. 1. 获取数据的方式[1]DataReader 为在线操作数据, DataReader会一直占用SqlConnection连接,在其获 ...

  2. Simula-Virtual function

    Simula is the name of two simulation programming languages, Simula I and Simula 67, developed in the ...

  3. 安装django和selenium

    安装很简单(前提是python已经安装),命令窗口直接输入pip install django,回车就会自动进行安装,selenium也是一样pip install selenium 启动django ...

  4. 12 个最佳 GNOME(GTK)主题

    作者: Phillip Prado 译者: LCTT 郑 | 2019-04-14 09:45   评论: 1 收藏: 2 让我们来看一些漂亮的 GTK 主题,你不仅可以用在 Ubuntu 上,也可以 ...

  5. X位的有/无符号整数

    目录 X位的有符号整数 X位的无符号整数 知识点来自leetcode整数翻转 X位的有符号整数 以4位为例,不管多少位都是相同的概念 在有符号整数中,第一位二进制位用来表示符号,0为正,1为负 最大值 ...

  6. 密信(Mesince)首创全自动邮件加密,颠覆传统邮件加密软件

    电子邮件泄密已经成为一个全球性的日益严峻的安全问题,解决这个问题的唯一有效办法就是电子邮件内容先加密后发送.然而,使用基于S/MIME标准的传统邮件加密软件进行邮件加密,需要用户具备一定的技术基础.用 ...

  7. 注解实战aftersuite和beforesuite

    package com.course.testng;import org.testng.annotations.*; public class BasicAnnotation { //最基本的注解,用 ...

  8. SPOJ CIRU

    SPOJ CIRU 题意 给出n个圆,求他们覆盖的面积. 解法 自适应Simpson,但需要将圆离散化一下,以保证我们查询的是一个连续的有圆的区间. 奇怪的是我没有离散化,样例都没有过,却把题给A了 ...

  9. 使用yum方式安装mysql5.6

    1.新开的云服务器,需要检测系统是否自带安装mysql # yum list installed | grep mysql 2.如果发现有系统自带mysql,果断这么干 # yum -y remove ...

  10. ansible组件 Ad-Hoc

    ad hoc ---临时的,在ansible里需要快速执行,并不用保存命令的执行方式 简单命令 playbook 复杂命令     EXAMPLES: - name: install the late ...