1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <bits/stdc++.h> #define LOG 20 #define MAX_N 200000 #define MAX_M 500000 #define INF 0x3f3f3f3f #define mid ((s+t)>>1) using namespace std; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } vector <int> G[MAX_N+5]; int N, M, Q, sz, h[MAX_N+5], fa[MAX_N+5]; int cnt, ind, id[MAX_N+5], into[MAX_N+5], outo[MAX_N+5]; int anc[MAX_N+5][LOG+1], dep[MAX_N+5], rt[MAX_N+5]; int val[MAX_N+5], rev[MAX_N+5], tot; map <int, int> mp; struct edge {int u, v, c;} E[MAX_M+5]; struct node {int ls, rs, s;} tr[MAX_N*LOG]; void addedge(int u, int v) {G[u].push_back(v);} bool cmp(const edge &a, const edge &b) {return a.c < b.c;} int getf(int x) {return fa[x] == x ? x : fa[x] = getf(fa[x]);} void Kruskal() { cnt = 1, sort(E+1, E+M+1, cmp); for (int i = 1; i <= N; i++) fa[i] = i; for (int i = 1; i <= M && cnt < N; i++) { int u = getf(E[i].u), v = getf(E[i].v); if (u^v) fa[cnt+N] = cnt+N, fa[u] = fa[v] = cnt+N, addedge(cnt+N, u), addedge(cnt+N, v), h[cnt+N] = E[i].c, cnt++; } } void DFS(int u) { into[u] = ++ind, id[ind] = u; for (int i = 1; i <= LOG; i++) anc[u][i] = anc[anc[u][i-1]][i-1]; for (int i = 0, v; i < (int)G[u].size(); i++) anc[v = G[u][i]][0] = u, dep[v] = dep[u]+1, DFS(v); outo[u] = ind; } void modify(int v, int o, int s, int t, int p) { tr[v] = tr[o]; if (s == t) {tr[v].s++; return;} if (p <= mid) modify(tr[v].ls = ++sz, tr[o].ls, s, mid, p); if (p >= mid+1) modify(tr[v].rs = ++sz, tr[o].rs, mid+1, t, p); tr[v].s = tr[tr[v].ls].s+tr[tr[v].rs].s; } int query(int v1, int v2, int s, int t, int k) { if (s == t) return s; int rsz = tr[tr[v2].rs].s-tr[tr[v1].rs].s; if (k > rsz) return query(tr[v1].ls, tr[v2].ls, s, mid, k-rsz); return query(tr[v1].rs, tr[v2].rs, mid+1, t, k); } int getpos(int u, int c) { for (int i = LOG; ~i; i--) if (h[anc[u][i]] <= c) u = anc[u][i]; return u; } int getans(int u, int c, int k) { u = getpos(u, c); int l = into[u]-1, r = outo[u]; if (tr[rt[r]].s-tr[rt[l]].s < k) return -1; return rev[query(rt[l], rt[r], 1, N, k)]; } int main() { read(N), read(M), read(Q), h[0] = INF; for (int i = 1; i <= N; i++) read(h[i]); for (int i = 1; i <= N; i++) val[i] = h[i]; sort(val+1, val+N+1); for (int i = 1; i <= N; i++) if (i == 1 || (val[i]^val[i-1])) mp[val[i]] = ++tot, rev[tot] = val[i]; for (int i = 1; i <= N; i++) h[i] = mp[h[i]]; for (int i = 1, u, v, c; i <= M; i++) read(u), read(v), read(c), E[i] = (edge){u, v, c}; Kruskal(), DFS(getf(1)); for (int i = 1, p; i < N+N; i++) if ((p = id[i]) > N) rt[i] = rt[i-1]; else modify(rt[i] = ++sz, rt[i-1], 1, N, h[p]); for (int i = 1, lst = -1; i <= Q; i++) { int u, c, k; read(u), read(c), read(k); if (~lst) u ^= lst, c ^= lst, k ^= lst; printf("%d\n", lst = getans(u, c, k)); } return 0; }
|