在Bytemountains有N座山峰,每座山峰有他的高度h_i。有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经过困难值小于等于x的路径所能到达的山峰中第k高的山峰,如果无解输出-1。
BZOJ3551强制在线。
Kruskal重构树。
考虑Kruskal的加边过程,将边从小到大加入的同时,新建一个节点,边连接的两部分作为该节点的左右子树。
对于一个节点,如果要求困难值小于等于x,那么就找到它满足连接边小于等于x的、深度最小的祖先,该节点可以走到该祖先子树内的任意一点。所以我们可以倍增找到该祖先,搞出DFS序后用主席树来维护祖先子树内的第k大。
/**************************************
* Au: Hany01
* Prob: [BZOJ3551][ONTAK2010] Peaks 加强版
* Date: Jul 20th, 2018
* Email: [email protected]
**************************************/
#include
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _, __;
for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 1e5 + 5, maxm = 5e5 + 5;
int n, m, N, fa[19][maxn << 1], id[maxn], h[maxn], ls[maxn], tot, sum, hd[maxn << 1];
struct Edge {
int u, v, w;
}E[maxm];
bool operator < (Edge A, Edge B) { return A.w < B.w; }
int dsu_fa[maxn];
int find(int u) { return dsu_fa[u] == u ? u : dsu_fa[u] = find(dsu_fa[u]); }
int beg[maxn << 1], nex[maxn << 1], v[maxn << 1], e, lst[maxn << 1], cnt, st[maxn << 1], ed[maxn << 1];
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
void DFS(int u, int pa) {
fa[0][u] = pa, lst[st[u] = ++ cnt] = u;
for (register int i = beg[u]; i; i = nex[i]) DFS(v[i], u);
ed[u] = cnt;
}
struct Node { int lc, rc, val; }tr[maxn * 30];
int rt[maxn << 1];
#define mid ((l + r) >> 1)
void update(int &now, int las, int l, int r, int x) {
tr[now = ++ sum] = tr[las], ++ tr[now].val;
if (l != r) if (x <= mid) update(tr[now].lc, tr[las].lc, l, mid, x);
else update(tr[now].rc, tr[las].rc, mid + 1, r, x);
}
int query(int lrt, int rrt, int l, int r, int rk) {
if (l == r) return l;
if (rk <= tr[tr[rrt].lc].val - tr[tr[lrt].lc].val) return query(tr[lrt].lc, tr[rrt].lc, l, mid, rk);
return query(tr[lrt].rc, tr[rrt].rc, mid + 1, r, rk - tr[tr[rrt].lc].val + tr[tr[lrt].lc].val);
}
#undef mid
int main()
{
#ifdef hany01
File("bzoj3551");
#endif
static int q, v, x, k, lastans;
n = read(), m = read(), q = read();
For(i, 1, n) h[i] = ls[i] = read();
sort(ls + 1, ls + 1 + n), tot = unique(ls + 1, ls + 1 + n) - ls - 1;
For(i, 1, n) h[i] = lower_bound(ls + 1, ls + 1 + tot, h[i]) - ls;
For(i, 1, m) E[i].u = read(), E[i].v = read(), E[i].w = read();
sort(E + 1, E + 1 + m), N = n;
For(i, 1, n) dsu_fa[i] = id[i] = i;
For(i, 1, m) {
int fu = find(E[i].u), fv = find(E[i].v);
if (fu != fv) hd[++ N] = E[i].w, add(N, id[fu]), add(N, id[fv]), id[dsu_fa[fu] = fv] = N;
}
DFS(N, N);
For(j, 1, 18) For(i, 1, N) fa[j][i] = fa[j - 1][fa[j - 1][i]];
For(i, 1, N)
if (lst[i] <= n) update(rt[i], rt[i - 1], 1, tot, h[lst[i]]);
else rt[i] = rt[i - 1];
while (q --) {
v = read(), x = read(), k = read();
if (lastans != -1) v ^= lastans, x ^= lastans, k ^= lastans;
Fordown(i, 18, 0) if (hd[fa[i][v]] <= x) v = fa[i][v];
x = tr[rt[ed[v]]].val - tr[rt[st[v] - 1]].val;
if (x < k) x = -1;
else x = query(rt[st[v] - 1], rt[ed[v]], 1, tot, x - k + 1);
printf("%d\n", lastans = (x == -1 ? -1 : ls[x]));
}
return 0;
}