//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define file(s) if(fopen(s".in","r")) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define len(x) (int)x.size()
#define tm (tl + tr >> 1)
#define ls v << 1, tl, tm
#define rs v << 1 | 1, tm + 1, tr
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define elif else if
#define F first
#define S second
#define int long long
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ld;
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const int P = 911;
const ll INF = 1e18;
int rnd() {
int x = rand() << 15;
return x ^ rand();
}
int tin[N], tout[N], up[N][19], t[N << 2], a[N], temp, m;
vector <int> g[N];
void dfs(int v, int pr) {
tin[v] = ++temp;
up[v][0] = pr;
for (int i = 1; i <= 18; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (int u: g[v]) if (u != pr) {
dfs(u, v);
}
tout[v] = temp;
}
bool upper(int v, int u) {
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
int lca(int v, int u) {
if (v && (!u || upper(v, u))) return v;
if (u && (!v || upper(u, v))) return u;
for (int i = 18; i >= 0; i--) {
if (!up[v][i]) continue;
if (!upper(up[v][i], u)) v = up[v][i];
}
return up[v][0];
}
void build(int v = 1, int tl = 1, int tr = m) {
if (tl == tr) {
t[v] = a[tl];
return;
}
build(ls);
build(rs);
t[v] = lca(t[v << 1], t[v << 1 | 1]);
}
void upd(int pos, int val, int v = 1, int tl = 1, int tr = m) {
if (pos == tl && tr == pos) {
a[tl] = val;
t[v] = val;
return;
}
if (pos <= tm) upd(pos, val, ls);
else upd(pos, val, rs);
t[v] = lca(t[v << 1], t[v << 1 | 1]);
}
int get(int l, int r, int v = 1, int tl = 1, int tr = m) {
if (l <= tl && tr <= r) return t[v];
elif (r < tl || tr < l) return 0;
return lca(get(l, r, ls), get(l, r, rs));
}
void GazizMadi() {
int n, q;
cin >> n >> m >> q;
for (int i = 1, v, u; i < n; i++) {
cin >> v >> u;
g[v].pb(u);
g[u].pb(v);
}
dfs(1, 0);
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
build();
while (q--) {
int ty;
cin >> ty;
if (ty == 1) {
int pos, val;
cin >> pos >> val;
upd(pos, val);
} else {
int l, r, v, ans1 = -1, ans2 = -1;
cin >> l >> r >> v;
for (int i = l; i <= r; i++) {
if (a[i] == v) {
ans1 = i;
ans2 = i;
break;
}
if (upper(a[i], v) || lca(a[i], v) != v) continue;
int now = get(i, r);
if (now == v) {
ans1 = i;
ans2 = r;
break;
}
if (upper(v, now)) break;
for (int j = i; j <= r; j++) {
// cout << "Asd " << i << ' ' << j << ' ' << get(i, j) << '\n';
if (get(i, j) == v) {
ans1 = i;
ans2 = j;
break;
}
}
}
cout << ans1 << ' ' << ans2 << '\n';
}
}
}
const bool Cases = 0;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0));
int TT = 1;
if (Cases) cin >> TT;
for (int i = 1; i <= TT; i++) {
//cout << "Case " << i << ": ";
GazizMadi();
}
}
Compilation message
treearray.cpp: In function 'void build(long long int, long long int, long long int)':
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
treearray.cpp:14:24: note: in expansion of macro 'tm'
14 | #define ls v << 1, tl, tm
| ^~
treearray.cpp:78:8: note: in expansion of macro 'ls'
78 | build(ls);
| ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
treearray.cpp:15:24: note: in expansion of macro 'tm'
15 | #define rs v << 1 | 1, tm + 1, tr
| ^~
treearray.cpp:79:8: note: in expansion of macro 'rs'
79 | build(rs);
| ^~
treearray.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
treearray.cpp:89:13: note: in expansion of macro 'tm'
89 | if (pos <= tm) upd(pos, val, ls);
| ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
treearray.cpp:14:24: note: in expansion of macro 'tm'
14 | #define ls v << 1, tl, tm
| ^~
treearray.cpp:89:31: note: in expansion of macro 'ls'
89 | if (pos <= tm) upd(pos, val, ls);
| ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
treearray.cpp:15:24: note: in expansion of macro 'tm'
15 | #define rs v << 1 | 1, tm + 1, tr
| ^~
treearray.cpp:90:21: note: in expansion of macro 'rs'
90 | else upd(pos, val, rs);
| ^~
treearray.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
treearray.cpp:14:24: note: in expansion of macro 'tm'
14 | #define ls v << 1, tl, tm
| ^~
treearray.cpp:97:23: note: in expansion of macro 'ls'
97 | return lca(get(l, r, ls), get(l, r, rs));
| ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | #define tm (tl + tr >> 1)
| ~~~^~~~
treearray.cpp:15:24: note: in expansion of macro 'tm'
15 | #define rs v << 1 | 1, tm + 1, tr
| ^~
treearray.cpp:97:38: note: in expansion of macro 'rs'
97 | return lca(get(l, r, ls), get(l, r, rs));
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
n=5 |
2 |
Correct |
3 ms |
12892 KB |
n=100 |
3 |
Correct |
3 ms |
12892 KB |
n=100 |
4 |
Correct |
4 ms |
12892 KB |
n=100 |
5 |
Correct |
4 ms |
12892 KB |
n=100 |
6 |
Correct |
26 ms |
12892 KB |
n=100 |
7 |
Correct |
26 ms |
12892 KB |
n=100 |
8 |
Correct |
5 ms |
12888 KB |
n=100 |
9 |
Correct |
4 ms |
12892 KB |
n=100 |
10 |
Correct |
15 ms |
12888 KB |
n=100 |
11 |
Correct |
19 ms |
12892 KB |
n=100 |
12 |
Correct |
3 ms |
12888 KB |
n=100 |
13 |
Correct |
3 ms |
12892 KB |
n=100 |
14 |
Correct |
3 ms |
12892 KB |
n=100 |
15 |
Correct |
2 ms |
12892 KB |
n=100 |
16 |
Correct |
2 ms |
12892 KB |
n=100 |
17 |
Correct |
6 ms |
12892 KB |
n=100 |
18 |
Correct |
4 ms |
12892 KB |
n=100 |
19 |
Correct |
2 ms |
12892 KB |
n=100 |
20 |
Correct |
4 ms |
12892 KB |
n=100 |
21 |
Correct |
4 ms |
13144 KB |
n=100 |
22 |
Correct |
2 ms |
12888 KB |
n=100 |
23 |
Correct |
2 ms |
12888 KB |
n=100 |
24 |
Correct |
2 ms |
12892 KB |
n=100 |
25 |
Correct |
2 ms |
12888 KB |
n=100 |
26 |
Correct |
2 ms |
12888 KB |
n=12 |
27 |
Correct |
2 ms |
12892 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
n=5 |
2 |
Correct |
3 ms |
12892 KB |
n=100 |
3 |
Correct |
3 ms |
12892 KB |
n=100 |
4 |
Correct |
4 ms |
12892 KB |
n=100 |
5 |
Correct |
4 ms |
12892 KB |
n=100 |
6 |
Correct |
26 ms |
12892 KB |
n=100 |
7 |
Correct |
26 ms |
12892 KB |
n=100 |
8 |
Correct |
5 ms |
12888 KB |
n=100 |
9 |
Correct |
4 ms |
12892 KB |
n=100 |
10 |
Correct |
15 ms |
12888 KB |
n=100 |
11 |
Correct |
19 ms |
12892 KB |
n=100 |
12 |
Correct |
3 ms |
12888 KB |
n=100 |
13 |
Correct |
3 ms |
12892 KB |
n=100 |
14 |
Correct |
3 ms |
12892 KB |
n=100 |
15 |
Correct |
2 ms |
12892 KB |
n=100 |
16 |
Correct |
2 ms |
12892 KB |
n=100 |
17 |
Correct |
6 ms |
12892 KB |
n=100 |
18 |
Correct |
4 ms |
12892 KB |
n=100 |
19 |
Correct |
2 ms |
12892 KB |
n=100 |
20 |
Correct |
4 ms |
12892 KB |
n=100 |
21 |
Correct |
4 ms |
13144 KB |
n=100 |
22 |
Correct |
2 ms |
12888 KB |
n=100 |
23 |
Correct |
2 ms |
12888 KB |
n=100 |
24 |
Correct |
2 ms |
12892 KB |
n=100 |
25 |
Correct |
2 ms |
12888 KB |
n=100 |
26 |
Correct |
2 ms |
12888 KB |
n=12 |
27 |
Correct |
2 ms |
12892 KB |
n=100 |
28 |
Correct |
2 ms |
12892 KB |
n=500 |
29 |
Correct |
323 ms |
12888 KB |
n=500 |
30 |
Correct |
324 ms |
12948 KB |
n=500 |
31 |
Correct |
315 ms |
12892 KB |
n=500 |
32 |
Correct |
6 ms |
12892 KB |
n=500 |
33 |
Correct |
350 ms |
12956 KB |
n=500 |
34 |
Correct |
2 ms |
12892 KB |
n=500 |
35 |
Correct |
284 ms |
12964 KB |
n=500 |
36 |
Correct |
3517 ms |
13140 KB |
n=500 |
37 |
Correct |
3491 ms |
13216 KB |
n=500 |
38 |
Correct |
3503 ms |
13216 KB |
n=500 |
39 |
Correct |
312 ms |
12968 KB |
n=500 |
40 |
Correct |
286 ms |
12892 KB |
n=500 |
41 |
Correct |
284 ms |
12964 KB |
n=500 |
42 |
Correct |
1721 ms |
12968 KB |
n=500 |
43 |
Correct |
1671 ms |
13048 KB |
n=500 |
44 |
Correct |
1570 ms |
12972 KB |
n=500 |
45 |
Correct |
4 ms |
12892 KB |
n=500 |
46 |
Correct |
338 ms |
12892 KB |
n=500 |
47 |
Correct |
260 ms |
12892 KB |
n=500 |
48 |
Correct |
2 ms |
12892 KB |
n=500 |
49 |
Correct |
222 ms |
13144 KB |
n=500 |
50 |
Correct |
4 ms |
12892 KB |
n=500 |
51 |
Correct |
232 ms |
12888 KB |
n=500 |
52 |
Correct |
415 ms |
12976 KB |
n=500 |
53 |
Correct |
136 ms |
12888 KB |
n=500 |
54 |
Correct |
3 ms |
12944 KB |
n=500 |
55 |
Correct |
75 ms |
12948 KB |
n=278 |
56 |
Correct |
3 ms |
12892 KB |
n=500 |
57 |
Correct |
3 ms |
12968 KB |
n=500 |
58 |
Correct |
12 ms |
12896 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
n=5 |
2 |
Correct |
3 ms |
12892 KB |
n=100 |
3 |
Correct |
3 ms |
12892 KB |
n=100 |
4 |
Correct |
4 ms |
12892 KB |
n=100 |
5 |
Correct |
4 ms |
12892 KB |
n=100 |
6 |
Correct |
26 ms |
12892 KB |
n=100 |
7 |
Correct |
26 ms |
12892 KB |
n=100 |
8 |
Correct |
5 ms |
12888 KB |
n=100 |
9 |
Correct |
4 ms |
12892 KB |
n=100 |
10 |
Correct |
15 ms |
12888 KB |
n=100 |
11 |
Correct |
19 ms |
12892 KB |
n=100 |
12 |
Correct |
3 ms |
12888 KB |
n=100 |
13 |
Correct |
3 ms |
12892 KB |
n=100 |
14 |
Correct |
3 ms |
12892 KB |
n=100 |
15 |
Correct |
2 ms |
12892 KB |
n=100 |
16 |
Correct |
2 ms |
12892 KB |
n=100 |
17 |
Correct |
6 ms |
12892 KB |
n=100 |
18 |
Correct |
4 ms |
12892 KB |
n=100 |
19 |
Correct |
2 ms |
12892 KB |
n=100 |
20 |
Correct |
4 ms |
12892 KB |
n=100 |
21 |
Correct |
4 ms |
13144 KB |
n=100 |
22 |
Correct |
2 ms |
12888 KB |
n=100 |
23 |
Correct |
2 ms |
12888 KB |
n=100 |
24 |
Correct |
2 ms |
12892 KB |
n=100 |
25 |
Correct |
2 ms |
12888 KB |
n=100 |
26 |
Correct |
2 ms |
12888 KB |
n=12 |
27 |
Correct |
2 ms |
12892 KB |
n=100 |
28 |
Correct |
2 ms |
12892 KB |
n=500 |
29 |
Correct |
323 ms |
12888 KB |
n=500 |
30 |
Correct |
324 ms |
12948 KB |
n=500 |
31 |
Correct |
315 ms |
12892 KB |
n=500 |
32 |
Correct |
6 ms |
12892 KB |
n=500 |
33 |
Correct |
350 ms |
12956 KB |
n=500 |
34 |
Correct |
2 ms |
12892 KB |
n=500 |
35 |
Correct |
284 ms |
12964 KB |
n=500 |
36 |
Correct |
3517 ms |
13140 KB |
n=500 |
37 |
Correct |
3491 ms |
13216 KB |
n=500 |
38 |
Correct |
3503 ms |
13216 KB |
n=500 |
39 |
Correct |
312 ms |
12968 KB |
n=500 |
40 |
Correct |
286 ms |
12892 KB |
n=500 |
41 |
Correct |
284 ms |
12964 KB |
n=500 |
42 |
Correct |
1721 ms |
12968 KB |
n=500 |
43 |
Correct |
1671 ms |
13048 KB |
n=500 |
44 |
Correct |
1570 ms |
12972 KB |
n=500 |
45 |
Correct |
4 ms |
12892 KB |
n=500 |
46 |
Correct |
338 ms |
12892 KB |
n=500 |
47 |
Correct |
260 ms |
12892 KB |
n=500 |
48 |
Correct |
2 ms |
12892 KB |
n=500 |
49 |
Correct |
222 ms |
13144 KB |
n=500 |
50 |
Correct |
4 ms |
12892 KB |
n=500 |
51 |
Correct |
232 ms |
12888 KB |
n=500 |
52 |
Correct |
415 ms |
12976 KB |
n=500 |
53 |
Correct |
136 ms |
12888 KB |
n=500 |
54 |
Correct |
3 ms |
12944 KB |
n=500 |
55 |
Correct |
75 ms |
12948 KB |
n=278 |
56 |
Correct |
3 ms |
12892 KB |
n=500 |
57 |
Correct |
3 ms |
12968 KB |
n=500 |
58 |
Correct |
12 ms |
12896 KB |
n=500 |
59 |
Correct |
91 ms |
13068 KB |
n=2000 |
60 |
Execution timed out |
4059 ms |
13148 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
n=5 |
2 |
Correct |
3 ms |
12892 KB |
n=100 |
3 |
Correct |
3 ms |
12892 KB |
n=100 |
4 |
Correct |
4 ms |
12892 KB |
n=100 |
5 |
Correct |
4 ms |
12892 KB |
n=100 |
6 |
Correct |
26 ms |
12892 KB |
n=100 |
7 |
Correct |
26 ms |
12892 KB |
n=100 |
8 |
Correct |
5 ms |
12888 KB |
n=100 |
9 |
Correct |
4 ms |
12892 KB |
n=100 |
10 |
Correct |
15 ms |
12888 KB |
n=100 |
11 |
Correct |
19 ms |
12892 KB |
n=100 |
12 |
Correct |
3 ms |
12888 KB |
n=100 |
13 |
Correct |
3 ms |
12892 KB |
n=100 |
14 |
Correct |
3 ms |
12892 KB |
n=100 |
15 |
Correct |
2 ms |
12892 KB |
n=100 |
16 |
Correct |
2 ms |
12892 KB |
n=100 |
17 |
Correct |
6 ms |
12892 KB |
n=100 |
18 |
Correct |
4 ms |
12892 KB |
n=100 |
19 |
Correct |
2 ms |
12892 KB |
n=100 |
20 |
Correct |
4 ms |
12892 KB |
n=100 |
21 |
Correct |
4 ms |
13144 KB |
n=100 |
22 |
Correct |
2 ms |
12888 KB |
n=100 |
23 |
Correct |
2 ms |
12888 KB |
n=100 |
24 |
Correct |
2 ms |
12892 KB |
n=100 |
25 |
Correct |
2 ms |
12888 KB |
n=100 |
26 |
Correct |
2 ms |
12888 KB |
n=12 |
27 |
Correct |
2 ms |
12892 KB |
n=100 |
28 |
Correct |
2 ms |
12892 KB |
n=500 |
29 |
Correct |
323 ms |
12888 KB |
n=500 |
30 |
Correct |
324 ms |
12948 KB |
n=500 |
31 |
Correct |
315 ms |
12892 KB |
n=500 |
32 |
Correct |
6 ms |
12892 KB |
n=500 |
33 |
Correct |
350 ms |
12956 KB |
n=500 |
34 |
Correct |
2 ms |
12892 KB |
n=500 |
35 |
Correct |
284 ms |
12964 KB |
n=500 |
36 |
Correct |
3517 ms |
13140 KB |
n=500 |
37 |
Correct |
3491 ms |
13216 KB |
n=500 |
38 |
Correct |
3503 ms |
13216 KB |
n=500 |
39 |
Correct |
312 ms |
12968 KB |
n=500 |
40 |
Correct |
286 ms |
12892 KB |
n=500 |
41 |
Correct |
284 ms |
12964 KB |
n=500 |
42 |
Correct |
1721 ms |
12968 KB |
n=500 |
43 |
Correct |
1671 ms |
13048 KB |
n=500 |
44 |
Correct |
1570 ms |
12972 KB |
n=500 |
45 |
Correct |
4 ms |
12892 KB |
n=500 |
46 |
Correct |
338 ms |
12892 KB |
n=500 |
47 |
Correct |
260 ms |
12892 KB |
n=500 |
48 |
Correct |
2 ms |
12892 KB |
n=500 |
49 |
Correct |
222 ms |
13144 KB |
n=500 |
50 |
Correct |
4 ms |
12892 KB |
n=500 |
51 |
Correct |
232 ms |
12888 KB |
n=500 |
52 |
Correct |
415 ms |
12976 KB |
n=500 |
53 |
Correct |
136 ms |
12888 KB |
n=500 |
54 |
Correct |
3 ms |
12944 KB |
n=500 |
55 |
Correct |
75 ms |
12948 KB |
n=278 |
56 |
Correct |
3 ms |
12892 KB |
n=500 |
57 |
Correct |
3 ms |
12968 KB |
n=500 |
58 |
Correct |
12 ms |
12896 KB |
n=500 |
59 |
Correct |
91 ms |
13068 KB |
n=2000 |
60 |
Execution timed out |
4059 ms |
13148 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |