//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
//#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2003, K = 12;
int n, m, k, a[N], tin[N], tout[N], timer, up[K][N], h[N];
vector <int> q[N];
void dfs(int v, int p = 1) {
if (v != 1) {
h[v] = h[p] + 1;
}
tin[v] = ++timer;
up[0][v] = p;
for (int i = 1; i < K; i++) {
up[i][v] = up[i - 1][up[i - 1][v]];
}
for (auto i : q[v]) {
if (i == p) {
continue;
}
dfs(i, v);
}
tout[v] = timer;
}
int w(int x, int y) {
assert(tin[x] >= tin[y] && tout[x] <= tout[y]);
for (int i = K - 1; i >= 0; i--) {
if (h[up[i][x]] > h[y]) {
x = up[i][x];
}
}
return x;
}
void output(int x, int y) {
if (x > y) {
swap(x, y);
}
cout << x << " " << y << endl;
}
void getans(int l, int r, int v) {
int x = -1, x2 = -1, y = -1, y2 = -1;
for (int j = l; j <= r; j++) {
int i = a[j];
if (tin[i] < tin[v] || tout[i] > tout[v]) {
if (a[x] == v && a[y] == v) {
output(x, y);
return;
}
if (a[x2] == v && a[y2] == v) {
output(x2, y2);
return;
}
if (x != -1 && y != -1) {
if (tin[a[y]] < tin[w(a[x], v)]) {
output(x, y);
return;
}
}
if (x2 != -1 && y2 != -1) {
if (tout[a[y2]] > tout[w(a[x2], v)]) {
output(x2, y2);
return;
}
}
x = -1, x2 = -1, y = -1, y2 = -1;
} else {
if (y == -1 || tin[a[y]] > tin[i]) {
y = j;
}
if (y2 == -1 || tout[a[y2]] < tout[i]) {
y2 = j;
}
if (x == -1 || tin[w(a[x], v)] < tin[w(i, v)]) {
x = j;
}
if (x2 == -1 || tout[w(a[x2], v)] > tout[w(i, v)]) {
x2 = j;
}
}
}
if (a[x] == v && a[y] == v) {
output(x, y);
return;
}
if (a[x2] == v && a[y2] == v) {
output(x2, y2);
return;
}
if (x != -1 && y != -1) {
if (tin[a[y]] < tin[w(a[x], v)]) {
output(x, y);
return;
}
}
if (x2 != -1 && y2 != -1) {
if (tout[a[y2]] > tout[w(a[x2], v)]) {
output(x2, y2);
return;
}
}
cout << "-1 -1" << endl;
return;
}
void solve(int tc) {
cin >> n >> m >> k;
for (int i = 1; i <= n - 1; i++) {
int x, y;
cin >> x >> y;
q[x].pb(y);
q[y].pb(x);
}
dfs(1);
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
for (int i = 1; i <= k; i++) {
int tp;
cin >> tp;
if (tp == 1) {
int x, y;
cin >> x >> y;
a[x] = y;
} else {
int l, r, v;
cin >> l >> r >> v;
getans(l, r, v);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 0; i < tc; i++) {
solve(i);
// cleanup();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
n=5 |
2 |
Correct |
2 ms |
504 KB |
n=100 |
3 |
Correct |
3 ms |
504 KB |
n=100 |
4 |
Correct |
2 ms |
504 KB |
n=100 |
5 |
Correct |
2 ms |
504 KB |
n=100 |
6 |
Correct |
2 ms |
504 KB |
n=100 |
7 |
Correct |
2 ms |
504 KB |
n=100 |
8 |
Correct |
2 ms |
504 KB |
n=100 |
9 |
Correct |
2 ms |
504 KB |
n=100 |
10 |
Correct |
2 ms |
504 KB |
n=100 |
11 |
Correct |
2 ms |
504 KB |
n=100 |
12 |
Correct |
2 ms |
476 KB |
n=100 |
13 |
Correct |
2 ms |
504 KB |
n=100 |
14 |
Correct |
2 ms |
476 KB |
n=100 |
15 |
Correct |
3 ms |
504 KB |
n=100 |
16 |
Correct |
3 ms |
504 KB |
n=100 |
17 |
Correct |
2 ms |
504 KB |
n=100 |
18 |
Correct |
3 ms |
504 KB |
n=100 |
19 |
Correct |
3 ms |
504 KB |
n=100 |
20 |
Correct |
3 ms |
508 KB |
n=100 |
21 |
Correct |
2 ms |
504 KB |
n=100 |
22 |
Correct |
3 ms |
504 KB |
n=100 |
23 |
Correct |
3 ms |
504 KB |
n=100 |
24 |
Correct |
3 ms |
504 KB |
n=100 |
25 |
Correct |
3 ms |
504 KB |
n=100 |
26 |
Correct |
2 ms |
504 KB |
n=12 |
27 |
Correct |
2 ms |
504 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
n=5 |
2 |
Correct |
2 ms |
504 KB |
n=100 |
3 |
Correct |
3 ms |
504 KB |
n=100 |
4 |
Correct |
2 ms |
504 KB |
n=100 |
5 |
Correct |
2 ms |
504 KB |
n=100 |
6 |
Correct |
2 ms |
504 KB |
n=100 |
7 |
Correct |
2 ms |
504 KB |
n=100 |
8 |
Correct |
2 ms |
504 KB |
n=100 |
9 |
Correct |
2 ms |
504 KB |
n=100 |
10 |
Correct |
2 ms |
504 KB |
n=100 |
11 |
Correct |
2 ms |
504 KB |
n=100 |
12 |
Correct |
2 ms |
476 KB |
n=100 |
13 |
Correct |
2 ms |
504 KB |
n=100 |
14 |
Correct |
2 ms |
476 KB |
n=100 |
15 |
Correct |
3 ms |
504 KB |
n=100 |
16 |
Correct |
3 ms |
504 KB |
n=100 |
17 |
Correct |
2 ms |
504 KB |
n=100 |
18 |
Correct |
3 ms |
504 KB |
n=100 |
19 |
Correct |
3 ms |
504 KB |
n=100 |
20 |
Correct |
3 ms |
508 KB |
n=100 |
21 |
Correct |
2 ms |
504 KB |
n=100 |
22 |
Correct |
3 ms |
504 KB |
n=100 |
23 |
Correct |
3 ms |
504 KB |
n=100 |
24 |
Correct |
3 ms |
504 KB |
n=100 |
25 |
Correct |
3 ms |
504 KB |
n=100 |
26 |
Correct |
2 ms |
504 KB |
n=12 |
27 |
Correct |
2 ms |
504 KB |
n=100 |
28 |
Correct |
11 ms |
504 KB |
n=500 |
29 |
Correct |
5 ms |
504 KB |
n=500 |
30 |
Correct |
5 ms |
504 KB |
n=500 |
31 |
Correct |
5 ms |
504 KB |
n=500 |
32 |
Correct |
12 ms |
504 KB |
n=500 |
33 |
Correct |
5 ms |
504 KB |
n=500 |
34 |
Correct |
12 ms |
504 KB |
n=500 |
35 |
Correct |
5 ms |
504 KB |
n=500 |
36 |
Correct |
9 ms |
504 KB |
n=500 |
37 |
Correct |
9 ms |
580 KB |
n=500 |
38 |
Correct |
9 ms |
504 KB |
n=500 |
39 |
Correct |
6 ms |
504 KB |
n=500 |
40 |
Correct |
6 ms |
504 KB |
n=500 |
41 |
Correct |
6 ms |
504 KB |
n=500 |
42 |
Correct |
6 ms |
504 KB |
n=500 |
43 |
Correct |
6 ms |
504 KB |
n=500 |
44 |
Correct |
6 ms |
504 KB |
n=500 |
45 |
Correct |
10 ms |
504 KB |
n=500 |
46 |
Correct |
5 ms |
504 KB |
n=500 |
47 |
Correct |
5 ms |
504 KB |
n=500 |
48 |
Correct |
10 ms |
504 KB |
n=500 |
49 |
Correct |
4 ms |
504 KB |
n=500 |
50 |
Correct |
3 ms |
504 KB |
n=500 |
51 |
Correct |
4 ms |
504 KB |
n=500 |
52 |
Correct |
6 ms |
504 KB |
n=500 |
53 |
Correct |
4 ms |
508 KB |
n=500 |
54 |
Correct |
22 ms |
504 KB |
n=500 |
55 |
Correct |
3 ms |
504 KB |
n=278 |
56 |
Correct |
22 ms |
504 KB |
n=500 |
57 |
Correct |
23 ms |
504 KB |
n=500 |
58 |
Correct |
3 ms |
548 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
n=5 |
2 |
Correct |
2 ms |
504 KB |
n=100 |
3 |
Correct |
3 ms |
504 KB |
n=100 |
4 |
Correct |
2 ms |
504 KB |
n=100 |
5 |
Correct |
2 ms |
504 KB |
n=100 |
6 |
Correct |
2 ms |
504 KB |
n=100 |
7 |
Correct |
2 ms |
504 KB |
n=100 |
8 |
Correct |
2 ms |
504 KB |
n=100 |
9 |
Correct |
2 ms |
504 KB |
n=100 |
10 |
Correct |
2 ms |
504 KB |
n=100 |
11 |
Correct |
2 ms |
504 KB |
n=100 |
12 |
Correct |
2 ms |
476 KB |
n=100 |
13 |
Correct |
2 ms |
504 KB |
n=100 |
14 |
Correct |
2 ms |
476 KB |
n=100 |
15 |
Correct |
3 ms |
504 KB |
n=100 |
16 |
Correct |
3 ms |
504 KB |
n=100 |
17 |
Correct |
2 ms |
504 KB |
n=100 |
18 |
Correct |
3 ms |
504 KB |
n=100 |
19 |
Correct |
3 ms |
504 KB |
n=100 |
20 |
Correct |
3 ms |
508 KB |
n=100 |
21 |
Correct |
2 ms |
504 KB |
n=100 |
22 |
Correct |
3 ms |
504 KB |
n=100 |
23 |
Correct |
3 ms |
504 KB |
n=100 |
24 |
Correct |
3 ms |
504 KB |
n=100 |
25 |
Correct |
3 ms |
504 KB |
n=100 |
26 |
Correct |
2 ms |
504 KB |
n=12 |
27 |
Correct |
2 ms |
504 KB |
n=100 |
28 |
Correct |
11 ms |
504 KB |
n=500 |
29 |
Correct |
5 ms |
504 KB |
n=500 |
30 |
Correct |
5 ms |
504 KB |
n=500 |
31 |
Correct |
5 ms |
504 KB |
n=500 |
32 |
Correct |
12 ms |
504 KB |
n=500 |
33 |
Correct |
5 ms |
504 KB |
n=500 |
34 |
Correct |
12 ms |
504 KB |
n=500 |
35 |
Correct |
5 ms |
504 KB |
n=500 |
36 |
Correct |
9 ms |
504 KB |
n=500 |
37 |
Correct |
9 ms |
580 KB |
n=500 |
38 |
Correct |
9 ms |
504 KB |
n=500 |
39 |
Correct |
6 ms |
504 KB |
n=500 |
40 |
Correct |
6 ms |
504 KB |
n=500 |
41 |
Correct |
6 ms |
504 KB |
n=500 |
42 |
Correct |
6 ms |
504 KB |
n=500 |
43 |
Correct |
6 ms |
504 KB |
n=500 |
44 |
Correct |
6 ms |
504 KB |
n=500 |
45 |
Correct |
10 ms |
504 KB |
n=500 |
46 |
Correct |
5 ms |
504 KB |
n=500 |
47 |
Correct |
5 ms |
504 KB |
n=500 |
48 |
Correct |
10 ms |
504 KB |
n=500 |
49 |
Correct |
4 ms |
504 KB |
n=500 |
50 |
Correct |
3 ms |
504 KB |
n=500 |
51 |
Correct |
4 ms |
504 KB |
n=500 |
52 |
Correct |
6 ms |
504 KB |
n=500 |
53 |
Correct |
4 ms |
508 KB |
n=500 |
54 |
Correct |
22 ms |
504 KB |
n=500 |
55 |
Correct |
3 ms |
504 KB |
n=278 |
56 |
Correct |
22 ms |
504 KB |
n=500 |
57 |
Correct |
23 ms |
504 KB |
n=500 |
58 |
Correct |
3 ms |
548 KB |
n=500 |
59 |
Correct |
151 ms |
692 KB |
n=2000 |
60 |
Correct |
46 ms |
760 KB |
n=2000 |
61 |
Correct |
42 ms |
632 KB |
n=2000 |
62 |
Correct |
32 ms |
636 KB |
n=2000 |
63 |
Correct |
163 ms |
760 KB |
n=2000 |
64 |
Correct |
40 ms |
632 KB |
n=2000 |
65 |
Correct |
165 ms |
632 KB |
n=2000 |
66 |
Correct |
48 ms |
760 KB |
n=2000 |
67 |
Correct |
91 ms |
632 KB |
n=2000 |
68 |
Correct |
42 ms |
636 KB |
n=2000 |
69 |
Correct |
113 ms |
632 KB |
n=2000 |
70 |
Correct |
114 ms |
632 KB |
n=2000 |
71 |
Correct |
114 ms |
632 KB |
n=2000 |
72 |
Correct |
59 ms |
680 KB |
n=2000 |
73 |
Correct |
59 ms |
632 KB |
n=2000 |
74 |
Correct |
129 ms |
632 KB |
n=1844 |
75 |
Correct |
59 ms |
716 KB |
n=2000 |
76 |
Correct |
68 ms |
636 KB |
n=2000 |
77 |
Correct |
65 ms |
632 KB |
n=2000 |
78 |
Correct |
73 ms |
632 KB |
n=2000 |
79 |
Correct |
151 ms |
632 KB |
n=2000 |
80 |
Correct |
39 ms |
632 KB |
n=2000 |
81 |
Correct |
41 ms |
632 KB |
n=2000 |
82 |
Correct |
134 ms |
632 KB |
n=2000 |
83 |
Correct |
39 ms |
888 KB |
n=2000 |
84 |
Correct |
8 ms |
576 KB |
n=2000 |
85 |
Correct |
32 ms |
632 KB |
n=2000 |
86 |
Correct |
25 ms |
632 KB |
n=2000 |
87 |
Correct |
9 ms |
632 KB |
n=2000 |
88 |
Correct |
379 ms |
772 KB |
n=2000 |
89 |
Correct |
371 ms |
892 KB |
n=2000 |
90 |
Correct |
310 ms |
760 KB |
n=2000 |
91 |
Correct |
13 ms |
632 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
n=5 |
2 |
Correct |
2 ms |
504 KB |
n=100 |
3 |
Correct |
3 ms |
504 KB |
n=100 |
4 |
Correct |
2 ms |
504 KB |
n=100 |
5 |
Correct |
2 ms |
504 KB |
n=100 |
6 |
Correct |
2 ms |
504 KB |
n=100 |
7 |
Correct |
2 ms |
504 KB |
n=100 |
8 |
Correct |
2 ms |
504 KB |
n=100 |
9 |
Correct |
2 ms |
504 KB |
n=100 |
10 |
Correct |
2 ms |
504 KB |
n=100 |
11 |
Correct |
2 ms |
504 KB |
n=100 |
12 |
Correct |
2 ms |
476 KB |
n=100 |
13 |
Correct |
2 ms |
504 KB |
n=100 |
14 |
Correct |
2 ms |
476 KB |
n=100 |
15 |
Correct |
3 ms |
504 KB |
n=100 |
16 |
Correct |
3 ms |
504 KB |
n=100 |
17 |
Correct |
2 ms |
504 KB |
n=100 |
18 |
Correct |
3 ms |
504 KB |
n=100 |
19 |
Correct |
3 ms |
504 KB |
n=100 |
20 |
Correct |
3 ms |
508 KB |
n=100 |
21 |
Correct |
2 ms |
504 KB |
n=100 |
22 |
Correct |
3 ms |
504 KB |
n=100 |
23 |
Correct |
3 ms |
504 KB |
n=100 |
24 |
Correct |
3 ms |
504 KB |
n=100 |
25 |
Correct |
3 ms |
504 KB |
n=100 |
26 |
Correct |
2 ms |
504 KB |
n=12 |
27 |
Correct |
2 ms |
504 KB |
n=100 |
28 |
Correct |
11 ms |
504 KB |
n=500 |
29 |
Correct |
5 ms |
504 KB |
n=500 |
30 |
Correct |
5 ms |
504 KB |
n=500 |
31 |
Correct |
5 ms |
504 KB |
n=500 |
32 |
Correct |
12 ms |
504 KB |
n=500 |
33 |
Correct |
5 ms |
504 KB |
n=500 |
34 |
Correct |
12 ms |
504 KB |
n=500 |
35 |
Correct |
5 ms |
504 KB |
n=500 |
36 |
Correct |
9 ms |
504 KB |
n=500 |
37 |
Correct |
9 ms |
580 KB |
n=500 |
38 |
Correct |
9 ms |
504 KB |
n=500 |
39 |
Correct |
6 ms |
504 KB |
n=500 |
40 |
Correct |
6 ms |
504 KB |
n=500 |
41 |
Correct |
6 ms |
504 KB |
n=500 |
42 |
Correct |
6 ms |
504 KB |
n=500 |
43 |
Correct |
6 ms |
504 KB |
n=500 |
44 |
Correct |
6 ms |
504 KB |
n=500 |
45 |
Correct |
10 ms |
504 KB |
n=500 |
46 |
Correct |
5 ms |
504 KB |
n=500 |
47 |
Correct |
5 ms |
504 KB |
n=500 |
48 |
Correct |
10 ms |
504 KB |
n=500 |
49 |
Correct |
4 ms |
504 KB |
n=500 |
50 |
Correct |
3 ms |
504 KB |
n=500 |
51 |
Correct |
4 ms |
504 KB |
n=500 |
52 |
Correct |
6 ms |
504 KB |
n=500 |
53 |
Correct |
4 ms |
508 KB |
n=500 |
54 |
Correct |
22 ms |
504 KB |
n=500 |
55 |
Correct |
3 ms |
504 KB |
n=278 |
56 |
Correct |
22 ms |
504 KB |
n=500 |
57 |
Correct |
23 ms |
504 KB |
n=500 |
58 |
Correct |
3 ms |
548 KB |
n=500 |
59 |
Correct |
151 ms |
692 KB |
n=2000 |
60 |
Correct |
46 ms |
760 KB |
n=2000 |
61 |
Correct |
42 ms |
632 KB |
n=2000 |
62 |
Correct |
32 ms |
636 KB |
n=2000 |
63 |
Correct |
163 ms |
760 KB |
n=2000 |
64 |
Correct |
40 ms |
632 KB |
n=2000 |
65 |
Correct |
165 ms |
632 KB |
n=2000 |
66 |
Correct |
48 ms |
760 KB |
n=2000 |
67 |
Correct |
91 ms |
632 KB |
n=2000 |
68 |
Correct |
42 ms |
636 KB |
n=2000 |
69 |
Correct |
113 ms |
632 KB |
n=2000 |
70 |
Correct |
114 ms |
632 KB |
n=2000 |
71 |
Correct |
114 ms |
632 KB |
n=2000 |
72 |
Correct |
59 ms |
680 KB |
n=2000 |
73 |
Correct |
59 ms |
632 KB |
n=2000 |
74 |
Correct |
129 ms |
632 KB |
n=1844 |
75 |
Correct |
59 ms |
716 KB |
n=2000 |
76 |
Correct |
68 ms |
636 KB |
n=2000 |
77 |
Correct |
65 ms |
632 KB |
n=2000 |
78 |
Correct |
73 ms |
632 KB |
n=2000 |
79 |
Correct |
151 ms |
632 KB |
n=2000 |
80 |
Correct |
39 ms |
632 KB |
n=2000 |
81 |
Correct |
41 ms |
632 KB |
n=2000 |
82 |
Correct |
134 ms |
632 KB |
n=2000 |
83 |
Correct |
39 ms |
888 KB |
n=2000 |
84 |
Correct |
8 ms |
576 KB |
n=2000 |
85 |
Correct |
32 ms |
632 KB |
n=2000 |
86 |
Correct |
25 ms |
632 KB |
n=2000 |
87 |
Correct |
9 ms |
632 KB |
n=2000 |
88 |
Correct |
379 ms |
772 KB |
n=2000 |
89 |
Correct |
371 ms |
892 KB |
n=2000 |
90 |
Correct |
310 ms |
760 KB |
n=2000 |
91 |
Correct |
13 ms |
632 KB |
n=2000 |
92 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
93 |
Halted |
0 ms |
0 KB |
- |