这题洛谷的数据太…水,克鲁斯卡尔重构树不连通都可水过。

  • 3545: [ONTAK2010]Peaks
  • 3551: [ONTAK2010]Peaks加强版

在线算法:克鲁斯卡尔重构树套主席树。

在克鲁斯卡尔重构树上维护 DFS 序(或树链剖分)再套上主席树,维护第 $k$ 大。

当然非加强版由于你是重构树(被针对了)可能要大力卡常。比如加个 fread 以及离散化一下什么的。

代码( Fast IO 直接用了别人的板子不要在意 qwq):

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// ==============================
// author: memset0
// website: https://memset0.cn
// ==============================
#include <bits/stdc++.h>
#define ll long long
#define il inline
#define re register
#define DEBUG std::cerr << std::clock() << endl
#define rep(i,l,r) for (int i = l; i <= r; i++)
#define getc(x) getchar(x)
#define putc(x) putchar(x)

namespace FastIO {
const size_t str = 1 << 20;

struct Reader {
char buf[str], *s, *t;
Reader ( ) : s( ), t( ), buf() { }
inline char pick ( ) {
return (s == t) ? ( t = buf + fread ( s = buf, 1, str , stdin ), *s++ ) : ( *s++ );
}

template < class T >
inline Reader& operator >> ( T& x ) {
static char ch;
static short opt;
opt = (ch != 45);
while ( !isdigit ( ch = pick () ) && (ch ^ -1) && ( ch ^ 45 ) );
if ( ch == -1 ) return *this;
if ( ch == 45 ) { opt = 0; ch = pick (); }
for ( x = -48 + ch; isdigit ( ch = pick () ); ( x *= 10 ) += ch - 48 );
opt ? 1 : x = -x;
return *this;
}

} cin;

struct Writer {
char buf[str], *s, *t;
Writer () : s ( buf ), t( buf + str ), buf ( ) { }
~Writer () { fwrite( buf, 1, s - buf, stdout ); }

inline void echo ( char c ) {
( s == t ) ? ( fwrite ( s = buf, 1, str, stdout ), *s++ = c ) : ( *s++ = c );
}

inline Writer& operator << ( long long x ) {
if( !x ) return echo( 48 ), *this;
static int t[21], top;
if (x < 0) x = -x, echo ( '-' );
while ( x ) t[++top] = x % 10, x /= 10;
while ( top ) echo(t[top--] + 48);
return *this;
}
inline Writer& operator << (const char* s) {
while ( *s ) echo( *s++ ) ;
return *this;
}
} cout;
const char *endl = "\n";
}

using FastIO::cin;
using FastIO::cout;
using FastIO::endl;

const int N = 200010, M = 500010, E = N * 30;

int n, m, p, u, v, w, t, x, k, tn, cnt, pos, ord, lastans;
int b[N], fa[N], id[N], wid[N], val[N], siz[N];
int f[N][20], g[N][20];
int lc[E], rc[E], sum[E], root[N];
bool vis[N];

struct edge {
int u, v, w;
} e[M];
bool operator < (const edge &a, const edge &b) {
return a.w < b.w;
}

int tot = 2, hed[N], nxt[N << 1], to[N << 1];
il void add_edge(int u, int v) {
nxt[tot] = hed[u], to[tot] = v, hed[u] = tot++;
nxt[tot] = hed[v], to[tot] = u, hed[v] = tot++;
}

il int find(int u) {
if (fa[u] == u) return u;
return fa[u] = find(fa[u]);
}

void dfs(int u) {
siz[u] = 1, id[u] = ++pos, wid[id[u]] = u, vis[u] = 1;
for (int i = hed[u], v = to[i]; i; i = nxt[i], v = to[i])
if (v ^ f[u][0]) {
f[v][0] = u;
g[v][0] = (u <= n ? 0 : val[u]);
dfs(v);
siz[u] += siz[v];
}
}

void build(int &u, int v, int l, int r, int k) {
u = ++ord, lc[u] = lc[v], rc[u] = rc[v], sum[u] = sum[v] + 1;
if (l == r) return;
int mid = (l + r) >> 1;
if (k <= mid) build(lc[u], lc[v], l, mid, k);
else build(rc[u], rc[v], mid + 1, r, k);
}

int query(int u, int v, int l, int r, int k) {
if (l == r) return (k <= (sum[v] - sum[u]) ? b[l] : -1);
if (l == r) return l;
int mid = (l + r) >> 1, tmp;
tmp = sum[rc[v]] - sum[rc[u]];
if (k <= tmp)
return query(rc[u], rc[v], mid + 1, r, k);
else
return query(lc[u], lc[v], l, mid, k - tmp);
}

int main() {

cin >> n >> m >> p;
for (re int i = 1; i <= n; i++)
cin >> val[i];
for (re int i = 1; i <= m; i++)
cin >> e[i].u >> e[i].v >> e[i].w;

std::sort(e + 1, e + m + 1);
for (re int i = 1; i <= (n << 1); i++)
fa[i] = i;
cnt = n;

for (re int i = 1; i <= n; i++)
b[i] = val[i];
std::sort(b + 1, b + n + 1);
tn = std::unique(b + 1, b + n + 1) - b - 1;
for (re int i = 1; i <= n; i++)
val[i] = std::lower_bound(b + 1, b + tn + 1, val[i]) - b;

for (re int i = 1; i <= m; i++) {
u = e[i].u, v = e[i].v, w = e[i].w;
if (find(u) ^ find(v)) {
t = ++cnt;
u = find(u), v = find(v);
// printf("== %d %d %d ==\n", u, v, w);
fa[u] = fa[v] = t;
add_edge(u, t);
add_edge(v, t);
val[t] = w;
}
}

val[++cnt] = 1e9;
for (re int i = 1; i < cnt; i++)
if (find(i) != find(cnt)) {
add_edge(find(i), cnt);
fa[find(i)] = find(cnt);
}

dfs(cnt);
for (re int i = 1; i <= cnt; i++)
if (wid[i] <= n)
build(root[i], root[i - 1], 1, tn, val[wid[i]]);
else
root[i] = root[i - 1];

for (re int i = 1; i <= 19; i++)
for (re int j = 1; j <= cnt; j++) {
f[j][i] = f[f[j][i - 1]][i - 1];
g[j][i] = std::max(g[j][i - 1], g[f[j][i - 1]][i - 1]);
}

// DEBUG;
for (re int i = 1; i <= p; i++) {
cin >> u >> x >> k;
if (~lastans) {
u ^= lastans;
x ^= lastans;
k ^= lastans;
}
for (re int i = 19; i >= 0; i--)
if (x >= g[u][i] && f[u][i]) {
u = f[u][i];
}
cout << (lastans = query(root[id[u] - 1], root[id[u] + siz[u] - 1], 1, tn, k)) << endl;
}

return 0;
}