#include <bits/stdc++.h>
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
using namespace std;
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;
const int N = (int)2e5 + 7;
const int M = (int)7e6 + 7;
const ll MOD = (ll)1e9 + 7;
const int inf = (int)1e9 + 7;
const ll INF = (ll)3e18 + 7;
pii dir[] = {{-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
int n, m, q, a[N], t[4*N], leaf;
int timer, tin[N], tout[N], up[N][19];
vector<int> g[N];
void calc(int v, int pr) {
up[v][0] = pr;
tin[v] = ++timer;
for(int i = 1; i <= 18; ++i) {
up[v][i] = up[up[v][i-1]][i-1];
}
for(auto to : g[v]) {
if(to != pr) {
calc(to, v);
}
}
tout[v] = ++timer;
}
bool is_parent(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
if(u == -1) return v;
if(v == -1) return u;
if(is_parent(u, v)) return u;
if(is_parent(v, u)) return v;
for(int i = 18; i >= 0; --i) {
if(!is_parent(up[u][i], v)) {
u = up[u][i];
}
}
return up[u][0];
}
void build(int v, int tl, int tr) {
if(tl == tr) {
t[v] = a[tl];
return;
}
int mid = (tl + tr) >> 1;
build(v * 2, tl, mid);
build(v * 2 + 1, mid + 1, tr);
t[v] = lca(t[v * 2], t[v * 2 + 1]);
}
void upd(int v, int tl, int tr, int id, int x) {
if(tl == tr) {
t[v] = x;
return;
}
int mid = (tl + tr) >> 1;
if(id <= mid)
upd(v * 2, tl, mid, id, x);
else
upd(v * 2 + 1, mid + 1, tr, id, x);
t[v] = lca(t[v * 2], t[v * 2 + 1]);
}
int get(int v, int tl, int tr, int l, int r) {
if(tl >= l && tr <= r)
return t[v];
if(tl > r || l > tr)
return -1;
int mid = (tl + tr) >> 1;
return lca(get(v * 2, tl, mid, l, r), get(v * 2 + 1, mid + 1, tr, l, r));
}
void solve() {
cin >> n >> m >> q;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= m; ++i) cin >> a[i];
calc(1, 1);
build(1, 1, m);
while(q--) {
char tp;
cin >> tp;
int l, r, v;
if(tp == '1') {
cin >> l >> r;
upd(1, 1, m, l, r);
} else {
cin >> l >> r >> v;
int a = -1, b = -1;
for(int i = l; i <= r && a == -1; ++i) {
int L = i, R = r, res = -1;
while(L <= R) {
int mid = (L + R) >> 1;
int x = get(1, 1, m, i, mid);
if(x == v) {
a = i;
b = mid;
break;
}
if(is_parent(x, v)) {
res = mid;
R = mid - 1;
} else
L = mid + 1;
}
}
cout << a << ' ' << b << nl;
}
}
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ": ";
solve();
}
return 0;
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
Compilation message
treearray.cpp: In function 'void solve()':
treearray.cpp:116:23: warning: variable 'res' set but not used [-Wunused-but-set-variable]
116 | int L = i, R = r, res = -1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=5 |
2 |
Correct |
4 ms |
5068 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
3 ms |
5068 KB |
n=100 |
5 |
Correct |
3 ms |
5068 KB |
n=100 |
6 |
Correct |
7 ms |
5068 KB |
n=100 |
7 |
Correct |
8 ms |
5068 KB |
n=100 |
8 |
Correct |
4 ms |
5068 KB |
n=100 |
9 |
Correct |
3 ms |
5068 KB |
n=100 |
10 |
Correct |
4 ms |
5068 KB |
n=100 |
11 |
Correct |
4 ms |
5068 KB |
n=100 |
12 |
Correct |
3 ms |
5068 KB |
n=100 |
13 |
Correct |
3 ms |
5068 KB |
n=100 |
14 |
Correct |
2 ms |
5068 KB |
n=100 |
15 |
Correct |
3 ms |
5068 KB |
n=100 |
16 |
Correct |
2 ms |
5068 KB |
n=100 |
17 |
Correct |
3 ms |
5068 KB |
n=100 |
18 |
Correct |
4 ms |
5076 KB |
n=100 |
19 |
Correct |
3 ms |
5068 KB |
n=100 |
20 |
Correct |
3 ms |
5068 KB |
n=100 |
21 |
Correct |
2 ms |
5068 KB |
n=100 |
22 |
Correct |
2 ms |
5068 KB |
n=100 |
23 |
Correct |
4 ms |
5068 KB |
n=100 |
24 |
Correct |
4 ms |
5068 KB |
n=100 |
25 |
Correct |
3 ms |
5068 KB |
n=100 |
26 |
Correct |
2 ms |
4940 KB |
n=12 |
27 |
Correct |
5 ms |
4992 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=5 |
2 |
Correct |
4 ms |
5068 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
3 ms |
5068 KB |
n=100 |
5 |
Correct |
3 ms |
5068 KB |
n=100 |
6 |
Correct |
7 ms |
5068 KB |
n=100 |
7 |
Correct |
8 ms |
5068 KB |
n=100 |
8 |
Correct |
4 ms |
5068 KB |
n=100 |
9 |
Correct |
3 ms |
5068 KB |
n=100 |
10 |
Correct |
4 ms |
5068 KB |
n=100 |
11 |
Correct |
4 ms |
5068 KB |
n=100 |
12 |
Correct |
3 ms |
5068 KB |
n=100 |
13 |
Correct |
3 ms |
5068 KB |
n=100 |
14 |
Correct |
2 ms |
5068 KB |
n=100 |
15 |
Correct |
3 ms |
5068 KB |
n=100 |
16 |
Correct |
2 ms |
5068 KB |
n=100 |
17 |
Correct |
3 ms |
5068 KB |
n=100 |
18 |
Correct |
4 ms |
5076 KB |
n=100 |
19 |
Correct |
3 ms |
5068 KB |
n=100 |
20 |
Correct |
3 ms |
5068 KB |
n=100 |
21 |
Correct |
2 ms |
5068 KB |
n=100 |
22 |
Correct |
2 ms |
5068 KB |
n=100 |
23 |
Correct |
4 ms |
5068 KB |
n=100 |
24 |
Correct |
4 ms |
5068 KB |
n=100 |
25 |
Correct |
3 ms |
5068 KB |
n=100 |
26 |
Correct |
2 ms |
4940 KB |
n=12 |
27 |
Correct |
5 ms |
4992 KB |
n=100 |
28 |
Correct |
3 ms |
5168 KB |
n=500 |
29 |
Correct |
25 ms |
5180 KB |
n=500 |
30 |
Correct |
21 ms |
5068 KB |
n=500 |
31 |
Correct |
22 ms |
5068 KB |
n=500 |
32 |
Correct |
4 ms |
5068 KB |
n=500 |
33 |
Correct |
19 ms |
5172 KB |
n=500 |
34 |
Correct |
4 ms |
5068 KB |
n=500 |
35 |
Correct |
22 ms |
5180 KB |
n=500 |
36 |
Correct |
156 ms |
5068 KB |
n=500 |
37 |
Correct |
154 ms |
5068 KB |
n=500 |
38 |
Correct |
163 ms |
5152 KB |
n=500 |
39 |
Correct |
56 ms |
5068 KB |
n=500 |
40 |
Correct |
52 ms |
5148 KB |
n=500 |
41 |
Correct |
51 ms |
5168 KB |
n=500 |
42 |
Correct |
83 ms |
5152 KB |
n=500 |
43 |
Correct |
85 ms |
5068 KB |
n=500 |
44 |
Correct |
82 ms |
5068 KB |
n=500 |
45 |
Correct |
3 ms |
5068 KB |
n=500 |
46 |
Correct |
25 ms |
5180 KB |
n=500 |
47 |
Correct |
25 ms |
5180 KB |
n=500 |
48 |
Correct |
3 ms |
5068 KB |
n=500 |
49 |
Correct |
19 ms |
5068 KB |
n=500 |
50 |
Correct |
33 ms |
5068 KB |
n=500 |
51 |
Correct |
36 ms |
5176 KB |
n=500 |
52 |
Correct |
40 ms |
5160 KB |
n=500 |
53 |
Correct |
46 ms |
5068 KB |
n=500 |
54 |
Correct |
22 ms |
5180 KB |
n=500 |
55 |
Correct |
6 ms |
5068 KB |
n=278 |
56 |
Correct |
118 ms |
5068 KB |
n=500 |
57 |
Correct |
129 ms |
5160 KB |
n=500 |
58 |
Correct |
105 ms |
5272 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=5 |
2 |
Correct |
4 ms |
5068 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
3 ms |
5068 KB |
n=100 |
5 |
Correct |
3 ms |
5068 KB |
n=100 |
6 |
Correct |
7 ms |
5068 KB |
n=100 |
7 |
Correct |
8 ms |
5068 KB |
n=100 |
8 |
Correct |
4 ms |
5068 KB |
n=100 |
9 |
Correct |
3 ms |
5068 KB |
n=100 |
10 |
Correct |
4 ms |
5068 KB |
n=100 |
11 |
Correct |
4 ms |
5068 KB |
n=100 |
12 |
Correct |
3 ms |
5068 KB |
n=100 |
13 |
Correct |
3 ms |
5068 KB |
n=100 |
14 |
Correct |
2 ms |
5068 KB |
n=100 |
15 |
Correct |
3 ms |
5068 KB |
n=100 |
16 |
Correct |
2 ms |
5068 KB |
n=100 |
17 |
Correct |
3 ms |
5068 KB |
n=100 |
18 |
Correct |
4 ms |
5076 KB |
n=100 |
19 |
Correct |
3 ms |
5068 KB |
n=100 |
20 |
Correct |
3 ms |
5068 KB |
n=100 |
21 |
Correct |
2 ms |
5068 KB |
n=100 |
22 |
Correct |
2 ms |
5068 KB |
n=100 |
23 |
Correct |
4 ms |
5068 KB |
n=100 |
24 |
Correct |
4 ms |
5068 KB |
n=100 |
25 |
Correct |
3 ms |
5068 KB |
n=100 |
26 |
Correct |
2 ms |
4940 KB |
n=12 |
27 |
Correct |
5 ms |
4992 KB |
n=100 |
28 |
Correct |
3 ms |
5168 KB |
n=500 |
29 |
Correct |
25 ms |
5180 KB |
n=500 |
30 |
Correct |
21 ms |
5068 KB |
n=500 |
31 |
Correct |
22 ms |
5068 KB |
n=500 |
32 |
Correct |
4 ms |
5068 KB |
n=500 |
33 |
Correct |
19 ms |
5172 KB |
n=500 |
34 |
Correct |
4 ms |
5068 KB |
n=500 |
35 |
Correct |
22 ms |
5180 KB |
n=500 |
36 |
Correct |
156 ms |
5068 KB |
n=500 |
37 |
Correct |
154 ms |
5068 KB |
n=500 |
38 |
Correct |
163 ms |
5152 KB |
n=500 |
39 |
Correct |
56 ms |
5068 KB |
n=500 |
40 |
Correct |
52 ms |
5148 KB |
n=500 |
41 |
Correct |
51 ms |
5168 KB |
n=500 |
42 |
Correct |
83 ms |
5152 KB |
n=500 |
43 |
Correct |
85 ms |
5068 KB |
n=500 |
44 |
Correct |
82 ms |
5068 KB |
n=500 |
45 |
Correct |
3 ms |
5068 KB |
n=500 |
46 |
Correct |
25 ms |
5180 KB |
n=500 |
47 |
Correct |
25 ms |
5180 KB |
n=500 |
48 |
Correct |
3 ms |
5068 KB |
n=500 |
49 |
Correct |
19 ms |
5068 KB |
n=500 |
50 |
Correct |
33 ms |
5068 KB |
n=500 |
51 |
Correct |
36 ms |
5176 KB |
n=500 |
52 |
Correct |
40 ms |
5160 KB |
n=500 |
53 |
Correct |
46 ms |
5068 KB |
n=500 |
54 |
Correct |
22 ms |
5180 KB |
n=500 |
55 |
Correct |
6 ms |
5068 KB |
n=278 |
56 |
Correct |
118 ms |
5068 KB |
n=500 |
57 |
Correct |
129 ms |
5160 KB |
n=500 |
58 |
Correct |
105 ms |
5272 KB |
n=500 |
59 |
Correct |
8 ms |
5452 KB |
n=2000 |
60 |
Correct |
506 ms |
5616 KB |
n=2000 |
61 |
Correct |
490 ms |
5588 KB |
n=2000 |
62 |
Correct |
331 ms |
5568 KB |
n=2000 |
63 |
Correct |
17 ms |
5452 KB |
n=2000 |
64 |
Correct |
442 ms |
5580 KB |
n=2000 |
65 |
Correct |
6 ms |
5544 KB |
n=2000 |
66 |
Correct |
548 ms |
5736 KB |
n=2000 |
67 |
Correct |
31 ms |
5452 KB |
n=2000 |
68 |
Correct |
453 ms |
5584 KB |
n=2000 |
69 |
Correct |
3545 ms |
5680 KB |
n=2000 |
70 |
Correct |
3592 ms |
5564 KB |
n=2000 |
71 |
Correct |
3571 ms |
5552 KB |
n=2000 |
72 |
Correct |
1230 ms |
5540 KB |
n=2000 |
73 |
Correct |
1236 ms |
5700 KB |
n=2000 |
74 |
Correct |
5 ms |
5444 KB |
n=1844 |
75 |
Correct |
1257 ms |
5668 KB |
n=2000 |
76 |
Correct |
1833 ms |
5532 KB |
n=2000 |
77 |
Correct |
1818 ms |
5572 KB |
n=2000 |
78 |
Correct |
1929 ms |
5540 KB |
n=2000 |
79 |
Correct |
5 ms |
5452 KB |
n=2000 |
80 |
Correct |
449 ms |
5604 KB |
n=2000 |
81 |
Correct |
397 ms |
5568 KB |
n=2000 |
82 |
Correct |
5 ms |
5452 KB |
n=2000 |
83 |
Correct |
439 ms |
5664 KB |
n=2000 |
84 |
Correct |
848 ms |
5676 KB |
n=2000 |
85 |
Correct |
800 ms |
5580 KB |
n=2000 |
86 |
Correct |
853 ms |
5580 KB |
n=2000 |
87 |
Correct |
860 ms |
5548 KB |
n=2000 |
88 |
Correct |
3166 ms |
5748 KB |
n=2000 |
89 |
Correct |
3165 ms |
5704 KB |
n=2000 |
90 |
Correct |
485 ms |
5580 KB |
n=2000 |
91 |
Correct |
2418 ms |
5580 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=5 |
2 |
Correct |
4 ms |
5068 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
3 ms |
5068 KB |
n=100 |
5 |
Correct |
3 ms |
5068 KB |
n=100 |
6 |
Correct |
7 ms |
5068 KB |
n=100 |
7 |
Correct |
8 ms |
5068 KB |
n=100 |
8 |
Correct |
4 ms |
5068 KB |
n=100 |
9 |
Correct |
3 ms |
5068 KB |
n=100 |
10 |
Correct |
4 ms |
5068 KB |
n=100 |
11 |
Correct |
4 ms |
5068 KB |
n=100 |
12 |
Correct |
3 ms |
5068 KB |
n=100 |
13 |
Correct |
3 ms |
5068 KB |
n=100 |
14 |
Correct |
2 ms |
5068 KB |
n=100 |
15 |
Correct |
3 ms |
5068 KB |
n=100 |
16 |
Correct |
2 ms |
5068 KB |
n=100 |
17 |
Correct |
3 ms |
5068 KB |
n=100 |
18 |
Correct |
4 ms |
5076 KB |
n=100 |
19 |
Correct |
3 ms |
5068 KB |
n=100 |
20 |
Correct |
3 ms |
5068 KB |
n=100 |
21 |
Correct |
2 ms |
5068 KB |
n=100 |
22 |
Correct |
2 ms |
5068 KB |
n=100 |
23 |
Correct |
4 ms |
5068 KB |
n=100 |
24 |
Correct |
4 ms |
5068 KB |
n=100 |
25 |
Correct |
3 ms |
5068 KB |
n=100 |
26 |
Correct |
2 ms |
4940 KB |
n=12 |
27 |
Correct |
5 ms |
4992 KB |
n=100 |
28 |
Correct |
3 ms |
5168 KB |
n=500 |
29 |
Correct |
25 ms |
5180 KB |
n=500 |
30 |
Correct |
21 ms |
5068 KB |
n=500 |
31 |
Correct |
22 ms |
5068 KB |
n=500 |
32 |
Correct |
4 ms |
5068 KB |
n=500 |
33 |
Correct |
19 ms |
5172 KB |
n=500 |
34 |
Correct |
4 ms |
5068 KB |
n=500 |
35 |
Correct |
22 ms |
5180 KB |
n=500 |
36 |
Correct |
156 ms |
5068 KB |
n=500 |
37 |
Correct |
154 ms |
5068 KB |
n=500 |
38 |
Correct |
163 ms |
5152 KB |
n=500 |
39 |
Correct |
56 ms |
5068 KB |
n=500 |
40 |
Correct |
52 ms |
5148 KB |
n=500 |
41 |
Correct |
51 ms |
5168 KB |
n=500 |
42 |
Correct |
83 ms |
5152 KB |
n=500 |
43 |
Correct |
85 ms |
5068 KB |
n=500 |
44 |
Correct |
82 ms |
5068 KB |
n=500 |
45 |
Correct |
3 ms |
5068 KB |
n=500 |
46 |
Correct |
25 ms |
5180 KB |
n=500 |
47 |
Correct |
25 ms |
5180 KB |
n=500 |
48 |
Correct |
3 ms |
5068 KB |
n=500 |
49 |
Correct |
19 ms |
5068 KB |
n=500 |
50 |
Correct |
33 ms |
5068 KB |
n=500 |
51 |
Correct |
36 ms |
5176 KB |
n=500 |
52 |
Correct |
40 ms |
5160 KB |
n=500 |
53 |
Correct |
46 ms |
5068 KB |
n=500 |
54 |
Correct |
22 ms |
5180 KB |
n=500 |
55 |
Correct |
6 ms |
5068 KB |
n=278 |
56 |
Correct |
118 ms |
5068 KB |
n=500 |
57 |
Correct |
129 ms |
5160 KB |
n=500 |
58 |
Correct |
105 ms |
5272 KB |
n=500 |
59 |
Correct |
8 ms |
5452 KB |
n=2000 |
60 |
Correct |
506 ms |
5616 KB |
n=2000 |
61 |
Correct |
490 ms |
5588 KB |
n=2000 |
62 |
Correct |
331 ms |
5568 KB |
n=2000 |
63 |
Correct |
17 ms |
5452 KB |
n=2000 |
64 |
Correct |
442 ms |
5580 KB |
n=2000 |
65 |
Correct |
6 ms |
5544 KB |
n=2000 |
66 |
Correct |
548 ms |
5736 KB |
n=2000 |
67 |
Correct |
31 ms |
5452 KB |
n=2000 |
68 |
Correct |
453 ms |
5584 KB |
n=2000 |
69 |
Correct |
3545 ms |
5680 KB |
n=2000 |
70 |
Correct |
3592 ms |
5564 KB |
n=2000 |
71 |
Correct |
3571 ms |
5552 KB |
n=2000 |
72 |
Correct |
1230 ms |
5540 KB |
n=2000 |
73 |
Correct |
1236 ms |
5700 KB |
n=2000 |
74 |
Correct |
5 ms |
5444 KB |
n=1844 |
75 |
Correct |
1257 ms |
5668 KB |
n=2000 |
76 |
Correct |
1833 ms |
5532 KB |
n=2000 |
77 |
Correct |
1818 ms |
5572 KB |
n=2000 |
78 |
Correct |
1929 ms |
5540 KB |
n=2000 |
79 |
Correct |
5 ms |
5452 KB |
n=2000 |
80 |
Correct |
449 ms |
5604 KB |
n=2000 |
81 |
Correct |
397 ms |
5568 KB |
n=2000 |
82 |
Correct |
5 ms |
5452 KB |
n=2000 |
83 |
Correct |
439 ms |
5664 KB |
n=2000 |
84 |
Correct |
848 ms |
5676 KB |
n=2000 |
85 |
Correct |
800 ms |
5580 KB |
n=2000 |
86 |
Correct |
853 ms |
5580 KB |
n=2000 |
87 |
Correct |
860 ms |
5548 KB |
n=2000 |
88 |
Correct |
3166 ms |
5748 KB |
n=2000 |
89 |
Correct |
3165 ms |
5704 KB |
n=2000 |
90 |
Correct |
485 ms |
5580 KB |
n=2000 |
91 |
Correct |
2418 ms |
5580 KB |
n=2000 |
92 |
Execution timed out |
4046 ms |
56344 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |