Submission #441629

# Submission time Handle Problem Language Result Execution time Memory
441629 2021-07-05T15:24:27 Z vishesh312 Birthday gift (IZhO18_treearray) C++17
100 / 100
1174 ms 77656 KB
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)(x).size()

using ll = long long;
const int mod = 1e9+7;

vector<vector<int>> adj;
vector<int> depth;
vector<array<int, 20>> up;

void dfs(int u, int p = -1) {
    for (int v : adj[u]) {
        if (v != p) {
            depth[v] = depth[u] + 1;
            up[v][0] = u;
            for (int l = 1; l < 20; ++l) {
                up[v][l] = up[up[v][l-1]][l-1];
            }
            dfs(v, u);
        }
    }
}

void solve(int tc) {
    int n, m, q;
    cin >> n >> m >> q;
    adj.resize(n); up.resize(n); depth.resize(n);
    for (int i = 0; i < n-1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(0);
    auto get_lca = [&] (int u, int v) {
        if (depth[u] < depth[v])
            swap(u, v);
        int k = depth[u] - depth[v];
        for (int l = 19; l >= 0; --l)
            if (k & (1<<l))
                u = up[u][l];
        if (u == v)
            return u;
        for (int l = 19; l >= 0; --l) {
            if (up[u][l] != up[v][l]) {
                u = up[u][l];
                v = up[v][l];
            }
        }
        return up[u][0];
    };
    vector<set<int>> lca(n);
    vector<set<int>> pos(n);
    vector<int> a(m);
    for (auto &x : a) {
        cin >> x;
        --x;
    }
    for (int i = 0; i < m; ++i)
        pos[a[i]].insert(i);
    for (int i = 0; i < m-1; ++i) {
        int u = get_lca(a[i], a[i+1]);
        lca[u].insert(i);
    }
    while (q--) {
        int x;
        cin >> x;
        if (x == 1) {
            int p, v;
            cin >> p >> v;
            --p, --v;
            pos[a[p]].erase(p);
            pos[v].insert(p);
            if (p) {
                lca[get_lca(a[p-1], a[p])].erase(p-1);
                lca[get_lca(a[p-1], v)].insert(p-1);
            }
            if (p < m-1) {
                lca[get_lca(a[p], a[p+1])].erase(p);
                lca[get_lca(v, a[p+1])].insert(p);
            }
            a[p] = v;
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            --l, --r, --v;
            auto it = pos[v].lower_bound(l);
            int p = *it;
            if (it != pos[v].end() and p <= r) {
                cout << p+1 << " " << p+1 << '\n';
                continue;
            }
            it = lca[v].lower_bound(l);
            p = *it;
            if (it != lca[v].end() and p < r) {
                cout << p+1 << " " << p+2 << '\n';
                continue;
            }
            cout << "-1 -1\n";
        }
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int tc = 1;
    //cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 280 KB n=100
5 Correct 1 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 332 KB n=100
11 Correct 1 ms 332 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 332 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 312 KB n=100
17 Correct 1 ms 332 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 1 ms 332 KB n=100
24 Correct 1 ms 332 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 280 KB n=100
5 Correct 1 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 332 KB n=100
11 Correct 1 ms 332 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 332 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 312 KB n=100
17 Correct 1 ms 332 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 1 ms 332 KB n=100
24 Correct 1 ms 332 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
28 Correct 1 ms 444 KB n=500
29 Correct 2 ms 460 KB n=500
30 Correct 2 ms 460 KB n=500
31 Correct 1 ms 460 KB n=500
32 Correct 1 ms 460 KB n=500
33 Correct 1 ms 460 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 1 ms 460 KB n=500
36 Correct 1 ms 460 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 1 ms 376 KB n=500
39 Correct 1 ms 460 KB n=500
40 Correct 1 ms 460 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 460 KB n=500
43 Correct 2 ms 460 KB n=500
44 Correct 2 ms 460 KB n=500
45 Correct 1 ms 448 KB n=500
46 Correct 1 ms 460 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 460 KB n=500
49 Correct 1 ms 460 KB n=500
50 Correct 1 ms 460 KB n=500
51 Correct 1 ms 460 KB n=500
52 Correct 1 ms 460 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 460 KB n=500
55 Correct 1 ms 332 KB n=278
56 Correct 1 ms 460 KB n=500
57 Correct 1 ms 460 KB n=500
58 Correct 1 ms 460 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 280 KB n=100
5 Correct 1 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 332 KB n=100
11 Correct 1 ms 332 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 332 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 312 KB n=100
17 Correct 1 ms 332 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 1 ms 332 KB n=100
24 Correct 1 ms 332 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
28 Correct 1 ms 444 KB n=500
29 Correct 2 ms 460 KB n=500
30 Correct 2 ms 460 KB n=500
31 Correct 1 ms 460 KB n=500
32 Correct 1 ms 460 KB n=500
33 Correct 1 ms 460 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 1 ms 460 KB n=500
36 Correct 1 ms 460 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 1 ms 376 KB n=500
39 Correct 1 ms 460 KB n=500
40 Correct 1 ms 460 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 460 KB n=500
43 Correct 2 ms 460 KB n=500
44 Correct 2 ms 460 KB n=500
45 Correct 1 ms 448 KB n=500
46 Correct 1 ms 460 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 460 KB n=500
49 Correct 1 ms 460 KB n=500
50 Correct 1 ms 460 KB n=500
51 Correct 1 ms 460 KB n=500
52 Correct 1 ms 460 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 460 KB n=500
55 Correct 1 ms 332 KB n=278
56 Correct 1 ms 460 KB n=500
57 Correct 1 ms 460 KB n=500
58 Correct 1 ms 460 KB n=500
59 Correct 4 ms 972 KB n=2000
60 Correct 4 ms 1100 KB n=2000
61 Correct 4 ms 972 KB n=2000
62 Correct 5 ms 972 KB n=2000
63 Correct 4 ms 972 KB n=2000
64 Correct 4 ms 972 KB n=2000
65 Correct 4 ms 964 KB n=2000
66 Correct 5 ms 1100 KB n=2000
67 Correct 5 ms 972 KB n=2000
68 Correct 4 ms 972 KB n=2000
69 Correct 4 ms 972 KB n=2000
70 Correct 4 ms 972 KB n=2000
71 Correct 4 ms 972 KB n=2000
72 Correct 3 ms 972 KB n=2000
73 Correct 5 ms 972 KB n=2000
74 Correct 4 ms 972 KB n=1844
75 Correct 3 ms 972 KB n=2000
76 Correct 5 ms 972 KB n=2000
77 Correct 5 ms 972 KB n=2000
78 Correct 5 ms 1008 KB n=2000
79 Correct 5 ms 972 KB n=2000
80 Correct 4 ms 1100 KB n=2000
81 Correct 4 ms 972 KB n=2000
82 Correct 4 ms 972 KB n=2000
83 Correct 5 ms 1100 KB n=2000
84 Correct 4 ms 972 KB n=2000
85 Correct 5 ms 972 KB n=2000
86 Correct 4 ms 972 KB n=2000
87 Correct 4 ms 972 KB n=2000
88 Correct 4 ms 1100 KB n=2000
89 Correct 4 ms 1100 KB n=2000
90 Correct 6 ms 1100 KB n=2000
91 Correct 3 ms 972 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB n=5
2 Correct 1 ms 332 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 280 KB n=100
5 Correct 1 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 332 KB n=100
11 Correct 1 ms 332 KB n=100
12 Correct 1 ms 332 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 332 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 312 KB n=100
17 Correct 1 ms 332 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 1 ms 332 KB n=100
24 Correct 1 ms 332 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 332 KB n=100
28 Correct 1 ms 444 KB n=500
29 Correct 2 ms 460 KB n=500
30 Correct 2 ms 460 KB n=500
31 Correct 1 ms 460 KB n=500
32 Correct 1 ms 460 KB n=500
33 Correct 1 ms 460 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 1 ms 460 KB n=500
36 Correct 1 ms 460 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 1 ms 376 KB n=500
39 Correct 1 ms 460 KB n=500
40 Correct 1 ms 460 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 460 KB n=500
43 Correct 2 ms 460 KB n=500
44 Correct 2 ms 460 KB n=500
45 Correct 1 ms 448 KB n=500
46 Correct 1 ms 460 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 460 KB n=500
49 Correct 1 ms 460 KB n=500
50 Correct 1 ms 460 KB n=500
51 Correct 1 ms 460 KB n=500
52 Correct 1 ms 460 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 460 KB n=500
55 Correct 1 ms 332 KB n=278
56 Correct 1 ms 460 KB n=500
57 Correct 1 ms 460 KB n=500
58 Correct 1 ms 460 KB n=500
59 Correct 4 ms 972 KB n=2000
60 Correct 4 ms 1100 KB n=2000
61 Correct 4 ms 972 KB n=2000
62 Correct 5 ms 972 KB n=2000
63 Correct 4 ms 972 KB n=2000
64 Correct 4 ms 972 KB n=2000
65 Correct 4 ms 964 KB n=2000
66 Correct 5 ms 1100 KB n=2000
67 Correct 5 ms 972 KB n=2000
68 Correct 4 ms 972 KB n=2000
69 Correct 4 ms 972 KB n=2000
70 Correct 4 ms 972 KB n=2000
71 Correct 4 ms 972 KB n=2000
72 Correct 3 ms 972 KB n=2000
73 Correct 5 ms 972 KB n=2000
74 Correct 4 ms 972 KB n=1844
75 Correct 3 ms 972 KB n=2000
76 Correct 5 ms 972 KB n=2000
77 Correct 5 ms 972 KB n=2000
78 Correct 5 ms 1008 KB n=2000
79 Correct 5 ms 972 KB n=2000
80 Correct 4 ms 1100 KB n=2000
81 Correct 4 ms 972 KB n=2000
82 Correct 4 ms 972 KB n=2000
83 Correct 5 ms 1100 KB n=2000
84 Correct 4 ms 972 KB n=2000
85 Correct 5 ms 972 KB n=2000
86 Correct 4 ms 972 KB n=2000
87 Correct 4 ms 972 KB n=2000
88 Correct 4 ms 1100 KB n=2000
89 Correct 4 ms 1100 KB n=2000
90 Correct 6 ms 1100 KB n=2000
91 Correct 3 ms 972 KB n=2000
92 Correct 785 ms 69064 KB n=200000
93 Correct 1057 ms 72828 KB n=200000
94 Correct 998 ms 76100 KB n=200000
95 Correct 732 ms 69108 KB n=200000
96 Correct 741 ms 69072 KB n=200000
97 Correct 1080 ms 72036 KB n=200000
98 Correct 742 ms 69308 KB n=200000
99 Correct 883 ms 68540 KB n=200000
100 Correct 719 ms 69156 KB n=200000
101 Correct 1070 ms 77440 KB n=200000
102 Correct 495 ms 69936 KB n=200000
103 Correct 508 ms 69936 KB n=200000
104 Correct 462 ms 69956 KB n=200000
105 Correct 475 ms 69648 KB n=200000
106 Correct 498 ms 69664 KB n=200000
107 Correct 495 ms 69644 KB n=200000
108 Correct 849 ms 68664 KB n=200000
109 Correct 869 ms 68720 KB n=200000
110 Correct 883 ms 68596 KB n=200000
111 Correct 750 ms 69076 KB n=200000
112 Correct 1036 ms 76644 KB n=200000
113 Correct 1174 ms 71948 KB n=200000
114 Correct 734 ms 69016 KB n=200000
115 Correct 1122 ms 69980 KB n=200000
116 Correct 694 ms 68748 KB n=200000
117 Correct 1000 ms 76836 KB n=200000
118 Correct 1082 ms 70760 KB n=200000
119 Correct 689 ms 68764 KB n=200000
120 Correct 992 ms 77212 KB n=200000
121 Correct 944 ms 77248 KB n=200000
122 Correct 952 ms 77656 KB n=200000
123 Correct 504 ms 69400 KB n=200000
124 Correct 231 ms 17476 KB n=25264