#pragma GCC optimize("O3", "unroll-loops") // "Ofast"
#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
#include <bits/stdc++.h>
#define int long long
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define dbg(x) cerr << #x << " = " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vvt = vt< vt<int> >;
const int N = 2e5 + 5, mod = 1e9 + 7, inf = 1e18 + 7, W = 500, LIM = (1ll << 60);
const double eps = 1e-6;
int n, m, q, up[N][21], tin[N], tout[N], timer;
vt<int> g[N];
void dfs (int v, int p = 1) {
tin[v] = timer++;
up[v][0] = p;
for (int i = 1; i <= 20; i++) {
int jump = up[v][i - 1];
up[v][i] = up[jump][i - 1];
}
for (auto to: g[v]) {
if (to != p) dfs(to, v);
}
tout[v] = timer++;
}
bool is_parent (int a, int b) {
return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca (int a, int b) {
if (is_parent(a, b)) return a;
if (is_parent(b, a)) return b;
for (int i = 20; i >= 0; i--) {
if (!is_parent(up[a][i], b)) a = up[a][i];
}
return up[a][0];
}
int a[N];
set<int> s[N], st[N];
void upd (int i, int x) {
st[a[i]].erase(i);
if (i + 1 <= m) {
int lc = lca(a[i], a[i + 1]);
s[lc].erase(i);
}
if (i - 1 >= 1) {
int lc = lca(a[i], a[i - 1]);
s[lc].erase(i - 1);
}
a[i] = x;
st[a[i]].insert(i);
if (i + 1 <= m) {
int lc = lca(a[i], a[i + 1]);
s[lc].insert(i);
}
if (i - 1 >= 1) {
int lc = lca(a[i], a[i - 1]);
s[lc].insert(i - 1);
}
}
pii get (int l, int r, int v) {
auto it = s[v].lower_bound(l);
if (it != s[v].end()) {
int x = *it;
if (x + 1 <= r) return {x, x + 1};
}
it = st[v].lower_bound(l);
if (it != st[v].end()) {
int x = *it;
if (x <= r) return {x, x};
}
return {-1, -1};
}
void solve () {
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
dfs(1);
for (int i = 1; i <= m; i++) cin >> a[i], st[a[i]].insert(i);
for (int i = 1; i < m; i++) s[lca(a[i], a[i + 1])].insert(i);
while (q--) {
int tp;
cin >> tp;
if (tp == 1) {
int pos, x;
cin >> pos >> x;
upd(pos, x);
}
if (tp == 2) {
int l, r, v;
cin >> l >> r >> v;
pii ans = get(l, r, v);
cout << ans.ff << ' ' << ans.ss << '\n';
}
}
cout << '\n';
}
bool testcases = 0;
signed main() {
#ifdef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
cin.tie(0) -> sync_with_stdio(0);
int test = 1;
if (testcases) cin >> test;
for (int cs = 1; cs <= test; cs++) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
16 ms |
23800 KB |
n=100 |
3 |
Correct |
13 ms |
23764 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23828 KB |
n=100 |
6 |
Correct |
15 ms |
23776 KB |
n=100 |
7 |
Correct |
14 ms |
23812 KB |
n=100 |
8 |
Correct |
17 ms |
23864 KB |
n=100 |
9 |
Correct |
14 ms |
23840 KB |
n=100 |
10 |
Correct |
14 ms |
23824 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
15 ms |
23820 KB |
n=100 |
13 |
Correct |
13 ms |
23824 KB |
n=100 |
14 |
Correct |
14 ms |
23852 KB |
n=100 |
15 |
Correct |
14 ms |
23764 KB |
n=100 |
16 |
Correct |
13 ms |
23844 KB |
n=100 |
17 |
Correct |
13 ms |
23860 KB |
n=100 |
18 |
Correct |
13 ms |
23820 KB |
n=100 |
19 |
Correct |
13 ms |
23848 KB |
n=100 |
20 |
Correct |
14 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23820 KB |
n=100 |
23 |
Correct |
14 ms |
23828 KB |
n=100 |
24 |
Correct |
14 ms |
23796 KB |
n=100 |
25 |
Correct |
13 ms |
23764 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23832 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
16 ms |
23800 KB |
n=100 |
3 |
Correct |
13 ms |
23764 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23828 KB |
n=100 |
6 |
Correct |
15 ms |
23776 KB |
n=100 |
7 |
Correct |
14 ms |
23812 KB |
n=100 |
8 |
Correct |
17 ms |
23864 KB |
n=100 |
9 |
Correct |
14 ms |
23840 KB |
n=100 |
10 |
Correct |
14 ms |
23824 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
15 ms |
23820 KB |
n=100 |
13 |
Correct |
13 ms |
23824 KB |
n=100 |
14 |
Correct |
14 ms |
23852 KB |
n=100 |
15 |
Correct |
14 ms |
23764 KB |
n=100 |
16 |
Correct |
13 ms |
23844 KB |
n=100 |
17 |
Correct |
13 ms |
23860 KB |
n=100 |
18 |
Correct |
13 ms |
23820 KB |
n=100 |
19 |
Correct |
13 ms |
23848 KB |
n=100 |
20 |
Correct |
14 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23820 KB |
n=100 |
23 |
Correct |
14 ms |
23828 KB |
n=100 |
24 |
Correct |
14 ms |
23796 KB |
n=100 |
25 |
Correct |
13 ms |
23764 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23832 KB |
n=100 |
28 |
Correct |
13 ms |
23892 KB |
n=500 |
29 |
Correct |
16 ms |
23952 KB |
n=500 |
30 |
Correct |
14 ms |
24020 KB |
n=500 |
31 |
Correct |
13 ms |
24012 KB |
n=500 |
32 |
Correct |
13 ms |
23880 KB |
n=500 |
33 |
Correct |
14 ms |
23892 KB |
n=500 |
34 |
Correct |
13 ms |
23952 KB |
n=500 |
35 |
Correct |
13 ms |
24032 KB |
n=500 |
36 |
Correct |
15 ms |
23892 KB |
n=500 |
37 |
Correct |
16 ms |
23956 KB |
n=500 |
38 |
Correct |
15 ms |
23892 KB |
n=500 |
39 |
Correct |
14 ms |
24020 KB |
n=500 |
40 |
Correct |
13 ms |
24020 KB |
n=500 |
41 |
Correct |
13 ms |
24016 KB |
n=500 |
42 |
Correct |
13 ms |
23996 KB |
n=500 |
43 |
Correct |
15 ms |
23892 KB |
n=500 |
44 |
Correct |
14 ms |
23892 KB |
n=500 |
45 |
Correct |
14 ms |
23952 KB |
n=500 |
46 |
Correct |
13 ms |
23972 KB |
n=500 |
47 |
Correct |
13 ms |
23960 KB |
n=500 |
48 |
Correct |
13 ms |
23956 KB |
n=500 |
49 |
Correct |
13 ms |
23956 KB |
n=500 |
50 |
Correct |
14 ms |
23892 KB |
n=500 |
51 |
Correct |
13 ms |
23892 KB |
n=500 |
52 |
Correct |
14 ms |
23964 KB |
n=500 |
53 |
Correct |
13 ms |
23960 KB |
n=500 |
54 |
Correct |
14 ms |
23892 KB |
n=500 |
55 |
Correct |
14 ms |
23892 KB |
n=278 |
56 |
Correct |
14 ms |
23948 KB |
n=500 |
57 |
Correct |
13 ms |
23900 KB |
n=500 |
58 |
Correct |
14 ms |
23936 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
16 ms |
23800 KB |
n=100 |
3 |
Correct |
13 ms |
23764 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23828 KB |
n=100 |
6 |
Correct |
15 ms |
23776 KB |
n=100 |
7 |
Correct |
14 ms |
23812 KB |
n=100 |
8 |
Correct |
17 ms |
23864 KB |
n=100 |
9 |
Correct |
14 ms |
23840 KB |
n=100 |
10 |
Correct |
14 ms |
23824 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
15 ms |
23820 KB |
n=100 |
13 |
Correct |
13 ms |
23824 KB |
n=100 |
14 |
Correct |
14 ms |
23852 KB |
n=100 |
15 |
Correct |
14 ms |
23764 KB |
n=100 |
16 |
Correct |
13 ms |
23844 KB |
n=100 |
17 |
Correct |
13 ms |
23860 KB |
n=100 |
18 |
Correct |
13 ms |
23820 KB |
n=100 |
19 |
Correct |
13 ms |
23848 KB |
n=100 |
20 |
Correct |
14 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23820 KB |
n=100 |
23 |
Correct |
14 ms |
23828 KB |
n=100 |
24 |
Correct |
14 ms |
23796 KB |
n=100 |
25 |
Correct |
13 ms |
23764 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23832 KB |
n=100 |
28 |
Correct |
13 ms |
23892 KB |
n=500 |
29 |
Correct |
16 ms |
23952 KB |
n=500 |
30 |
Correct |
14 ms |
24020 KB |
n=500 |
31 |
Correct |
13 ms |
24012 KB |
n=500 |
32 |
Correct |
13 ms |
23880 KB |
n=500 |
33 |
Correct |
14 ms |
23892 KB |
n=500 |
34 |
Correct |
13 ms |
23952 KB |
n=500 |
35 |
Correct |
13 ms |
24032 KB |
n=500 |
36 |
Correct |
15 ms |
23892 KB |
n=500 |
37 |
Correct |
16 ms |
23956 KB |
n=500 |
38 |
Correct |
15 ms |
23892 KB |
n=500 |
39 |
Correct |
14 ms |
24020 KB |
n=500 |
40 |
Correct |
13 ms |
24020 KB |
n=500 |
41 |
Correct |
13 ms |
24016 KB |
n=500 |
42 |
Correct |
13 ms |
23996 KB |
n=500 |
43 |
Correct |
15 ms |
23892 KB |
n=500 |
44 |
Correct |
14 ms |
23892 KB |
n=500 |
45 |
Correct |
14 ms |
23952 KB |
n=500 |
46 |
Correct |
13 ms |
23972 KB |
n=500 |
47 |
Correct |
13 ms |
23960 KB |
n=500 |
48 |
Correct |
13 ms |
23956 KB |
n=500 |
49 |
Correct |
13 ms |
23956 KB |
n=500 |
50 |
Correct |
14 ms |
23892 KB |
n=500 |
51 |
Correct |
13 ms |
23892 KB |
n=500 |
52 |
Correct |
14 ms |
23964 KB |
n=500 |
53 |
Correct |
13 ms |
23960 KB |
n=500 |
54 |
Correct |
14 ms |
23892 KB |
n=500 |
55 |
Correct |
14 ms |
23892 KB |
n=278 |
56 |
Correct |
14 ms |
23948 KB |
n=500 |
57 |
Correct |
13 ms |
23900 KB |
n=500 |
58 |
Correct |
14 ms |
23936 KB |
n=500 |
59 |
Correct |
17 ms |
24472 KB |
n=2000 |
60 |
Correct |
15 ms |
24568 KB |
n=2000 |
61 |
Correct |
17 ms |
24532 KB |
n=2000 |
62 |
Correct |
18 ms |
24440 KB |
n=2000 |
63 |
Correct |
18 ms |
24472 KB |
n=2000 |
64 |
Correct |
16 ms |
24532 KB |
n=2000 |
65 |
Correct |
16 ms |
24532 KB |
n=2000 |
66 |
Correct |
16 ms |
24472 KB |
n=2000 |
67 |
Correct |
18 ms |
24532 KB |
n=2000 |
68 |
Correct |
16 ms |
24532 KB |
n=2000 |
69 |
Correct |
16 ms |
24532 KB |
n=2000 |
70 |
Correct |
16 ms |
24532 KB |
n=2000 |
71 |
Correct |
16 ms |
24472 KB |
n=2000 |
72 |
Correct |
15 ms |
24436 KB |
n=2000 |
73 |
Correct |
14 ms |
24468 KB |
n=2000 |
74 |
Correct |
16 ms |
24472 KB |
n=1844 |
75 |
Correct |
18 ms |
24532 KB |
n=2000 |
76 |
Correct |
17 ms |
24404 KB |
n=2000 |
77 |
Correct |
17 ms |
24532 KB |
n=2000 |
78 |
Correct |
16 ms |
24440 KB |
n=2000 |
79 |
Correct |
16 ms |
24532 KB |
n=2000 |
80 |
Correct |
18 ms |
24544 KB |
n=2000 |
81 |
Correct |
17 ms |
24532 KB |
n=2000 |
82 |
Correct |
20 ms |
24516 KB |
n=2000 |
83 |
Correct |
19 ms |
24532 KB |
n=2000 |
84 |
Correct |
17 ms |
24496 KB |
n=2000 |
85 |
Correct |
16 ms |
24532 KB |
n=2000 |
86 |
Correct |
19 ms |
24436 KB |
n=2000 |
87 |
Correct |
18 ms |
24480 KB |
n=2000 |
88 |
Correct |
16 ms |
24608 KB |
n=2000 |
89 |
Correct |
15 ms |
24532 KB |
n=2000 |
90 |
Correct |
16 ms |
24560 KB |
n=2000 |
91 |
Correct |
16 ms |
24468 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
16 ms |
23800 KB |
n=100 |
3 |
Correct |
13 ms |
23764 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23828 KB |
n=100 |
6 |
Correct |
15 ms |
23776 KB |
n=100 |
7 |
Correct |
14 ms |
23812 KB |
n=100 |
8 |
Correct |
17 ms |
23864 KB |
n=100 |
9 |
Correct |
14 ms |
23840 KB |
n=100 |
10 |
Correct |
14 ms |
23824 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
15 ms |
23820 KB |
n=100 |
13 |
Correct |
13 ms |
23824 KB |
n=100 |
14 |
Correct |
14 ms |
23852 KB |
n=100 |
15 |
Correct |
14 ms |
23764 KB |
n=100 |
16 |
Correct |
13 ms |
23844 KB |
n=100 |
17 |
Correct |
13 ms |
23860 KB |
n=100 |
18 |
Correct |
13 ms |
23820 KB |
n=100 |
19 |
Correct |
13 ms |
23848 KB |
n=100 |
20 |
Correct |
14 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23820 KB |
n=100 |
23 |
Correct |
14 ms |
23828 KB |
n=100 |
24 |
Correct |
14 ms |
23796 KB |
n=100 |
25 |
Correct |
13 ms |
23764 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23832 KB |
n=100 |
28 |
Correct |
13 ms |
23892 KB |
n=500 |
29 |
Correct |
16 ms |
23952 KB |
n=500 |
30 |
Correct |
14 ms |
24020 KB |
n=500 |
31 |
Correct |
13 ms |
24012 KB |
n=500 |
32 |
Correct |
13 ms |
23880 KB |
n=500 |
33 |
Correct |
14 ms |
23892 KB |
n=500 |
34 |
Correct |
13 ms |
23952 KB |
n=500 |
35 |
Correct |
13 ms |
24032 KB |
n=500 |
36 |
Correct |
15 ms |
23892 KB |
n=500 |
37 |
Correct |
16 ms |
23956 KB |
n=500 |
38 |
Correct |
15 ms |
23892 KB |
n=500 |
39 |
Correct |
14 ms |
24020 KB |
n=500 |
40 |
Correct |
13 ms |
24020 KB |
n=500 |
41 |
Correct |
13 ms |
24016 KB |
n=500 |
42 |
Correct |
13 ms |
23996 KB |
n=500 |
43 |
Correct |
15 ms |
23892 KB |
n=500 |
44 |
Correct |
14 ms |
23892 KB |
n=500 |
45 |
Correct |
14 ms |
23952 KB |
n=500 |
46 |
Correct |
13 ms |
23972 KB |
n=500 |
47 |
Correct |
13 ms |
23960 KB |
n=500 |
48 |
Correct |
13 ms |
23956 KB |
n=500 |
49 |
Correct |
13 ms |
23956 KB |
n=500 |
50 |
Correct |
14 ms |
23892 KB |
n=500 |
51 |
Correct |
13 ms |
23892 KB |
n=500 |
52 |
Correct |
14 ms |
23964 KB |
n=500 |
53 |
Correct |
13 ms |
23960 KB |
n=500 |
54 |
Correct |
14 ms |
23892 KB |
n=500 |
55 |
Correct |
14 ms |
23892 KB |
n=278 |
56 |
Correct |
14 ms |
23948 KB |
n=500 |
57 |
Correct |
13 ms |
23900 KB |
n=500 |
58 |
Correct |
14 ms |
23936 KB |
n=500 |
59 |
Correct |
17 ms |
24472 KB |
n=2000 |
60 |
Correct |
15 ms |
24568 KB |
n=2000 |
61 |
Correct |
17 ms |
24532 KB |
n=2000 |
62 |
Correct |
18 ms |
24440 KB |
n=2000 |
63 |
Correct |
18 ms |
24472 KB |
n=2000 |
64 |
Correct |
16 ms |
24532 KB |
n=2000 |
65 |
Correct |
16 ms |
24532 KB |
n=2000 |
66 |
Correct |
16 ms |
24472 KB |
n=2000 |
67 |
Correct |
18 ms |
24532 KB |
n=2000 |
68 |
Correct |
16 ms |
24532 KB |
n=2000 |
69 |
Correct |
16 ms |
24532 KB |
n=2000 |
70 |
Correct |
16 ms |
24532 KB |
n=2000 |
71 |
Correct |
16 ms |
24472 KB |
n=2000 |
72 |
Correct |
15 ms |
24436 KB |
n=2000 |
73 |
Correct |
14 ms |
24468 KB |
n=2000 |
74 |
Correct |
16 ms |
24472 KB |
n=1844 |
75 |
Correct |
18 ms |
24532 KB |
n=2000 |
76 |
Correct |
17 ms |
24404 KB |
n=2000 |
77 |
Correct |
17 ms |
24532 KB |
n=2000 |
78 |
Correct |
16 ms |
24440 KB |
n=2000 |
79 |
Correct |
16 ms |
24532 KB |
n=2000 |
80 |
Correct |
18 ms |
24544 KB |
n=2000 |
81 |
Correct |
17 ms |
24532 KB |
n=2000 |
82 |
Correct |
20 ms |
24516 KB |
n=2000 |
83 |
Correct |
19 ms |
24532 KB |
n=2000 |
84 |
Correct |
17 ms |
24496 KB |
n=2000 |
85 |
Correct |
16 ms |
24532 KB |
n=2000 |
86 |
Correct |
19 ms |
24436 KB |
n=2000 |
87 |
Correct |
18 ms |
24480 KB |
n=2000 |
88 |
Correct |
16 ms |
24608 KB |
n=2000 |
89 |
Correct |
15 ms |
24532 KB |
n=2000 |
90 |
Correct |
16 ms |
24560 KB |
n=2000 |
91 |
Correct |
16 ms |
24468 KB |
n=2000 |
92 |
Correct |
791 ms |
95720 KB |
n=200000 |
93 |
Correct |
1000 ms |
100640 KB |
n=200000 |
94 |
Correct |
679 ms |
103436 KB |
n=200000 |
95 |
Correct |
765 ms |
95480 KB |
n=200000 |
96 |
Correct |
771 ms |
95480 KB |
n=200000 |
97 |
Correct |
1050 ms |
99780 KB |
n=200000 |
98 |
Correct |
779 ms |
95440 KB |
n=200000 |
99 |
Correct |
869 ms |
95796 KB |
n=200000 |
100 |
Correct |
768 ms |
95564 KB |
n=200000 |
101 |
Correct |
570 ms |
104336 KB |
n=200000 |
102 |
Correct |
465 ms |
96844 KB |
n=200000 |
103 |
Correct |
411 ms |
96932 KB |
n=200000 |
104 |
Correct |
445 ms |
96860 KB |
n=200000 |
105 |
Correct |
429 ms |
97232 KB |
n=200000 |
106 |
Correct |
462 ms |
97272 KB |
n=200000 |
107 |
Correct |
449 ms |
97288 KB |
n=200000 |
108 |
Correct |
813 ms |
95740 KB |
n=200000 |
109 |
Correct |
783 ms |
95768 KB |
n=200000 |
110 |
Correct |
845 ms |
95712 KB |
n=200000 |
111 |
Correct |
793 ms |
95020 KB |
n=200000 |
112 |
Correct |
656 ms |
103660 KB |
n=200000 |
113 |
Correct |
981 ms |
99808 KB |
n=200000 |
114 |
Correct |
762 ms |
95012 KB |
n=200000 |
115 |
Correct |
1173 ms |
97972 KB |
n=200000 |
116 |
Correct |
701 ms |
95784 KB |
n=200000 |
117 |
Correct |
587 ms |
104076 KB |
n=200000 |
118 |
Correct |
1068 ms |
98712 KB |
n=200000 |
119 |
Correct |
702 ms |
95664 KB |
n=200000 |
120 |
Correct |
495 ms |
103300 KB |
n=200000 |
121 |
Correct |
527 ms |
103296 KB |
n=200000 |
122 |
Correct |
497 ms |
103632 KB |
n=200000 |
123 |
Correct |
445 ms |
97100 KB |
n=200000 |
124 |
Correct |
181 ms |
41500 KB |
n=25264 |