You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight.

We will ask you to perform the following operation:

  • u v k : ask for the kth minimum weight on the path from node u to node v

Input

In the first line there are two integers N and M. (N, M <= 100000)

In the second line there are N integers. The ith integer denotes the weight of the ith node.

In the next N-1 lines, each line contains two integers u v, which describes an edge (uv).

In the next M lines, each line contains three integers u v k, which means an operation asking for the kth minimum weight on the path from node u to node v.

Output

For each operation, print its result.

Example

Input:
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
2 5 2
2 5 3
2 5 4
7 8 2 
Output:
2
8
9
105

给出一棵树,每个点都自己的权重,然后给出树上的边,要求从节点 u 到节点 v 路径上的第 k 小的权重的大小。
因为权重可能很大,所以需要离散化。
主席树求区间第 k 小维护的是权值线段树的前缀和,然后通过区间相减得到查询区间的权值线段树
所以树形结构的第 k 小维护的也是权值线段树的前缀和,这里的前缀和表示从第 i 个结点到根的前缀和,比如样例的树是

那么我们用主席树把这八个结点维护成这个样子

那么要得到其中两个点(u,v)之间的树形结构,就可以看成 TREE(u) + TREE(v) - TREE(lca(u,v))- TREE(fa(lca(u,v))),把查询看成四棵树之间的相加相减,然后在求一下lca(u,v)就可以了,这里我比较懒直接用在线的写了

 /*
.
';;;;;.
'!;;;;;;!;`
'!;|&#@|;;;;!:
`;;!&####@|;;;;!:
.;;;!&@$$%|!;;;;;;!'.`:::::'.
'!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
:!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
`!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
:@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;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''''''':;;;;;;;!%&@#@$%: '%%!%&;
|&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&;
|@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%`
!@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
'$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
|#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
|&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
`%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
!&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + ;
const int maxm = 1e5 + ;
const int mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T; struct Node {
int l, r;
int sum;
} node[maxn * ];
int a[maxn];
int rt[maxn];
bool vis[maxn];
int deep[maxn];
int fa[maxn][];
vector<int> vec[maxn];
vector<int> vv; void init() {
tol = ;
mes(a, );
mes(rt, );
mes(fa, );
mes(vis, );
mes(node, );
mes(deep, );
vv.clear();
for(int i=; i<=n; i++)
vec[i].clear();
} int getid(int x) {
return lower_bound(vv.begin(), vv.end(), x) - vv.begin() + ;
} void lca_dfs(int u, int f, int d) {
deep[u] = d;
int len = vec[u].size();
for(int i=; i<len; i++) {
int v = vec[u][i];
if(v == f) continue;
if(fa[v][])continue;
fa[v][] = u;
lca_dfs(v, u, d+);
}
} void lca_update() {
for(int j=; (<<j)<=n; j++) {
for(int i=; i<=n; i++) {
fa[i][j] = fa[fa[i][j-]][j-];
}
}
} int lca_query(int u, int v) {
if(deep[u] < deep[v]) swap(u, v);
int f = deep[u] - deep[v];
for(int i=; (<<i)<=f; i++) {
if(f & (<<i)) {
u = fa[u][i];
}
}
if(u != v) {
for(int i=(int)log2(n); i>=; i--) {
if(fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
u = fa[u][];
}
return u;
} void hjt_update(int l, int r, int &x, int y, int pos) {
tol++;
node[tol] = node[y];
node[tol].sum++;
x = tol;
if(l == r) return ;
int mid = (l + r) >> ;
if(pos <= mid)
hjt_update(l, mid, node[x].l, node[y].l, pos);
else
hjt_update(mid+, r, node[x].r, node[y].r, pos);
} void hjt_build(int u, int f) {
// printf("%d %d\n", u, f);
hjt_update(, n, rt[u], rt[f], getid(a[u]));
vis[u] = true;
int len = vec[u].size();
for(int i=; i<len; i++) {
int v = vec[u][i];
if(vis[v]) continue;
if(v == f) continue;
hjt_build(v, u);
}
} int hjt_query(int l, int r, int x, int y, int lca, int flca, int k) {
if(l == r)
return l;
int mid = (l + r) >> ;
int sum = node[node[x].l].sum + node[node[y].l].sum - node[node[lca].l].sum - node[node[flca].l].sum;
if(k <= sum)
return hjt_query(l, mid, node[x].l, node[y].l, node[lca].l, node[flca].l, k);
else
return hjt_query(mid+, r, node[x].r, node[y].r, node[lca].r, node[flca].r, k-sum);
} int main() {
scanf("%d%d", &n, &m);
init();
for(int i=; i<=n; i++) {
scanf("%d", &a[i]);
vv.push_back(a[i]);
}
sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end());
for(int i=; i<n; i++) {
int u, v;
scanf("%d%d", &u, &v);
vec[u].push_back(v);
vec[v].push_back(u);
}
fa[][] = ;
lca_dfs(, , );
lca_update();
hjt_build(, );
while(m--) {
int u, v, k;
scanf("%d%d%d", &u, &v, &k);
int ans = hjt_query(, n, rt[u], rt[v], rt[lca_query(u, v)], rt[fa[lca_query(u, v)][]], k);
printf("%d\n", vv[ans-]);
}
return ;
}

Count on a tree SPOJ - COT (主席树,LCA)的更多相关文章

  1. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

  2. SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解

    题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...

  3. 【BZOJ2588】Count on a tree 题解(主席树+LCA)

    前言:其实就是主席树板子啦……只不过变成了树上的查询 -------------------------- 题目链接 题目大意:求树上$u$到$v$路径第$k$大数. 查询静态区间第$k$大肯定是用主 ...

  4. ☆ [洛谷P2633] Count on a tree 「树上主席树」

    题目类型:主席树+\(LCA\) 传送门:>Here< 题意:给出一棵树.每个节点有点权.问某一条路径上排名第\(K\)小的点权是多少 解题思路 类似区间第\(K\)小,但放在了树上. 考 ...

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

  6. BZOJ2588 Count on a tree 【树上主席树】

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7577  Solved: 185 ...

  7. 【洛谷2633】Count on a tree(树上主席树)

    点此看题面 大致题意: 给你一棵树,每次问你两点之间第\(k\)小的点权,强制在线. 主席树 这种题目强制在线一般就是数据结构了. 而看到区间第\(k\)小,很容易就能想到主席树. 至少不会有人想到树 ...

  8. 🔺Count on a tree SPOJ - COT (无能为力。。。)

    https://cn.vjudge.net/problem/SPOJ-COT 插上 大佬的代码 和 我的...以后再看吧... Count on a tree 大佬:http://www.cnblog ...

  9. BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)

    题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...

随机推荐

  1. SVN上传的时候没法显示文件名,只显示后缀名

    之前在用SVN上传android代码的时候,发现上传列表上的文件没法显示名字,只显示了后缀名,就像这样: 各种疑惑,最终发现解决方法: 右键单击操作栏的status: 然后在出现的选项里面将filen ...

  2. Flask框架搭建REST-API服务

    一.目的 为了能够将测试工具部署成RESTful-API服务,这样就能通过接口的方式提供统一测试工具服务,使用人员就不用构建application而产生的各种环境问题.使用问题. 适合人群:Pytho ...

  3. ASP.NET Core 入门教程 2、使用ASP.NET Core MVC框架构建Web应用

    一.前言 1.本文主要内容 使用dotnet cli创建基于解决方案(sln+csproj)的项目 使用Visual Studio Code开发基于解决方案(sln+csproj)的项目 Visual ...

  4. mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享

    摘要: 下文讲述清空数据库中所有表信息的方法分享,如下所示: 实验环境:sql server 2008 实现思路: 1.禁用所有约束,外键 2.禁用所有触发器 3.删除表数据 4.开启触发器 5.开启 ...

  5. SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器

    SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器 1. T-SQL编程 (1)声明变量 declare @age int (2)为变量赋值 (3)while循环 ...

  6. 【记录】使用在线KMS激活Office系列

    摘要 (有能力的请支持正版office) 网上一些激活工具可能捆绑了木马.病毒.使用激活工具有风险.使用在线KMS来激活则没有这个风险. 找到你的office安装目录 已管理员身份运行cmd输入:cd ...

  7. Svn在工作中的实践感悟

    Svn是一款管理项目代码的版本控制系统,是基于集中式的版本控制系统.在工作中,由于实际开发工作的需要,部门是使用Svn来管理日常的项目开发任务.使用这么长时间了,来谈谈对Svn的感悟. 首先,说下工作 ...

  8. LeetCode算法题-Minimum Moves to Equal Array Elements(Java实现)

    这是悦乐书的第233次更新,第246篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第100题(顺位题号是453).给定大小为n的非空整数数组,找到使所有数组元素相等所需的 ...

  9. Scrapy案例01-爬取传智播客主页上的老师信息

    目录 1. 新建scrapy项目 2. 爬虫文件: 2.1. 查看需要爬取内容存在哪里: 2.2. 设置item需要保存的数据变量 2.3. 创建爬虫文件 2.4. 保存数据 2.5. yield的用 ...

  10. Kali Linux Netcat 学习 与 网络攻击

    Netcat 网络攻击 以及 应用 1.用Netcat进行黑客攻击第1部分:基础知识 Netcat是一个很好的网络实用程序,用于使用TCP和UPD协议读取和写入网络连接.Netcat通常被称为网络工具 ...