【题目链接】
【前置技能】
- 主席树
- Kruskal重构树
【题解】
- 两题其实分别是支持离线的版本和强制在线的版本。离线版本其实也可以离线询问,按困难值排序然后数据结构合并一下就好了。
- 仔细分析一下,其实有效的路径只有最小生成树上的边,因为如果走其他不在最小生成树上的路径到达某个节点,困难度的下限一定不会更小。在Kruskal重构树上倍增往上跳到最远能跳的点,那么所有能到达的点都在这个点的子树中。按照dfs序建立主席树,在主席树上二分即可。
- 时间复杂度 O ( N l o g N + Q l o g N ) O(NlogN+QlogN) O(NlogN+QlogN)
【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
#define MAXN 100010
#define MAXM 500010
#define MAXLOG 21
using namespace std;
int n, m, Q, h[MAXN], san[MAXN], v, lim, k, lastans;
struct edg{int u, v, w;}e[MAXM];
bool cmp(edg a, edg b){return a.w < b.w;}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void read(T &x){
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}
struct Segment_Tree{
struct info{int ls, rs, sum;}a[MAXN * MAXLOG + MAXN];
int n, root[MAXN], cnt;
void build(int &pos, int l, int r){
if (l == r) return;
int mid = (l + r) >> 1;
build(a[pos].ls, l, mid);
build(a[pos].rs, mid + 1, r);
}
void init(int x){
n = x, cnt = 0;
build(root[0], 1, n);
}
void modify(int &pos, int old, int l, int r, int p){
pos = ++cnt;
a[pos] = a[old], ++a[pos].sum;
if (l == r) return;
int mid = (l + r) >> 1;
if (p <= mid) modify(a[pos].ls, a[old].ls, l, mid, p);
else modify(a[pos].rs, a[old].rs, mid + 1, r, p);
}
void modify(int Tcur, int Told, int p){
modify(root[Tcur], root[Told], 1, n, p);
}
int query(int lp, int rp, int l, int r, int k){
if (l == r) return l;
int mid = (l + r) >> 1, tmp = a[a[rp].ls].sum - a[a[lp].ls].sum;
if (tmp >= k) return query(a[lp].ls, a[rp].ls, l, mid, k);
else return query(a[lp].rs, a[rp].rs, mid + 1, r, k - tmp);
}
int query(int L, int R, int k){
if (a[root[R]].sum - a[root[L - 1]].sum < k) return -1;
else return query(root[L - 1], root[R], 1, n , a[root[R]].sum - a[root[L - 1]].sum - k + 1);
}
}sgt;
struct Kruskal_Tree{
struct info{int ls, rs, val, L, R;}a[MAXN * 2];
int cnt, fa[MAXN * 2][MAXLOG + 2], rnk, F[MAXN * 2];
void dfs(int pos, int dad){
fa[pos][0] = dad, a[pos].L = INF, a[pos].R = -INF;
for (int i = 1; i <= MAXLOG; ++i)
fa[pos][i] = fa[fa[pos][i - 1]][i - 1];
if (a[pos].ls) {
dfs(a[pos].ls, pos);
chkmin(a[pos].L, a[a[pos].ls].L);
chkmax(a[pos].R, a[a[pos].ls].R);
}
if (a[pos].rs) {
dfs(a[pos].rs, pos);
chkmin(a[pos].L, a[a[pos].rs].L);
chkmax(a[pos].R, a[a[pos].rs].R);
}
if (pos <= n) {
a[pos].L = a[pos].R = ++rnk;
sgt.modify(rnk, rnk - 1, h[pos]);
}
}
int find(int x){
if (F[x] == x) return x;
else return (F[x] = find(F[x]));
}
void init(){
cnt = n, rnk = 0;
sort(e + 1, e + 1 + m, cmp);
for (int i = 1; i <= 2 * n; ++i)
F[i] = i;
int tmp = 0;
for (int i = 1; i <= m; ++i){
int x = e[i].u, y = e[i].v, w = e[i].w, fx = find(x), fy = find(y);
if (fx != fy){
++cnt;
a[cnt].ls = fx, a[cnt].rs = fy, a[cnt].val = w;
F[fx] = F[fy] = cnt;
++tmp;
if (tmp == n - 1) break;
}
}
dfs(cnt, 0);
}
int query(int v, int lim, int k){
for (int i = MAXLOG; i >= 0; --i)
if (fa[v][i] && a[fa[v][i]].val <= lim) v = fa[v][i];
return sgt.query(a[v].L, a[v].R, k);
}
}kt;
void discre(){
for (int i = 1; i <= n; ++i)
san[i] = h[i];
sort(san + 1, san + 1 + n);
int cnt = unique(san + 1, san + 1 + n) - san - 1;
for (int i = 1; i <= n; ++i)
h[i] = lower_bound(san + 1, san + 1 + cnt, h[i]) - san;
}
int main(){
read(n), read(m), read(Q);
for (int i = 1; i <= n; ++i)
read(h[i]);
discre();
for (int i = 1; i <= m; ++i)
read(e[i].u), read(e[i].v), read(e[i].w);
sgt.init(n);
kt.init();
while (Q--){
read(v), read(lim), read(k);
if (lastans != -1){
v = v ^ lastans, lim = lim ^ lastans, k = k ^ lastans;
}
lastans = kt.query(v, lim, k);
if (lastans != -1) lastans = san[lastans];
printf("%d\n", lastans);
}
return 0;
}