Submission #640095

# Submission time Handle Problem Language Result Execution time Memory
640095 2022-09-13T15:12:13 Z stevancv Birthday gift (IZhO18_treearray) C++14
100 / 100
852 ms 83252 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int mod = 1e9 + 7;
vector<int> g[N];
int nxt[N][18], in[N], out[N], tsz;
void Dfs(int s, int e) {
    in[s] = ++tsz;
    nxt[s][0] = e;
    for (int i = 1; i < 18; i++) {
        if (nxt[s][i - 1] != -1) nxt[s][i] = nxt[nxt[s][i - 1]][i - 1];
        else nxt[s][i] = -1;
    }
    for (int u : g[s]) {
        if (u != e) Dfs(u, s);
    }
    out[s] = tsz;
}
bool Ancestor(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}
int Lca(int u, int v) {
    if (Ancestor(u, v)) return u;
    if (Ancestor(v, u)) return v;
    for (int i = 17; i >= 0; i--) {
        if (nxt[u][i] != -1 && !Ancestor(nxt[u][i], v)) u = nxt[u][i];
    }
    return nxt[u][0];
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        u -= 1; v -= 1;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    Dfs(0, -1);
    vector<int> a(m), b(m - 1);
    for (int i = 0; i < m; i++) {
        cin >> a[i];
        a[i] -= 1;
    }
    for (int i = 0; i < m - 1; i++) b[i] = Lca(a[i], a[i + 1]);
    vector<set<int>> sa(n), sb(n);
    for (int i = 0; i < m; i++) sa[a[i]].insert(i);
    for (int i = 0; i < m - 1; i++) sb[b[i]].insert(i);
    while (q--) {
        int ty; cin >> ty;
        if (ty == 1) {
            int p, v; cin >> p >> v;
            p -= 1; v -= 1;
            sa[a[p]].erase(p);
            if (p > 0) sb[b[p - 1]].erase(p - 1);
            if (p < m - 1) sb[b[p]].erase(p);
            a[p] = v;
            sa[a[p]].insert(p);
            if (p > 0) {
                b[p - 1] = Lca(a[p - 1], a[p]);
                sb[b[p - 1]].insert(p - 1);
            }
            if (p < m - 1) {
                b[p] = Lca(a[p], a[p + 1]);
                sb[b[p]].insert(p);
            }
            continue;
        }
        int l, r, p; cin >> l >> r >> p;
        l -= 1; r -= 1; p -= 1;
        auto it = sa[p].lower_bound(l);
        if (it != sa[p].end() && (*it) <= r) {
            assert(0 <= *it && *it < m);
            cout << *it + 1 << sp << *it + 1 << en;
            continue;
        }
        it = sb[p].lower_bound(l);
        if (it != sb[p].end() && (*it) < r) {
            assert(0 <= *it && *it < m - 1);
            cout << *it + 1 << sp << *it + 2 << en;
            continue;
        }
        cout << -1 << sp << -1 << en;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5024 KB n=100
6 Correct 4 ms 4948 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 5024 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 4968 KB n=100
18 Correct 2 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 5020 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 4 ms 4948 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 4 ms 5016 KB n=12
27 Correct 3 ms 4948 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5024 KB n=100
6 Correct 4 ms 4948 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 5024 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 4968 KB n=100
18 Correct 2 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 5020 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 4 ms 4948 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 4 ms 5016 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 4 ms 5164 KB n=500
30 Correct 3 ms 5164 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5168 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 3 ms 5156 KB n=500
35 Correct 3 ms 5204 KB n=500
36 Correct 4 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 4 ms 5076 KB n=500
39 Correct 3 ms 5160 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 4 ms 5168 KB n=500
42 Correct 3 ms 5168 KB n=500
43 Correct 3 ms 5076 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 4 ms 5076 KB n=500
46 Correct 3 ms 5208 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5140 KB n=500
50 Correct 4 ms 5204 KB n=500
51 Correct 3 ms 5204 KB n=500
52 Correct 3 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 3 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5204 KB n=500
57 Correct 3 ms 5204 KB n=500
58 Correct 3 ms 5164 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5024 KB n=100
6 Correct 4 ms 4948 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 5024 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 4968 KB n=100
18 Correct 2 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 5020 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 4 ms 4948 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 4 ms 5016 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 4 ms 5164 KB n=500
30 Correct 3 ms 5164 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5168 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 3 ms 5156 KB n=500
35 Correct 3 ms 5204 KB n=500
36 Correct 4 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 4 ms 5076 KB n=500
39 Correct 3 ms 5160 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 4 ms 5168 KB n=500
42 Correct 3 ms 5168 KB n=500
43 Correct 3 ms 5076 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 4 ms 5076 KB n=500
46 Correct 3 ms 5208 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5140 KB n=500
50 Correct 4 ms 5204 KB n=500
51 Correct 3 ms 5204 KB n=500
52 Correct 3 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 3 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5204 KB n=500
57 Correct 3 ms 5204 KB n=500
58 Correct 3 ms 5164 KB n=500
59 Correct 5 ms 5628 KB n=2000
60 Correct 5 ms 5716 KB n=2000
61 Correct 5 ms 5716 KB n=2000
62 Correct 5 ms 5716 KB n=2000
63 Correct 5 ms 5676 KB n=2000
64 Correct 7 ms 5636 KB n=2000
65 Correct 6 ms 5716 KB n=2000
66 Correct 5 ms 5716 KB n=2000
67 Correct 6 ms 5716 KB n=2000
68 Correct 6 ms 5716 KB n=2000
69 Correct 5 ms 5728 KB n=2000
70 Correct 5 ms 5724 KB n=2000
71 Correct 4 ms 5680 KB n=2000
72 Correct 4 ms 5720 KB n=2000
73 Correct 5 ms 5728 KB n=2000
74 Correct 5 ms 5796 KB n=1844
75 Correct 7 ms 5684 KB n=2000
76 Correct 6 ms 5724 KB n=2000
77 Correct 5 ms 5676 KB n=2000
78 Correct 6 ms 5588 KB n=2000
79 Correct 5 ms 5720 KB n=2000
80 Correct 5 ms 5728 KB n=2000
81 Correct 5 ms 5716 KB n=2000
82 Correct 5 ms 5588 KB n=2000
83 Correct 6 ms 5760 KB n=2000
84 Correct 6 ms 5716 KB n=2000
85 Correct 5 ms 5676 KB n=2000
86 Correct 5 ms 5628 KB n=2000
87 Correct 5 ms 5716 KB n=2000
88 Correct 5 ms 5676 KB n=2000
89 Correct 5 ms 5716 KB n=2000
90 Correct 5 ms 5672 KB n=2000
91 Correct 6 ms 5716 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 5024 KB n=100
5 Correct 3 ms 5024 KB n=100
6 Correct 4 ms 4948 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5024 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 5024 KB n=100
12 Correct 2 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4948 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 4968 KB n=100
18 Correct 2 ms 4948 KB n=100
19 Correct 3 ms 4948 KB n=100
20 Correct 3 ms 5020 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 3 ms 4948 KB n=100
23 Correct 4 ms 4948 KB n=100
24 Correct 3 ms 4948 KB n=100
25 Correct 3 ms 4948 KB n=100
26 Correct 4 ms 5016 KB n=12
27 Correct 3 ms 4948 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 4 ms 5164 KB n=500
30 Correct 3 ms 5164 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5168 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 3 ms 5156 KB n=500
35 Correct 3 ms 5204 KB n=500
36 Correct 4 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 4 ms 5076 KB n=500
39 Correct 3 ms 5160 KB n=500
40 Correct 3 ms 5076 KB n=500
41 Correct 4 ms 5168 KB n=500
42 Correct 3 ms 5168 KB n=500
43 Correct 3 ms 5076 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 4 ms 5076 KB n=500
46 Correct 3 ms 5208 KB n=500
47 Correct 3 ms 5204 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 3 ms 5140 KB n=500
50 Correct 4 ms 5204 KB n=500
51 Correct 3 ms 5204 KB n=500
52 Correct 3 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 3 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 3 ms 5204 KB n=500
57 Correct 3 ms 5204 KB n=500
58 Correct 3 ms 5164 KB n=500
59 Correct 5 ms 5628 KB n=2000
60 Correct 5 ms 5716 KB n=2000
61 Correct 5 ms 5716 KB n=2000
62 Correct 5 ms 5716 KB n=2000
63 Correct 5 ms 5676 KB n=2000
64 Correct 7 ms 5636 KB n=2000
65 Correct 6 ms 5716 KB n=2000
66 Correct 5 ms 5716 KB n=2000
67 Correct 6 ms 5716 KB n=2000
68 Correct 6 ms 5716 KB n=2000
69 Correct 5 ms 5728 KB n=2000
70 Correct 5 ms 5724 KB n=2000
71 Correct 4 ms 5680 KB n=2000
72 Correct 4 ms 5720 KB n=2000
73 Correct 5 ms 5728 KB n=2000
74 Correct 5 ms 5796 KB n=1844
75 Correct 7 ms 5684 KB n=2000
76 Correct 6 ms 5724 KB n=2000
77 Correct 5 ms 5676 KB n=2000
78 Correct 6 ms 5588 KB n=2000
79 Correct 5 ms 5720 KB n=2000
80 Correct 5 ms 5728 KB n=2000
81 Correct 5 ms 5716 KB n=2000
82 Correct 5 ms 5588 KB n=2000
83 Correct 6 ms 5760 KB n=2000
84 Correct 6 ms 5716 KB n=2000
85 Correct 5 ms 5676 KB n=2000
86 Correct 5 ms 5628 KB n=2000
87 Correct 5 ms 5716 KB n=2000
88 Correct 5 ms 5676 KB n=2000
89 Correct 5 ms 5716 KB n=2000
90 Correct 5 ms 5672 KB n=2000
91 Correct 6 ms 5716 KB n=2000
92 Correct 579 ms 74144 KB n=200000
93 Correct 622 ms 77964 KB n=200000
94 Correct 518 ms 81308 KB n=200000
95 Correct 551 ms 73964 KB n=200000
96 Correct 517 ms 74032 KB n=200000
97 Correct 666 ms 77032 KB n=200000
98 Correct 557 ms 74080 KB n=200000
99 Correct 620 ms 73596 KB n=200000
100 Correct 552 ms 74108 KB n=200000
101 Correct 483 ms 82644 KB n=200000
102 Correct 399 ms 74988 KB n=200000
103 Correct 349 ms 75076 KB n=200000
104 Correct 340 ms 75036 KB n=200000
105 Correct 368 ms 74956 KB n=200000
106 Correct 343 ms 74832 KB n=200000
107 Correct 338 ms 74808 KB n=200000
108 Correct 624 ms 73796 KB n=200000
109 Correct 628 ms 73768 KB n=200000
110 Correct 619 ms 73780 KB n=200000
111 Correct 562 ms 73948 KB n=200000
112 Correct 494 ms 81504 KB n=200000
113 Correct 657 ms 77024 KB n=200000
114 Correct 536 ms 73832 KB n=200000
115 Correct 765 ms 74984 KB n=200000
116 Correct 561 ms 73796 KB n=200000
117 Correct 535 ms 82192 KB n=200000
118 Correct 852 ms 76528 KB n=200000
119 Correct 609 ms 74416 KB n=200000
120 Correct 652 ms 82756 KB n=200000
121 Correct 559 ms 83024 KB n=200000
122 Correct 505 ms 83252 KB n=200000
123 Correct 400 ms 76004 KB n=200000
124 Correct 134 ms 22476 KB n=25264