/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
const int N = 3e3 + 14;
struct New {
int v, l, r, i;
};
int n, m, q, level[N], up[15][N], a[N], t[4 * N];
vector<vector<int>> g(N);
void dfs(int v, int pr) {
level[v] = level[pr] + 1;
up[0][v] = pr;
for (int i = 1; i <= 9; i++)
up[i][v] = up[i - 1][up[i - 1][v]];
for (int to: g[v]) {
if (to == pr)
continue;
dfs(to, v);
}
}
int lca(int x, int y) {
if (level[x] > level[y])
swap(x, y);
for (int i = 9; i >= 0; i--) {
if (level[up[i][y]] >= level[x])
y = up[i][y];
}
if (x == y)
return x;
for (int i = 9; i >= 0; i--) {
if (up[i][x] != up[i][y])
x = up[i][x], y = up[i][y];
}
return up[0][x];
}
int not_lca(int x, int y) {
if (level[x] > level[y])
swap(x, y);
for (int i = 9; i >= 0; i--) {
if (level[up[i][y]] > level[x])
y = up[i][y];
}
if (up[0][y] == x)
return y;
return -1;
}
void build(int x, int lx, int rx) {
if (lx == rx)
t[x] = a[lx];
else {
int mid = (lx + rx) / 2;
build(2 * x + 1, lx, mid);
build(2 * x + 2, mid + 1, rx);
t[x] = lca(t[2 * x + 1], t[2 * x + 2]);
}
}
int get(int l, int r, int x, int lx, int rx) {
if (rx < l || r < lx) return -1;
if (l <= lx && rx <= r) return t[x];
int mid = (lx + rx) / 2;
int r1 = get(l, r, 2 * x + 1, lx, mid);
int r2 = get(l, r, 2 * x + 2, mid + 1, rx);
if (r1 == -1 && r2 == -1)
return -1;
if (r1 == -1)
return r2;
if (r2 == -1)
return r1;
return lca(r1, r2);
}
void upd(int i, int v, int x, int lx, int rx) {
if (lx == rx)
t[x] = v;
else {
int mid = (lx + rx) / 2;
if (i <= mid)
upd(i, v, 2 * x + 1, lx, mid);
else
upd(i, v, 2 * x + 2, mid + 1, rx);
t[x] = lca(t[2 * x + 1], t[2 * x + 2]);
}
}
void solve() {
cin >> n >> m >> q;
for (int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].push_back(y), g[y].push_back(x);
}
dfs(1, 0);
for (int i = 1; i <= m; i++)
cin >> a[i];
vector<int> b(m + 1);
for (int i = 1; i < m; i++)
b[i] = lca(a[i], a[i + 1]);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int pos, v;
cin >> pos >> v;
a[pos] = v;
if (pos + 1 <= m)
b[pos] = lca(a[pos], a[pos + 1]);
if (pos - 1 >= 1)
b[pos - 1] = lca(a[pos - 1], a[pos]);
} else {
int l, r, v;
cin >> l >> r >> v;
bool found = 0;
for (int i = l; i <= r; i++) {
if (a[i] == v) {
cout << i << ' ' << i << '\n';
found = 1;
break;
}
}
if (found)
continue;
for (int i = l; i < r; i++)
if (b[i] == v) {
cout << i << ' ' << i + 1 << '\n';
found = 1;
break;
}
if (!found)
cout << "-1 -1\n";
}
}
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message
treearray.cpp: In function 'void setIO(const string&)':
treearray.cpp:211:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
211 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:212:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
212 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
332 KB |
n=100 |
3 |
Correct |
1 ms |
332 KB |
n=100 |
4 |
Correct |
1 ms |
332 KB |
n=100 |
5 |
Correct |
0 ms |
332 KB |
n=100 |
6 |
Correct |
1 ms |
332 KB |
n=100 |
7 |
Correct |
1 ms |
332 KB |
n=100 |
8 |
Correct |
0 ms |
332 KB |
n=100 |
9 |
Correct |
1 ms |
332 KB |
n=100 |
10 |
Correct |
1 ms |
332 KB |
n=100 |
11 |
Correct |
1 ms |
332 KB |
n=100 |
12 |
Correct |
1 ms |
332 KB |
n=100 |
13 |
Correct |
0 ms |
332 KB |
n=100 |
14 |
Correct |
1 ms |
332 KB |
n=100 |
15 |
Correct |
1 ms |
332 KB |
n=100 |
16 |
Correct |
1 ms |
332 KB |
n=100 |
17 |
Correct |
1 ms |
332 KB |
n=100 |
18 |
Correct |
1 ms |
332 KB |
n=100 |
19 |
Correct |
1 ms |
332 KB |
n=100 |
20 |
Correct |
0 ms |
332 KB |
n=100 |
21 |
Correct |
1 ms |
332 KB |
n=100 |
22 |
Correct |
0 ms |
332 KB |
n=100 |
23 |
Correct |
1 ms |
332 KB |
n=100 |
24 |
Correct |
1 ms |
332 KB |
n=100 |
25 |
Correct |
1 ms |
332 KB |
n=100 |
26 |
Correct |
0 ms |
332 KB |
n=12 |
27 |
Correct |
1 ms |
332 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
332 KB |
n=100 |
3 |
Correct |
1 ms |
332 KB |
n=100 |
4 |
Correct |
1 ms |
332 KB |
n=100 |
5 |
Correct |
0 ms |
332 KB |
n=100 |
6 |
Correct |
1 ms |
332 KB |
n=100 |
7 |
Correct |
1 ms |
332 KB |
n=100 |
8 |
Correct |
0 ms |
332 KB |
n=100 |
9 |
Correct |
1 ms |
332 KB |
n=100 |
10 |
Correct |
1 ms |
332 KB |
n=100 |
11 |
Correct |
1 ms |
332 KB |
n=100 |
12 |
Correct |
1 ms |
332 KB |
n=100 |
13 |
Correct |
0 ms |
332 KB |
n=100 |
14 |
Correct |
1 ms |
332 KB |
n=100 |
15 |
Correct |
1 ms |
332 KB |
n=100 |
16 |
Correct |
1 ms |
332 KB |
n=100 |
17 |
Correct |
1 ms |
332 KB |
n=100 |
18 |
Correct |
1 ms |
332 KB |
n=100 |
19 |
Correct |
1 ms |
332 KB |
n=100 |
20 |
Correct |
0 ms |
332 KB |
n=100 |
21 |
Correct |
1 ms |
332 KB |
n=100 |
22 |
Correct |
0 ms |
332 KB |
n=100 |
23 |
Correct |
1 ms |
332 KB |
n=100 |
24 |
Correct |
1 ms |
332 KB |
n=100 |
25 |
Correct |
1 ms |
332 KB |
n=100 |
26 |
Correct |
0 ms |
332 KB |
n=12 |
27 |
Correct |
1 ms |
332 KB |
n=100 |
28 |
Correct |
1 ms |
460 KB |
n=500 |
29 |
Correct |
1 ms |
460 KB |
n=500 |
30 |
Correct |
1 ms |
460 KB |
n=500 |
31 |
Correct |
1 ms |
460 KB |
n=500 |
32 |
Correct |
1 ms |
460 KB |
n=500 |
33 |
Correct |
1 ms |
460 KB |
n=500 |
34 |
Correct |
1 ms |
460 KB |
n=500 |
35 |
Correct |
1 ms |
460 KB |
n=500 |
36 |
Correct |
2 ms |
460 KB |
n=500 |
37 |
Correct |
1 ms |
460 KB |
n=500 |
38 |
Correct |
1 ms |
460 KB |
n=500 |
39 |
Correct |
1 ms |
460 KB |
n=500 |
40 |
Correct |
1 ms |
460 KB |
n=500 |
41 |
Correct |
1 ms |
460 KB |
n=500 |
42 |
Correct |
1 ms |
460 KB |
n=500 |
43 |
Correct |
1 ms |
460 KB |
n=500 |
44 |
Correct |
1 ms |
460 KB |
n=500 |
45 |
Correct |
1 ms |
460 KB |
n=500 |
46 |
Correct |
1 ms |
460 KB |
n=500 |
47 |
Correct |
1 ms |
460 KB |
n=500 |
48 |
Correct |
1 ms |
460 KB |
n=500 |
49 |
Correct |
2 ms |
396 KB |
n=500 |
50 |
Correct |
1 ms |
460 KB |
n=500 |
51 |
Correct |
1 ms |
460 KB |
n=500 |
52 |
Correct |
1 ms |
460 KB |
n=500 |
53 |
Correct |
1 ms |
460 KB |
n=500 |
54 |
Correct |
1 ms |
460 KB |
n=500 |
55 |
Correct |
1 ms |
460 KB |
n=278 |
56 |
Correct |
1 ms |
460 KB |
n=500 |
57 |
Correct |
2 ms |
460 KB |
n=500 |
58 |
Correct |
2 ms |
460 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
332 KB |
n=100 |
3 |
Correct |
1 ms |
332 KB |
n=100 |
4 |
Correct |
1 ms |
332 KB |
n=100 |
5 |
Correct |
0 ms |
332 KB |
n=100 |
6 |
Correct |
1 ms |
332 KB |
n=100 |
7 |
Correct |
1 ms |
332 KB |
n=100 |
8 |
Correct |
0 ms |
332 KB |
n=100 |
9 |
Correct |
1 ms |
332 KB |
n=100 |
10 |
Correct |
1 ms |
332 KB |
n=100 |
11 |
Correct |
1 ms |
332 KB |
n=100 |
12 |
Correct |
1 ms |
332 KB |
n=100 |
13 |
Correct |
0 ms |
332 KB |
n=100 |
14 |
Correct |
1 ms |
332 KB |
n=100 |
15 |
Correct |
1 ms |
332 KB |
n=100 |
16 |
Correct |
1 ms |
332 KB |
n=100 |
17 |
Correct |
1 ms |
332 KB |
n=100 |
18 |
Correct |
1 ms |
332 KB |
n=100 |
19 |
Correct |
1 ms |
332 KB |
n=100 |
20 |
Correct |
0 ms |
332 KB |
n=100 |
21 |
Correct |
1 ms |
332 KB |
n=100 |
22 |
Correct |
0 ms |
332 KB |
n=100 |
23 |
Correct |
1 ms |
332 KB |
n=100 |
24 |
Correct |
1 ms |
332 KB |
n=100 |
25 |
Correct |
1 ms |
332 KB |
n=100 |
26 |
Correct |
0 ms |
332 KB |
n=12 |
27 |
Correct |
1 ms |
332 KB |
n=100 |
28 |
Correct |
1 ms |
460 KB |
n=500 |
29 |
Correct |
1 ms |
460 KB |
n=500 |
30 |
Correct |
1 ms |
460 KB |
n=500 |
31 |
Correct |
1 ms |
460 KB |
n=500 |
32 |
Correct |
1 ms |
460 KB |
n=500 |
33 |
Correct |
1 ms |
460 KB |
n=500 |
34 |
Correct |
1 ms |
460 KB |
n=500 |
35 |
Correct |
1 ms |
460 KB |
n=500 |
36 |
Correct |
2 ms |
460 KB |
n=500 |
37 |
Correct |
1 ms |
460 KB |
n=500 |
38 |
Correct |
1 ms |
460 KB |
n=500 |
39 |
Correct |
1 ms |
460 KB |
n=500 |
40 |
Correct |
1 ms |
460 KB |
n=500 |
41 |
Correct |
1 ms |
460 KB |
n=500 |
42 |
Correct |
1 ms |
460 KB |
n=500 |
43 |
Correct |
1 ms |
460 KB |
n=500 |
44 |
Correct |
1 ms |
460 KB |
n=500 |
45 |
Correct |
1 ms |
460 KB |
n=500 |
46 |
Correct |
1 ms |
460 KB |
n=500 |
47 |
Correct |
1 ms |
460 KB |
n=500 |
48 |
Correct |
1 ms |
460 KB |
n=500 |
49 |
Correct |
2 ms |
396 KB |
n=500 |
50 |
Correct |
1 ms |
460 KB |
n=500 |
51 |
Correct |
1 ms |
460 KB |
n=500 |
52 |
Correct |
1 ms |
460 KB |
n=500 |
53 |
Correct |
1 ms |
460 KB |
n=500 |
54 |
Correct |
1 ms |
460 KB |
n=500 |
55 |
Correct |
1 ms |
460 KB |
n=278 |
56 |
Correct |
1 ms |
460 KB |
n=500 |
57 |
Correct |
2 ms |
460 KB |
n=500 |
58 |
Correct |
2 ms |
460 KB |
n=500 |
59 |
Correct |
2 ms |
588 KB |
n=2000 |
60 |
Correct |
2 ms |
696 KB |
n=2000 |
61 |
Correct |
2 ms |
588 KB |
n=2000 |
62 |
Correct |
2 ms |
588 KB |
n=2000 |
63 |
Correct |
3 ms |
588 KB |
n=2000 |
64 |
Correct |
2 ms |
588 KB |
n=2000 |
65 |
Correct |
2 ms |
588 KB |
n=2000 |
66 |
Correct |
3 ms |
588 KB |
n=2000 |
67 |
Correct |
4 ms |
588 KB |
n=2000 |
68 |
Correct |
2 ms |
588 KB |
n=2000 |
69 |
Correct |
7 ms |
616 KB |
n=2000 |
70 |
Correct |
5 ms |
604 KB |
n=2000 |
71 |
Correct |
4 ms |
588 KB |
n=2000 |
72 |
Correct |
3 ms |
588 KB |
n=2000 |
73 |
Correct |
3 ms |
588 KB |
n=2000 |
74 |
Correct |
2 ms |
588 KB |
n=1844 |
75 |
Correct |
3 ms |
588 KB |
n=2000 |
76 |
Correct |
5 ms |
588 KB |
n=2000 |
77 |
Correct |
4 ms |
568 KB |
n=2000 |
78 |
Correct |
3 ms |
588 KB |
n=2000 |
79 |
Correct |
2 ms |
588 KB |
n=2000 |
80 |
Correct |
2 ms |
588 KB |
n=2000 |
81 |
Correct |
3 ms |
588 KB |
n=2000 |
82 |
Correct |
2 ms |
588 KB |
n=2000 |
83 |
Correct |
2 ms |
588 KB |
n=2000 |
84 |
Correct |
4 ms |
588 KB |
n=2000 |
85 |
Correct |
3 ms |
588 KB |
n=2000 |
86 |
Correct |
3 ms |
588 KB |
n=2000 |
87 |
Correct |
3 ms |
588 KB |
n=2000 |
88 |
Correct |
3 ms |
588 KB |
n=2000 |
89 |
Correct |
7 ms |
588 KB |
n=2000 |
90 |
Correct |
3 ms |
588 KB |
n=2000 |
91 |
Correct |
4 ms |
588 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
n=5 |
2 |
Correct |
1 ms |
332 KB |
n=100 |
3 |
Correct |
1 ms |
332 KB |
n=100 |
4 |
Correct |
1 ms |
332 KB |
n=100 |
5 |
Correct |
0 ms |
332 KB |
n=100 |
6 |
Correct |
1 ms |
332 KB |
n=100 |
7 |
Correct |
1 ms |
332 KB |
n=100 |
8 |
Correct |
0 ms |
332 KB |
n=100 |
9 |
Correct |
1 ms |
332 KB |
n=100 |
10 |
Correct |
1 ms |
332 KB |
n=100 |
11 |
Correct |
1 ms |
332 KB |
n=100 |
12 |
Correct |
1 ms |
332 KB |
n=100 |
13 |
Correct |
0 ms |
332 KB |
n=100 |
14 |
Correct |
1 ms |
332 KB |
n=100 |
15 |
Correct |
1 ms |
332 KB |
n=100 |
16 |
Correct |
1 ms |
332 KB |
n=100 |
17 |
Correct |
1 ms |
332 KB |
n=100 |
18 |
Correct |
1 ms |
332 KB |
n=100 |
19 |
Correct |
1 ms |
332 KB |
n=100 |
20 |
Correct |
0 ms |
332 KB |
n=100 |
21 |
Correct |
1 ms |
332 KB |
n=100 |
22 |
Correct |
0 ms |
332 KB |
n=100 |
23 |
Correct |
1 ms |
332 KB |
n=100 |
24 |
Correct |
1 ms |
332 KB |
n=100 |
25 |
Correct |
1 ms |
332 KB |
n=100 |
26 |
Correct |
0 ms |
332 KB |
n=12 |
27 |
Correct |
1 ms |
332 KB |
n=100 |
28 |
Correct |
1 ms |
460 KB |
n=500 |
29 |
Correct |
1 ms |
460 KB |
n=500 |
30 |
Correct |
1 ms |
460 KB |
n=500 |
31 |
Correct |
1 ms |
460 KB |
n=500 |
32 |
Correct |
1 ms |
460 KB |
n=500 |
33 |
Correct |
1 ms |
460 KB |
n=500 |
34 |
Correct |
1 ms |
460 KB |
n=500 |
35 |
Correct |
1 ms |
460 KB |
n=500 |
36 |
Correct |
2 ms |
460 KB |
n=500 |
37 |
Correct |
1 ms |
460 KB |
n=500 |
38 |
Correct |
1 ms |
460 KB |
n=500 |
39 |
Correct |
1 ms |
460 KB |
n=500 |
40 |
Correct |
1 ms |
460 KB |
n=500 |
41 |
Correct |
1 ms |
460 KB |
n=500 |
42 |
Correct |
1 ms |
460 KB |
n=500 |
43 |
Correct |
1 ms |
460 KB |
n=500 |
44 |
Correct |
1 ms |
460 KB |
n=500 |
45 |
Correct |
1 ms |
460 KB |
n=500 |
46 |
Correct |
1 ms |
460 KB |
n=500 |
47 |
Correct |
1 ms |
460 KB |
n=500 |
48 |
Correct |
1 ms |
460 KB |
n=500 |
49 |
Correct |
2 ms |
396 KB |
n=500 |
50 |
Correct |
1 ms |
460 KB |
n=500 |
51 |
Correct |
1 ms |
460 KB |
n=500 |
52 |
Correct |
1 ms |
460 KB |
n=500 |
53 |
Correct |
1 ms |
460 KB |
n=500 |
54 |
Correct |
1 ms |
460 KB |
n=500 |
55 |
Correct |
1 ms |
460 KB |
n=278 |
56 |
Correct |
1 ms |
460 KB |
n=500 |
57 |
Correct |
2 ms |
460 KB |
n=500 |
58 |
Correct |
2 ms |
460 KB |
n=500 |
59 |
Correct |
2 ms |
588 KB |
n=2000 |
60 |
Correct |
2 ms |
696 KB |
n=2000 |
61 |
Correct |
2 ms |
588 KB |
n=2000 |
62 |
Correct |
2 ms |
588 KB |
n=2000 |
63 |
Correct |
3 ms |
588 KB |
n=2000 |
64 |
Correct |
2 ms |
588 KB |
n=2000 |
65 |
Correct |
2 ms |
588 KB |
n=2000 |
66 |
Correct |
3 ms |
588 KB |
n=2000 |
67 |
Correct |
4 ms |
588 KB |
n=2000 |
68 |
Correct |
2 ms |
588 KB |
n=2000 |
69 |
Correct |
7 ms |
616 KB |
n=2000 |
70 |
Correct |
5 ms |
604 KB |
n=2000 |
71 |
Correct |
4 ms |
588 KB |
n=2000 |
72 |
Correct |
3 ms |
588 KB |
n=2000 |
73 |
Correct |
3 ms |
588 KB |
n=2000 |
74 |
Correct |
2 ms |
588 KB |
n=1844 |
75 |
Correct |
3 ms |
588 KB |
n=2000 |
76 |
Correct |
5 ms |
588 KB |
n=2000 |
77 |
Correct |
4 ms |
568 KB |
n=2000 |
78 |
Correct |
3 ms |
588 KB |
n=2000 |
79 |
Correct |
2 ms |
588 KB |
n=2000 |
80 |
Correct |
2 ms |
588 KB |
n=2000 |
81 |
Correct |
3 ms |
588 KB |
n=2000 |
82 |
Correct |
2 ms |
588 KB |
n=2000 |
83 |
Correct |
2 ms |
588 KB |
n=2000 |
84 |
Correct |
4 ms |
588 KB |
n=2000 |
85 |
Correct |
3 ms |
588 KB |
n=2000 |
86 |
Correct |
3 ms |
588 KB |
n=2000 |
87 |
Correct |
3 ms |
588 KB |
n=2000 |
88 |
Correct |
3 ms |
588 KB |
n=2000 |
89 |
Correct |
7 ms |
588 KB |
n=2000 |
90 |
Correct |
3 ms |
588 KB |
n=2000 |
91 |
Correct |
4 ms |
588 KB |
n=2000 |
92 |
Runtime error |
1 ms |
524 KB |
Execution killed with signal 11 |
93 |
Halted |
0 ms |
0 KB |
- |