//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define file ""
#define all(x) x.begin(), x.end()
#define fr first
#define sc second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 3e5;
const int inf = 1e9 + 5;
map <int, pii> mp;
int a[N], tin[N], tout[N], t[4 * N], timer = 1;
int up[N][25];
vector <int> g[N];
void dfs (int x, int pr) {
tin[x] = timer++;
up[x][0] = pr;
for (int i = 1; i <= 20; i++) {
up[x][i] = up[up[x][i - 1]][i - 1];
}
for (int to : g[x]) {
if (to != pr) dfs(to, x);
}
tout[x] = timer;
}
bool upper (int x, int y) {
if (tin[x] <= tin[y] && tout[x] >= tout[y]) return true;
return false;
}
int lca (int x, int y) {
if (x == 0) return y;
if (y == 0) return x;
if (upper(x, y)) return x;
if (upper(y, x)) return y;
for (int i = 20; i >= 0; i--) {
if (!upper(up[x][i], y)) x = up[x][i];
}
return up[x][0];
}
void build (int u, int l, int r) {
if (l == r) {
t[u] = a[l];
return;
}
int m = l + r >> 1;
build(u + u, l, m);
build(u + u + 1, m + 1, r);
t[u] = lca (t[u + u], t[u + u + 1]);
}
void upd (int u, int ul, int ur, int pos, int val) {
if (ul == ur && ul == pos) {
t[u] = val;
return;
}
int m = ul + ur >> 1;
if (pos <= m) upd(u + u, ul, m, pos, val);
else upd(u + u + 1, m + 1, ur, pos, val);
t[u] = lca (t[u + u], t[u + u + 1]);
}
int get (int u, int ul, int ur, int l, int r) {
if (ul > r || ur < l) return 0;
if (ul >= l && ur <= r) return t[u];
int m = ul + ur >> 1;
int x = get(u + u, ul, m, l, r);
int y = get(u + u + 1, m + 1, ur, l, r);
return lca(x, y);
}
pii get (int u, int ul, int ur, int l, int r, int val) {
//cerr << u << " " << ul << " " << ur << " " << l << " " << r << " " << val << endl;
if (ul == ur && !upper(val, t[u])) return {-1, -1};
if (ul > r || ur < l) {
//cerr << u << " " << ul << " " << ur << " " << val << " " << t[u] << " ok1\n";
return {-1, -1};
}
if (ul >= l && ur <= r && upper(val, t[u])) {
//cerr << u << " " << ul << " " << ur << " " << val << " " << t[u] << " ok2\n";
return {ul, ur};
}
//if (ul >= l && ur <= r && !upper(val, t[u])) {
// cerr << u << " " << ul << " " << ur << " " << val << " " << t[u] << " ok3\n";
// return {-1, -1};
//}
int m = ul + ur >> 1;
pii x = get(u + u, ul, m, l, r, val);
pii y = get(u + u + 1, m + 1, ur, l, r, val);
//cerr << u << " " << ul << " " << ur << ": " << x.fr << " " << x.sc << " - " << y.fr << " " << y.sc << endl;
if (x.fr == -1 && y.fr != -1) return y;
else if (y.fr == -1 && x.fr != -1) return x;
return {x.fr, y.sc};
}
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
int n, m, q;
cin >> n >> m >> q;
int u, v, tp;
for (int i = 1; i < n; i++) {
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 1);
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
while (q--) {
cin >> tp;
if (tp == 1) {
int pos, val;
cin >> pos >> val;
a[pos] = val;
}
else {
int l, r, val;
cin >> l >> r >> val;
int ok = 0;
for (int i = l; i <= r; i++) {
int LCA = a[i];
for (int j = i; j <= r; j++) {
LCA = lca(LCA, a[j]);
if (LCA == val) {
cout << i << " " << j << "\n";
ok = 1;
break;
}
}
if (ok == 1) break;
}
if (ok == 0) cout << -1 << " " << -1 << "\n";
}
}
}
Compilation message
treearray.cpp: In function 'void build(int, int, int)':
treearray.cpp:60:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int m = l + r >> 1;
| ~~^~~
treearray.cpp: In function 'void upd(int, int, int, int, int)':
treearray.cpp:71:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int m = ul + ur >> 1;
| ~~~^~~~
treearray.cpp: In function 'int get(int, int, int, int, int)':
treearray.cpp:80:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int m = ul + ur >> 1;
| ~~~^~~~
treearray.cpp: In function 'pii get(int, int, int, int, int, int)':
treearray.cpp:101:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
101 | int m = ul + ur >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
n=5 |
2 |
Correct |
4 ms |
7404 KB |
n=100 |
3 |
Correct |
4 ms |
7380 KB |
n=100 |
4 |
Correct |
5 ms |
7380 KB |
n=100 |
5 |
Correct |
4 ms |
7452 KB |
n=100 |
6 |
Correct |
5 ms |
7380 KB |
n=100 |
7 |
Correct |
5 ms |
7380 KB |
n=100 |
8 |
Correct |
4 ms |
7312 KB |
n=100 |
9 |
Correct |
4 ms |
7380 KB |
n=100 |
10 |
Correct |
4 ms |
7380 KB |
n=100 |
11 |
Correct |
4 ms |
7380 KB |
n=100 |
12 |
Correct |
3 ms |
7380 KB |
n=100 |
13 |
Correct |
4 ms |
7380 KB |
n=100 |
14 |
Correct |
5 ms |
7380 KB |
n=100 |
15 |
Correct |
3 ms |
7380 KB |
n=100 |
16 |
Correct |
4 ms |
7380 KB |
n=100 |
17 |
Correct |
5 ms |
7380 KB |
n=100 |
18 |
Correct |
4 ms |
7380 KB |
n=100 |
19 |
Correct |
4 ms |
7380 KB |
n=100 |
20 |
Correct |
4 ms |
7380 KB |
n=100 |
21 |
Correct |
4 ms |
7380 KB |
n=100 |
22 |
Correct |
4 ms |
7380 KB |
n=100 |
23 |
Correct |
4 ms |
7380 KB |
n=100 |
24 |
Correct |
4 ms |
7380 KB |
n=100 |
25 |
Correct |
4 ms |
7380 KB |
n=100 |
26 |
Correct |
3 ms |
7380 KB |
n=12 |
27 |
Correct |
5 ms |
7380 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
n=5 |
2 |
Correct |
4 ms |
7404 KB |
n=100 |
3 |
Correct |
4 ms |
7380 KB |
n=100 |
4 |
Correct |
5 ms |
7380 KB |
n=100 |
5 |
Correct |
4 ms |
7452 KB |
n=100 |
6 |
Correct |
5 ms |
7380 KB |
n=100 |
7 |
Correct |
5 ms |
7380 KB |
n=100 |
8 |
Correct |
4 ms |
7312 KB |
n=100 |
9 |
Correct |
4 ms |
7380 KB |
n=100 |
10 |
Correct |
4 ms |
7380 KB |
n=100 |
11 |
Correct |
4 ms |
7380 KB |
n=100 |
12 |
Correct |
3 ms |
7380 KB |
n=100 |
13 |
Correct |
4 ms |
7380 KB |
n=100 |
14 |
Correct |
5 ms |
7380 KB |
n=100 |
15 |
Correct |
3 ms |
7380 KB |
n=100 |
16 |
Correct |
4 ms |
7380 KB |
n=100 |
17 |
Correct |
5 ms |
7380 KB |
n=100 |
18 |
Correct |
4 ms |
7380 KB |
n=100 |
19 |
Correct |
4 ms |
7380 KB |
n=100 |
20 |
Correct |
4 ms |
7380 KB |
n=100 |
21 |
Correct |
4 ms |
7380 KB |
n=100 |
22 |
Correct |
4 ms |
7380 KB |
n=100 |
23 |
Correct |
4 ms |
7380 KB |
n=100 |
24 |
Correct |
4 ms |
7380 KB |
n=100 |
25 |
Correct |
4 ms |
7380 KB |
n=100 |
26 |
Correct |
3 ms |
7380 KB |
n=12 |
27 |
Correct |
5 ms |
7380 KB |
n=100 |
28 |
Correct |
5 ms |
7380 KB |
n=500 |
29 |
Correct |
16 ms |
7420 KB |
n=500 |
30 |
Correct |
14 ms |
7464 KB |
n=500 |
31 |
Correct |
17 ms |
7380 KB |
n=500 |
32 |
Correct |
5 ms |
7380 KB |
n=500 |
33 |
Correct |
14 ms |
7380 KB |
n=500 |
34 |
Correct |
4 ms |
7348 KB |
n=500 |
35 |
Correct |
15 ms |
7480 KB |
n=500 |
36 |
Correct |
169 ms |
7436 KB |
n=500 |
37 |
Correct |
144 ms |
7380 KB |
n=500 |
38 |
Correct |
149 ms |
7444 KB |
n=500 |
39 |
Correct |
36 ms |
7440 KB |
n=500 |
40 |
Correct |
27 ms |
7380 KB |
n=500 |
41 |
Correct |
26 ms |
7380 KB |
n=500 |
42 |
Correct |
74 ms |
7448 KB |
n=500 |
43 |
Correct |
76 ms |
7380 KB |
n=500 |
44 |
Correct |
74 ms |
7440 KB |
n=500 |
45 |
Correct |
4 ms |
7380 KB |
n=500 |
46 |
Correct |
16 ms |
7476 KB |
n=500 |
47 |
Correct |
14 ms |
7380 KB |
n=500 |
48 |
Correct |
5 ms |
7444 KB |
n=500 |
49 |
Correct |
14 ms |
7476 KB |
n=500 |
50 |
Correct |
22 ms |
7380 KB |
n=500 |
51 |
Correct |
27 ms |
7380 KB |
n=500 |
52 |
Correct |
24 ms |
7480 KB |
n=500 |
53 |
Correct |
26 ms |
7380 KB |
n=500 |
54 |
Correct |
12 ms |
7380 KB |
n=500 |
55 |
Correct |
6 ms |
7380 KB |
n=278 |
56 |
Correct |
90 ms |
7424 KB |
n=500 |
57 |
Correct |
78 ms |
7460 KB |
n=500 |
58 |
Correct |
75 ms |
7456 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
n=5 |
2 |
Correct |
4 ms |
7404 KB |
n=100 |
3 |
Correct |
4 ms |
7380 KB |
n=100 |
4 |
Correct |
5 ms |
7380 KB |
n=100 |
5 |
Correct |
4 ms |
7452 KB |
n=100 |
6 |
Correct |
5 ms |
7380 KB |
n=100 |
7 |
Correct |
5 ms |
7380 KB |
n=100 |
8 |
Correct |
4 ms |
7312 KB |
n=100 |
9 |
Correct |
4 ms |
7380 KB |
n=100 |
10 |
Correct |
4 ms |
7380 KB |
n=100 |
11 |
Correct |
4 ms |
7380 KB |
n=100 |
12 |
Correct |
3 ms |
7380 KB |
n=100 |
13 |
Correct |
4 ms |
7380 KB |
n=100 |
14 |
Correct |
5 ms |
7380 KB |
n=100 |
15 |
Correct |
3 ms |
7380 KB |
n=100 |
16 |
Correct |
4 ms |
7380 KB |
n=100 |
17 |
Correct |
5 ms |
7380 KB |
n=100 |
18 |
Correct |
4 ms |
7380 KB |
n=100 |
19 |
Correct |
4 ms |
7380 KB |
n=100 |
20 |
Correct |
4 ms |
7380 KB |
n=100 |
21 |
Correct |
4 ms |
7380 KB |
n=100 |
22 |
Correct |
4 ms |
7380 KB |
n=100 |
23 |
Correct |
4 ms |
7380 KB |
n=100 |
24 |
Correct |
4 ms |
7380 KB |
n=100 |
25 |
Correct |
4 ms |
7380 KB |
n=100 |
26 |
Correct |
3 ms |
7380 KB |
n=12 |
27 |
Correct |
5 ms |
7380 KB |
n=100 |
28 |
Correct |
5 ms |
7380 KB |
n=500 |
29 |
Correct |
16 ms |
7420 KB |
n=500 |
30 |
Correct |
14 ms |
7464 KB |
n=500 |
31 |
Correct |
17 ms |
7380 KB |
n=500 |
32 |
Correct |
5 ms |
7380 KB |
n=500 |
33 |
Correct |
14 ms |
7380 KB |
n=500 |
34 |
Correct |
4 ms |
7348 KB |
n=500 |
35 |
Correct |
15 ms |
7480 KB |
n=500 |
36 |
Correct |
169 ms |
7436 KB |
n=500 |
37 |
Correct |
144 ms |
7380 KB |
n=500 |
38 |
Correct |
149 ms |
7444 KB |
n=500 |
39 |
Correct |
36 ms |
7440 KB |
n=500 |
40 |
Correct |
27 ms |
7380 KB |
n=500 |
41 |
Correct |
26 ms |
7380 KB |
n=500 |
42 |
Correct |
74 ms |
7448 KB |
n=500 |
43 |
Correct |
76 ms |
7380 KB |
n=500 |
44 |
Correct |
74 ms |
7440 KB |
n=500 |
45 |
Correct |
4 ms |
7380 KB |
n=500 |
46 |
Correct |
16 ms |
7476 KB |
n=500 |
47 |
Correct |
14 ms |
7380 KB |
n=500 |
48 |
Correct |
5 ms |
7444 KB |
n=500 |
49 |
Correct |
14 ms |
7476 KB |
n=500 |
50 |
Correct |
22 ms |
7380 KB |
n=500 |
51 |
Correct |
27 ms |
7380 KB |
n=500 |
52 |
Correct |
24 ms |
7480 KB |
n=500 |
53 |
Correct |
26 ms |
7380 KB |
n=500 |
54 |
Correct |
12 ms |
7380 KB |
n=500 |
55 |
Correct |
6 ms |
7380 KB |
n=278 |
56 |
Correct |
90 ms |
7424 KB |
n=500 |
57 |
Correct |
78 ms |
7460 KB |
n=500 |
58 |
Correct |
75 ms |
7456 KB |
n=500 |
59 |
Correct |
15 ms |
7700 KB |
n=2000 |
60 |
Correct |
693 ms |
7764 KB |
n=2000 |
61 |
Correct |
687 ms |
7784 KB |
n=2000 |
62 |
Correct |
492 ms |
7748 KB |
n=2000 |
63 |
Correct |
19 ms |
7744 KB |
n=2000 |
64 |
Correct |
627 ms |
7876 KB |
n=2000 |
65 |
Correct |
7 ms |
7728 KB |
n=2000 |
66 |
Correct |
781 ms |
7904 KB |
n=2000 |
67 |
Correct |
42 ms |
7636 KB |
n=2000 |
68 |
Correct |
662 ms |
7764 KB |
n=2000 |
69 |
Execution timed out |
4049 ms |
7716 KB |
Time limit exceeded |
70 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
n=5 |
2 |
Correct |
4 ms |
7404 KB |
n=100 |
3 |
Correct |
4 ms |
7380 KB |
n=100 |
4 |
Correct |
5 ms |
7380 KB |
n=100 |
5 |
Correct |
4 ms |
7452 KB |
n=100 |
6 |
Correct |
5 ms |
7380 KB |
n=100 |
7 |
Correct |
5 ms |
7380 KB |
n=100 |
8 |
Correct |
4 ms |
7312 KB |
n=100 |
9 |
Correct |
4 ms |
7380 KB |
n=100 |
10 |
Correct |
4 ms |
7380 KB |
n=100 |
11 |
Correct |
4 ms |
7380 KB |
n=100 |
12 |
Correct |
3 ms |
7380 KB |
n=100 |
13 |
Correct |
4 ms |
7380 KB |
n=100 |
14 |
Correct |
5 ms |
7380 KB |
n=100 |
15 |
Correct |
3 ms |
7380 KB |
n=100 |
16 |
Correct |
4 ms |
7380 KB |
n=100 |
17 |
Correct |
5 ms |
7380 KB |
n=100 |
18 |
Correct |
4 ms |
7380 KB |
n=100 |
19 |
Correct |
4 ms |
7380 KB |
n=100 |
20 |
Correct |
4 ms |
7380 KB |
n=100 |
21 |
Correct |
4 ms |
7380 KB |
n=100 |
22 |
Correct |
4 ms |
7380 KB |
n=100 |
23 |
Correct |
4 ms |
7380 KB |
n=100 |
24 |
Correct |
4 ms |
7380 KB |
n=100 |
25 |
Correct |
4 ms |
7380 KB |
n=100 |
26 |
Correct |
3 ms |
7380 KB |
n=12 |
27 |
Correct |
5 ms |
7380 KB |
n=100 |
28 |
Correct |
5 ms |
7380 KB |
n=500 |
29 |
Correct |
16 ms |
7420 KB |
n=500 |
30 |
Correct |
14 ms |
7464 KB |
n=500 |
31 |
Correct |
17 ms |
7380 KB |
n=500 |
32 |
Correct |
5 ms |
7380 KB |
n=500 |
33 |
Correct |
14 ms |
7380 KB |
n=500 |
34 |
Correct |
4 ms |
7348 KB |
n=500 |
35 |
Correct |
15 ms |
7480 KB |
n=500 |
36 |
Correct |
169 ms |
7436 KB |
n=500 |
37 |
Correct |
144 ms |
7380 KB |
n=500 |
38 |
Correct |
149 ms |
7444 KB |
n=500 |
39 |
Correct |
36 ms |
7440 KB |
n=500 |
40 |
Correct |
27 ms |
7380 KB |
n=500 |
41 |
Correct |
26 ms |
7380 KB |
n=500 |
42 |
Correct |
74 ms |
7448 KB |
n=500 |
43 |
Correct |
76 ms |
7380 KB |
n=500 |
44 |
Correct |
74 ms |
7440 KB |
n=500 |
45 |
Correct |
4 ms |
7380 KB |
n=500 |
46 |
Correct |
16 ms |
7476 KB |
n=500 |
47 |
Correct |
14 ms |
7380 KB |
n=500 |
48 |
Correct |
5 ms |
7444 KB |
n=500 |
49 |
Correct |
14 ms |
7476 KB |
n=500 |
50 |
Correct |
22 ms |
7380 KB |
n=500 |
51 |
Correct |
27 ms |
7380 KB |
n=500 |
52 |
Correct |
24 ms |
7480 KB |
n=500 |
53 |
Correct |
26 ms |
7380 KB |
n=500 |
54 |
Correct |
12 ms |
7380 KB |
n=500 |
55 |
Correct |
6 ms |
7380 KB |
n=278 |
56 |
Correct |
90 ms |
7424 KB |
n=500 |
57 |
Correct |
78 ms |
7460 KB |
n=500 |
58 |
Correct |
75 ms |
7456 KB |
n=500 |
59 |
Correct |
15 ms |
7700 KB |
n=2000 |
60 |
Correct |
693 ms |
7764 KB |
n=2000 |
61 |
Correct |
687 ms |
7784 KB |
n=2000 |
62 |
Correct |
492 ms |
7748 KB |
n=2000 |
63 |
Correct |
19 ms |
7744 KB |
n=2000 |
64 |
Correct |
627 ms |
7876 KB |
n=2000 |
65 |
Correct |
7 ms |
7728 KB |
n=2000 |
66 |
Correct |
781 ms |
7904 KB |
n=2000 |
67 |
Correct |
42 ms |
7636 KB |
n=2000 |
68 |
Correct |
662 ms |
7764 KB |
n=2000 |
69 |
Execution timed out |
4049 ms |
7716 KB |
Time limit exceeded |
70 |
Halted |
0 ms |
0 KB |
- |