#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];
int timer, tin[N], tout[N], up[N][19];
vector<int> g[N];
set<pii> s[N];
set<int> t[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(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 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];
t[a[i]].insert(i);
}
calc(1, 1);
for(int i = 1; i < m; ++i) {
s[lca(a[i], a[i + 1])].insert({i, i + 1});
}
for(int i = 2; i <= m; ++i) {
s[lca(a[i], a[i - 1])].insert({i - 1, i});
}
while(q--) {
char tp;
cin >> tp;
int l, r, v;
if(tp == '1') {
cin >> l >> r;
if(l > 1) s[lca(a[l], a[l - 1])].erase({l - 1, l});
if(l < m) s[lca(a[l], a[l + 1])].erase({l, l + 1});
t[a[l]].erase(l);
a[l] = r;
if(l > 1) s[lca(a[l], a[l - 1])].insert({l - 1, l});
if(l < m) s[lca(a[l], a[l + 1])].insert({l, l + 1});
t[a[l]].insert(l);
} else {
cin >> l >> r >> v;
auto f = *t[v].lower_bound(l);
if(f >= l && f <= r) {
cout << f << ' ' << f << nl;
continue;
}
auto e = *s[v].lower_bound({l, -inf});
if(e.y > r || e.x < l) {
cout << "-1 -1\n";
continue;
}
cout << e.x << ' ' << e.y << 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
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
12 ms |
23756 KB |
n=100 |
4 |
Correct |
12 ms |
23852 KB |
n=100 |
5 |
Correct |
12 ms |
23756 KB |
n=100 |
6 |
Correct |
12 ms |
23756 KB |
n=100 |
7 |
Correct |
12 ms |
23756 KB |
n=100 |
8 |
Correct |
12 ms |
23756 KB |
n=100 |
9 |
Correct |
13 ms |
23756 KB |
n=100 |
10 |
Correct |
12 ms |
23756 KB |
n=100 |
11 |
Correct |
11 ms |
23744 KB |
n=100 |
12 |
Correct |
12 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23752 KB |
n=100 |
14 |
Correct |
12 ms |
23804 KB |
n=100 |
15 |
Correct |
11 ms |
23756 KB |
n=100 |
16 |
Correct |
13 ms |
23756 KB |
n=100 |
17 |
Correct |
12 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
16 ms |
23844 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
12 ms |
23820 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
12 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23852 KB |
n=100 |
26 |
Correct |
12 ms |
23756 KB |
n=12 |
27 |
Correct |
12 ms |
23848 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
12 ms |
23756 KB |
n=100 |
4 |
Correct |
12 ms |
23852 KB |
n=100 |
5 |
Correct |
12 ms |
23756 KB |
n=100 |
6 |
Correct |
12 ms |
23756 KB |
n=100 |
7 |
Correct |
12 ms |
23756 KB |
n=100 |
8 |
Correct |
12 ms |
23756 KB |
n=100 |
9 |
Correct |
13 ms |
23756 KB |
n=100 |
10 |
Correct |
12 ms |
23756 KB |
n=100 |
11 |
Correct |
11 ms |
23744 KB |
n=100 |
12 |
Correct |
12 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23752 KB |
n=100 |
14 |
Correct |
12 ms |
23804 KB |
n=100 |
15 |
Correct |
11 ms |
23756 KB |
n=100 |
16 |
Correct |
13 ms |
23756 KB |
n=100 |
17 |
Correct |
12 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
16 ms |
23844 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
12 ms |
23820 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
12 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23852 KB |
n=100 |
26 |
Correct |
12 ms |
23756 KB |
n=12 |
27 |
Correct |
12 ms |
23848 KB |
n=100 |
28 |
Correct |
14 ms |
23872 KB |
n=500 |
29 |
Correct |
12 ms |
23904 KB |
n=500 |
30 |
Correct |
13 ms |
23884 KB |
n=500 |
31 |
Correct |
14 ms |
23904 KB |
n=500 |
32 |
Correct |
13 ms |
23888 KB |
n=500 |
33 |
Correct |
13 ms |
23884 KB |
n=500 |
34 |
Correct |
12 ms |
23916 KB |
n=500 |
35 |
Correct |
12 ms |
24008 KB |
n=500 |
36 |
Correct |
12 ms |
23884 KB |
n=500 |
37 |
Correct |
13 ms |
23992 KB |
n=500 |
38 |
Correct |
13 ms |
23940 KB |
n=500 |
39 |
Correct |
12 ms |
23884 KB |
n=500 |
40 |
Correct |
12 ms |
23884 KB |
n=500 |
41 |
Correct |
12 ms |
23884 KB |
n=500 |
42 |
Correct |
13 ms |
23988 KB |
n=500 |
43 |
Correct |
13 ms |
23980 KB |
n=500 |
44 |
Correct |
13 ms |
23972 KB |
n=500 |
45 |
Correct |
12 ms |
23900 KB |
n=500 |
46 |
Correct |
12 ms |
23992 KB |
n=500 |
47 |
Correct |
15 ms |
23884 KB |
n=500 |
48 |
Correct |
14 ms |
24104 KB |
n=500 |
49 |
Correct |
12 ms |
23884 KB |
n=500 |
50 |
Correct |
13 ms |
23884 KB |
n=500 |
51 |
Correct |
12 ms |
23884 KB |
n=500 |
52 |
Correct |
13 ms |
24008 KB |
n=500 |
53 |
Correct |
13 ms |
23884 KB |
n=500 |
54 |
Correct |
16 ms |
23884 KB |
n=500 |
55 |
Correct |
12 ms |
23884 KB |
n=278 |
56 |
Correct |
12 ms |
23884 KB |
n=500 |
57 |
Correct |
12 ms |
24008 KB |
n=500 |
58 |
Correct |
13 ms |
23884 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
12 ms |
23756 KB |
n=100 |
4 |
Correct |
12 ms |
23852 KB |
n=100 |
5 |
Correct |
12 ms |
23756 KB |
n=100 |
6 |
Correct |
12 ms |
23756 KB |
n=100 |
7 |
Correct |
12 ms |
23756 KB |
n=100 |
8 |
Correct |
12 ms |
23756 KB |
n=100 |
9 |
Correct |
13 ms |
23756 KB |
n=100 |
10 |
Correct |
12 ms |
23756 KB |
n=100 |
11 |
Correct |
11 ms |
23744 KB |
n=100 |
12 |
Correct |
12 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23752 KB |
n=100 |
14 |
Correct |
12 ms |
23804 KB |
n=100 |
15 |
Correct |
11 ms |
23756 KB |
n=100 |
16 |
Correct |
13 ms |
23756 KB |
n=100 |
17 |
Correct |
12 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
16 ms |
23844 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
12 ms |
23820 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
12 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23852 KB |
n=100 |
26 |
Correct |
12 ms |
23756 KB |
n=12 |
27 |
Correct |
12 ms |
23848 KB |
n=100 |
28 |
Correct |
14 ms |
23872 KB |
n=500 |
29 |
Correct |
12 ms |
23904 KB |
n=500 |
30 |
Correct |
13 ms |
23884 KB |
n=500 |
31 |
Correct |
14 ms |
23904 KB |
n=500 |
32 |
Correct |
13 ms |
23888 KB |
n=500 |
33 |
Correct |
13 ms |
23884 KB |
n=500 |
34 |
Correct |
12 ms |
23916 KB |
n=500 |
35 |
Correct |
12 ms |
24008 KB |
n=500 |
36 |
Correct |
12 ms |
23884 KB |
n=500 |
37 |
Correct |
13 ms |
23992 KB |
n=500 |
38 |
Correct |
13 ms |
23940 KB |
n=500 |
39 |
Correct |
12 ms |
23884 KB |
n=500 |
40 |
Correct |
12 ms |
23884 KB |
n=500 |
41 |
Correct |
12 ms |
23884 KB |
n=500 |
42 |
Correct |
13 ms |
23988 KB |
n=500 |
43 |
Correct |
13 ms |
23980 KB |
n=500 |
44 |
Correct |
13 ms |
23972 KB |
n=500 |
45 |
Correct |
12 ms |
23900 KB |
n=500 |
46 |
Correct |
12 ms |
23992 KB |
n=500 |
47 |
Correct |
15 ms |
23884 KB |
n=500 |
48 |
Correct |
14 ms |
24104 KB |
n=500 |
49 |
Correct |
12 ms |
23884 KB |
n=500 |
50 |
Correct |
13 ms |
23884 KB |
n=500 |
51 |
Correct |
12 ms |
23884 KB |
n=500 |
52 |
Correct |
13 ms |
24008 KB |
n=500 |
53 |
Correct |
13 ms |
23884 KB |
n=500 |
54 |
Correct |
16 ms |
23884 KB |
n=500 |
55 |
Correct |
12 ms |
23884 KB |
n=278 |
56 |
Correct |
12 ms |
23884 KB |
n=500 |
57 |
Correct |
12 ms |
24008 KB |
n=500 |
58 |
Correct |
13 ms |
23884 KB |
n=500 |
59 |
Correct |
16 ms |
24396 KB |
n=2000 |
60 |
Correct |
14 ms |
24552 KB |
n=2000 |
61 |
Correct |
14 ms |
24488 KB |
n=2000 |
62 |
Correct |
15 ms |
24500 KB |
n=2000 |
63 |
Correct |
15 ms |
24396 KB |
n=2000 |
64 |
Correct |
15 ms |
24516 KB |
n=2000 |
65 |
Correct |
15 ms |
24396 KB |
n=2000 |
66 |
Correct |
15 ms |
24524 KB |
n=2000 |
67 |
Correct |
15 ms |
24396 KB |
n=2000 |
68 |
Correct |
18 ms |
24452 KB |
n=2000 |
69 |
Correct |
15 ms |
24488 KB |
n=2000 |
70 |
Correct |
14 ms |
24396 KB |
n=2000 |
71 |
Correct |
15 ms |
24488 KB |
n=2000 |
72 |
Correct |
15 ms |
24472 KB |
n=2000 |
73 |
Correct |
14 ms |
24396 KB |
n=2000 |
74 |
Correct |
15 ms |
24396 KB |
n=1844 |
75 |
Correct |
14 ms |
24396 KB |
n=2000 |
76 |
Correct |
16 ms |
24436 KB |
n=2000 |
77 |
Correct |
16 ms |
24396 KB |
n=2000 |
78 |
Correct |
16 ms |
24396 KB |
n=2000 |
79 |
Correct |
15 ms |
24448 KB |
n=2000 |
80 |
Correct |
14 ms |
24524 KB |
n=2000 |
81 |
Correct |
17 ms |
24392 KB |
n=2000 |
82 |
Correct |
15 ms |
24396 KB |
n=2000 |
83 |
Correct |
15 ms |
24440 KB |
n=2000 |
84 |
Correct |
15 ms |
24480 KB |
n=2000 |
85 |
Correct |
15 ms |
24396 KB |
n=2000 |
86 |
Correct |
15 ms |
24436 KB |
n=2000 |
87 |
Correct |
16 ms |
24396 KB |
n=2000 |
88 |
Correct |
14 ms |
24532 KB |
n=2000 |
89 |
Correct |
14 ms |
24476 KB |
n=2000 |
90 |
Correct |
14 ms |
24524 KB |
n=2000 |
91 |
Correct |
15 ms |
24396 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
12 ms |
23756 KB |
n=100 |
4 |
Correct |
12 ms |
23852 KB |
n=100 |
5 |
Correct |
12 ms |
23756 KB |
n=100 |
6 |
Correct |
12 ms |
23756 KB |
n=100 |
7 |
Correct |
12 ms |
23756 KB |
n=100 |
8 |
Correct |
12 ms |
23756 KB |
n=100 |
9 |
Correct |
13 ms |
23756 KB |
n=100 |
10 |
Correct |
12 ms |
23756 KB |
n=100 |
11 |
Correct |
11 ms |
23744 KB |
n=100 |
12 |
Correct |
12 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23752 KB |
n=100 |
14 |
Correct |
12 ms |
23804 KB |
n=100 |
15 |
Correct |
11 ms |
23756 KB |
n=100 |
16 |
Correct |
13 ms |
23756 KB |
n=100 |
17 |
Correct |
12 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
16 ms |
23844 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
12 ms |
23820 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
12 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23852 KB |
n=100 |
26 |
Correct |
12 ms |
23756 KB |
n=12 |
27 |
Correct |
12 ms |
23848 KB |
n=100 |
28 |
Correct |
14 ms |
23872 KB |
n=500 |
29 |
Correct |
12 ms |
23904 KB |
n=500 |
30 |
Correct |
13 ms |
23884 KB |
n=500 |
31 |
Correct |
14 ms |
23904 KB |
n=500 |
32 |
Correct |
13 ms |
23888 KB |
n=500 |
33 |
Correct |
13 ms |
23884 KB |
n=500 |
34 |
Correct |
12 ms |
23916 KB |
n=500 |
35 |
Correct |
12 ms |
24008 KB |
n=500 |
36 |
Correct |
12 ms |
23884 KB |
n=500 |
37 |
Correct |
13 ms |
23992 KB |
n=500 |
38 |
Correct |
13 ms |
23940 KB |
n=500 |
39 |
Correct |
12 ms |
23884 KB |
n=500 |
40 |
Correct |
12 ms |
23884 KB |
n=500 |
41 |
Correct |
12 ms |
23884 KB |
n=500 |
42 |
Correct |
13 ms |
23988 KB |
n=500 |
43 |
Correct |
13 ms |
23980 KB |
n=500 |
44 |
Correct |
13 ms |
23972 KB |
n=500 |
45 |
Correct |
12 ms |
23900 KB |
n=500 |
46 |
Correct |
12 ms |
23992 KB |
n=500 |
47 |
Correct |
15 ms |
23884 KB |
n=500 |
48 |
Correct |
14 ms |
24104 KB |
n=500 |
49 |
Correct |
12 ms |
23884 KB |
n=500 |
50 |
Correct |
13 ms |
23884 KB |
n=500 |
51 |
Correct |
12 ms |
23884 KB |
n=500 |
52 |
Correct |
13 ms |
24008 KB |
n=500 |
53 |
Correct |
13 ms |
23884 KB |
n=500 |
54 |
Correct |
16 ms |
23884 KB |
n=500 |
55 |
Correct |
12 ms |
23884 KB |
n=278 |
56 |
Correct |
12 ms |
23884 KB |
n=500 |
57 |
Correct |
12 ms |
24008 KB |
n=500 |
58 |
Correct |
13 ms |
23884 KB |
n=500 |
59 |
Correct |
16 ms |
24396 KB |
n=2000 |
60 |
Correct |
14 ms |
24552 KB |
n=2000 |
61 |
Correct |
14 ms |
24488 KB |
n=2000 |
62 |
Correct |
15 ms |
24500 KB |
n=2000 |
63 |
Correct |
15 ms |
24396 KB |
n=2000 |
64 |
Correct |
15 ms |
24516 KB |
n=2000 |
65 |
Correct |
15 ms |
24396 KB |
n=2000 |
66 |
Correct |
15 ms |
24524 KB |
n=2000 |
67 |
Correct |
15 ms |
24396 KB |
n=2000 |
68 |
Correct |
18 ms |
24452 KB |
n=2000 |
69 |
Correct |
15 ms |
24488 KB |
n=2000 |
70 |
Correct |
14 ms |
24396 KB |
n=2000 |
71 |
Correct |
15 ms |
24488 KB |
n=2000 |
72 |
Correct |
15 ms |
24472 KB |
n=2000 |
73 |
Correct |
14 ms |
24396 KB |
n=2000 |
74 |
Correct |
15 ms |
24396 KB |
n=1844 |
75 |
Correct |
14 ms |
24396 KB |
n=2000 |
76 |
Correct |
16 ms |
24436 KB |
n=2000 |
77 |
Correct |
16 ms |
24396 KB |
n=2000 |
78 |
Correct |
16 ms |
24396 KB |
n=2000 |
79 |
Correct |
15 ms |
24448 KB |
n=2000 |
80 |
Correct |
14 ms |
24524 KB |
n=2000 |
81 |
Correct |
17 ms |
24392 KB |
n=2000 |
82 |
Correct |
15 ms |
24396 KB |
n=2000 |
83 |
Correct |
15 ms |
24440 KB |
n=2000 |
84 |
Correct |
15 ms |
24480 KB |
n=2000 |
85 |
Correct |
15 ms |
24396 KB |
n=2000 |
86 |
Correct |
15 ms |
24436 KB |
n=2000 |
87 |
Correct |
16 ms |
24396 KB |
n=2000 |
88 |
Correct |
14 ms |
24532 KB |
n=2000 |
89 |
Correct |
14 ms |
24476 KB |
n=2000 |
90 |
Correct |
14 ms |
24524 KB |
n=2000 |
91 |
Correct |
15 ms |
24396 KB |
n=2000 |
92 |
Correct |
753 ms |
89240 KB |
n=200000 |
93 |
Correct |
884 ms |
96460 KB |
n=200000 |
94 |
Correct |
621 ms |
103560 KB |
n=200000 |
95 |
Correct |
717 ms |
95472 KB |
n=200000 |
96 |
Correct |
715 ms |
95400 KB |
n=200000 |
97 |
Correct |
1014 ms |
99824 KB |
n=200000 |
98 |
Correct |
718 ms |
95508 KB |
n=200000 |
99 |
Correct |
876 ms |
95888 KB |
n=200000 |
100 |
Correct |
713 ms |
95668 KB |
n=200000 |
101 |
Correct |
519 ms |
104460 KB |
n=200000 |
102 |
Correct |
469 ms |
96828 KB |
n=200000 |
103 |
Correct |
462 ms |
96836 KB |
n=200000 |
104 |
Correct |
461 ms |
96880 KB |
n=200000 |
105 |
Correct |
477 ms |
97208 KB |
n=200000 |
106 |
Correct |
485 ms |
97320 KB |
n=200000 |
107 |
Correct |
468 ms |
97196 KB |
n=200000 |
108 |
Correct |
820 ms |
95740 KB |
n=200000 |
109 |
Correct |
795 ms |
95880 KB |
n=200000 |
110 |
Correct |
829 ms |
95744 KB |
n=200000 |
111 |
Correct |
727 ms |
94992 KB |
n=200000 |
112 |
Correct |
578 ms |
103688 KB |
n=200000 |
113 |
Correct |
965 ms |
99668 KB |
n=200000 |
114 |
Correct |
744 ms |
95080 KB |
n=200000 |
115 |
Correct |
1157 ms |
97812 KB |
n=200000 |
116 |
Correct |
700 ms |
95788 KB |
n=200000 |
117 |
Correct |
542 ms |
103732 KB |
n=200000 |
118 |
Correct |
1036 ms |
98756 KB |
n=200000 |
119 |
Correct |
692 ms |
95596 KB |
n=200000 |
120 |
Correct |
470 ms |
103408 KB |
n=200000 |
121 |
Correct |
465 ms |
103436 KB |
n=200000 |
122 |
Correct |
471 ms |
103544 KB |
n=200000 |
123 |
Correct |
483 ms |
97092 KB |
n=200000 |
124 |
Correct |
165 ms |
42624 KB |
n=25264 |