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 (u, v).
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
7
题目大意:给出n个结点,这n个结点形成一棵树,然后每个结点有一个权值,然后问我们从u点到v点的路径上的第k小的点权值是多少。
解题思路:我们在这棵树上建主席树,每个儿子结点继承父亲结点的信息,然后加入自己的信息,那么我们的主席树其实就是维护了该节点和它到它的祖先结点的这一条链的值,相当于前缀和啦,那么从u到v的这条路径就可以表示为u+v-LCA(u,v)-fa(LCA(u,v))。所以在主席树query函数里面修改一下sum的求法就可以啦。
代码:
#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
using namespace std;
#define int long long
#define ls root<<1
#define rs root<<1|1
const int maxn = 1e5 + 7;
const int inf = 0x3f3f3f3f;
struct tree
{
int l, r, date;
} t[maxn << 6];
int root[maxn], tot, a[maxn], pos[maxn], len;
int f[maxn][30], lg[maxn], depth[maxn];
void build(int &rt,int l,int r)
{
rt = ++tot;
t[rt].date = 0;
if(l==r)
return;
int mid = l + r >> 1;
build(t[rt].l, l, mid);
build(t[rt].r, mid + 1, r);
}
void update(int pre,int &now,int l,int r,int c)
{
now = ++tot;
t[now] = t[pre], t[now].date++;
if(l==r)
return;
int mid = l + r >> 1;
if(c<=mid)
update(t[pre].l, t[now].l, l, mid, c);
else
update(t[pre].r, t[now].r, mid + 1, r, c);
}
int query(int pre,int now,int lcA,int flca,int l,int r,int k)
{
if(l==r)
return l;
int nl = t[now].l, pl = t[pre].l, ll = t[lcA].l, lcl = t[flca].l;
int sum = t[nl].date + t[pl].date - t[ll].date - t[lcl].date, mid = l + r >> 1;
if(sum>=k){
return query(pl, nl, ll, lcl,l, mid, k);
}
else{
return query(t[pre].r, t[now].r, t[lcA].r,t[flca].r, mid + 1, r, k - sum);
}
}
struct Edge
{
int v, next;
} e[maxn<<2];
int cnt, head[maxn];
void add(int a,int b)
{
e[++cnt] = Edge{b, head[a]};
head[a] = cnt;
}
void dfs(int u,int fa)
{
f[u][0] = fa;
depth[u] = depth[fa] + 1;
update(root[fa], root[u], 1, len, a[u]);
for (int i = 1; (1 << i) <= depth[u];i++){
f[u][i] = f[f[u][i - 1]][i - 1];
}
for (int i = head[u]; i;i=e[i].next){
int v = e[i].v;
if(v==fa)
continue;
dfs(v, u);
}
}
int getlca(int a,int b)
{
if(depth[a]<depth[b])
swap(a, b);
//cout << depth[a] << ' ' << depth[b] << endl;
while(depth[a]>depth[b]){
//cout << lg[depth[a] - depth[b]] << ' ';
a = f[a][lg[depth[a] - depth[b]] - 1];
//cout << a << endl;
}
//cout << a << ' ' << b << endl;
if(a==b)
return a;
for (int i = lg[depth[a]] - 1; i >= 0;i--){
if(f[a][i]!=f[b][i])
a = f[a][i], b = f[b][i];
}
return f[a][0];
}
signed main()
{
#ifndef ONLINE_JUDGE
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
#endif
int n, m;
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n + 100; i++){
lg[i] = ((1 << lg[i - 1]) == i) + lg[i-1];
}
for (int i = 1; i <= n;i++){
scanf("%lld", a + i);
pos[i] = a[i];
}
sort(pos + 1, pos + n + 1);
len = unique(pos + 1, pos + 1 + n) - pos - 1;
build(root[0], 1, len);
for (int i = 1; i <= n;i++)
a[i] = lower_bound(pos + 1, pos + 1 + len, a[i]) - pos;
for (int i = 1; i < n;i++){
int a, b;
scanf("%lld%lld", &a, &b);
add(a, b), add(b, a);
}
dfs(1, 0);
int lastans = 0;
while (m--)
{
int u, v, k;
scanf("%lld%lld%lld", &u, &v, &k);
//u ^= lastans;
int lca = getlca(u, v);
lastans = query(root[u], root[v], root[lca], root[f[lca][0]], 1, len, k);
//cout << lca << endl;
lastans = pos[lastans];
printf("%lld\n", lastans);
}
}
这个题在BZOJ上也有挂,本来是我之前写过的一道题,但是今天写的时候还是写了一堆bug,甚至把-打成了=,然后找了一中午的bug,还是太菜了叭。
BZOJ上的题是加了强制在线,感兴趣的朋友可以去BZOJ 2588看一下。