Submission #894060

# Submission time Handle Problem Language Result Execution time Memory
894060 2023-12-27T23:04:37 Z Pekiban Birthday gift (IZhO18_treearray) C++17
56 / 100
448 ms 262144 KB
#include <bits/stdc++.h>

using namespace std;

const int mxN = 2e5+2, LOG = 18;
vector<int> g[mxN];
multiset<int> st[4*mxN];
int up[LOG][mxN], dep[mxN], _m;
void build(const vector<int> &v, int i = 1, int l2 = 0, int r2 = _m - 1) {
    if (l2 == r2) {
        st[i].insert(v[l2]);
        return;
    }
    int m2 = (l2 + r2)/2;
    build(v, 2*i, l2, m2);
    build(v, 2*i+1, m2+1, r2);
    for (auto u : st[2*i]) {
        st[i].insert(u);
    }
    for (auto u : st[2*i+1]) {
        st[i].insert(u);
    }
}
void update(int l, int x, int y, int i = 1, int l2 = 0, int r2 = _m - 1) {
    st[i].erase(st[i].find(y));
    st[i].insert(x);
    if (l2 == r2)   return;
    int m2 = (l2 + r2)/2;
    if (l <= m2) {
        update(l, x, y, 2*i, l2, m2);
    }
    else {
        update(l, x, y, 2*i+1, m2+1, r2);
    }
}
int query(int l, int r, int u, int i = 1, int l2 = 0, int r2 = _m - 1) {
    if (l <= l2 && r2 <= r) {
        if (st[i].find(u) != st[i].end()) {
            while (l2 != r2) {
                int m2 = (l2 + r2)/2;
                if (st[2*i].find(u) != st[2*i].end()) {
                    r2 = m2;
                    i = 2*i;
                }
                else {
                    l2 = m2+1;
                    i = 2*i+1;
                }
            }
            return l2;
        }
        return -1;
    }
    int m2 = (l2 + r2)/2;
    if (l <= m2) {
        int x = query(l, r, u, 2*i, l2, m2);
        if (x != -1)    return x;
    }
    if (m2 + 1 <= r) {
        int x = query(l, r, u, 2*i+1, m2+1, r2);
        if (x != -1)    return x;
    }
    return -1;
}
void dfs(int s, int e) {
    for (auto u : g[s]) {
        if (u == e) continue;
        up[0][u] = s;
        dep[u] = dep[s] + 1;
        dfs(u, s);
    }
}
int jump(int s, int k) {
    for (int i = 0; i < LOG; ++i) {
        if ((1 << i) & k) {
            s = up[i][s];
        }
    }
    return s;
}
int lca(int u, int v) {
    if (dep[u] < dep[v])    swap(u, v);
    u = jump(u, dep[u] - dep[v]);
    if (u == v) return u;
    for (int i = LOG-1; i >= 0; --i) {
        if (up[i][u] != up[i][v]) {
            u = up[i][u], v = up[i][v];
        }
    }
    return up[0][u];
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q; _m = m-1;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v), g[v].push_back(u);
    }
    dfs(0, -1);
    for (int i = 1; i < LOG; ++i) {
        for (int j = 0; j < n; ++j) {
            up[i][j] = up[i-1][up[i-1][j]];
        }
    }
    int a[m];
    multiset<int> ms[n];
    vector<int> val(m);
    for (int i = 0; i < n; ++i) ms[i].insert(1e9);
    for (int i = 0; i < m; ++i) {
        cin >> a[i];
        a[i]--;
        if (i) {
            val[i-1] = lca(a[i-1], a[i]);
        }
        ms[a[i]].insert(i);
    }
    build(val);
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int l, u;
            cin >> l >> u;
            l--, u--;
            if (l != m-1) {
                int x = lca(u, a[l+1]);
                update(l, x, val[l]);
                val[l] = x;
            }
            if (l) {
                int x = lca(u, a[l-1]);
                update(l-1, x, val[l-1]);
                val[l-1] = x;
            }
            ms[a[l]].erase(ms[a[l]].find(l));
            ms[u].insert(l);
            a[l] = u;
        }
        if (t == 2) {
            int l, r, v;
            cin >> l >> r >> v;
            l--, r--, v--;
            if (l == r) {
                if (a[l] == v) {
                    cout << l+1 << ' ' << l+1 << '\n';
                }
                else {
                    cout << -1 << ' ' << -1 << '\n';
                }
                continue;
              }
            if (*ms[v].lower_bound(l) <= r) {
                cout << *ms[v].lower_bound(l)+1 << ' ' << *ms[v].lower_bound(l)+1 << '\n';
            }
            else {
                int x = query(l, r-1, v);
                if (x != -1) {
                    cout << x+1 << ' ' << x+2 << '\n';
                }
                else {
                    cout << -1 << ' ' << -1 << '\n';
                }
            }
        }
    }
    
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 57176 KB n=5
2 Correct 11 ms 57436 KB n=100
3 Correct 12 ms 57432 KB n=100
4 Correct 10 ms 57436 KB n=100
5 Correct 11 ms 57436 KB n=100
6 Correct 11 ms 57436 KB n=100
7 Correct 11 ms 57436 KB n=100
8 Correct 11 ms 57432 KB n=100
9 Correct 11 ms 57924 KB n=100
10 Correct 11 ms 57436 KB n=100
11 Correct 11 ms 57432 KB n=100
12 Correct 11 ms 57436 KB n=100
13 Correct 11 ms 57436 KB n=100
14 Correct 11 ms 57436 KB n=100
15 Correct 12 ms 57436 KB n=100
16 Correct 11 ms 57432 KB n=100
17 Correct 11 ms 57436 KB n=100
18 Correct 11 ms 57436 KB n=100
19 Correct 11 ms 57476 KB n=100
20 Correct 10 ms 57436 KB n=100
21 Correct 11 ms 57436 KB n=100
22 Correct 11 ms 57472 KB n=100
23 Correct 11 ms 57436 KB n=100
24 Correct 11 ms 57476 KB n=100
25 Correct 11 ms 57436 KB n=100
26 Correct 10 ms 57436 KB n=12
27 Correct 10 ms 57436 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 11 ms 57176 KB n=5
2 Correct 11 ms 57436 KB n=100
3 Correct 12 ms 57432 KB n=100
4 Correct 10 ms 57436 KB n=100
5 Correct 11 ms 57436 KB n=100
6 Correct 11 ms 57436 KB n=100
7 Correct 11 ms 57436 KB n=100
8 Correct 11 ms 57432 KB n=100
9 Correct 11 ms 57924 KB n=100
10 Correct 11 ms 57436 KB n=100
11 Correct 11 ms 57432 KB n=100
12 Correct 11 ms 57436 KB n=100
13 Correct 11 ms 57436 KB n=100
14 Correct 11 ms 57436 KB n=100
15 Correct 12 ms 57436 KB n=100
16 Correct 11 ms 57432 KB n=100
17 Correct 11 ms 57436 KB n=100
18 Correct 11 ms 57436 KB n=100
19 Correct 11 ms 57476 KB n=100
20 Correct 10 ms 57436 KB n=100
21 Correct 11 ms 57436 KB n=100
22 Correct 11 ms 57472 KB n=100
23 Correct 11 ms 57436 KB n=100
24 Correct 11 ms 57476 KB n=100
25 Correct 11 ms 57436 KB n=100
26 Correct 10 ms 57436 KB n=12
27 Correct 10 ms 57436 KB n=100
28 Correct 12 ms 57688 KB n=500
29 Correct 12 ms 57692 KB n=500
30 Correct 12 ms 57748 KB n=500
31 Correct 12 ms 57692 KB n=500
32 Correct 15 ms 57692 KB n=500
33 Correct 12 ms 57692 KB n=500
34 Correct 11 ms 57692 KB n=500
35 Correct 12 ms 57692 KB n=500
36 Correct 11 ms 57760 KB n=500
37 Correct 12 ms 57688 KB n=500
38 Correct 12 ms 57692 KB n=500
39 Correct 13 ms 57688 KB n=500
40 Correct 12 ms 57692 KB n=500
41 Correct 11 ms 57688 KB n=500
42 Correct 12 ms 57692 KB n=500
43 Correct 12 ms 57692 KB n=500
44 Correct 12 ms 57620 KB n=500
45 Correct 12 ms 57688 KB n=500
46 Correct 12 ms 57756 KB n=500
47 Correct 13 ms 57688 KB n=500
48 Correct 12 ms 57688 KB n=500
49 Correct 13 ms 57692 KB n=500
50 Correct 11 ms 57692 KB n=500
51 Correct 12 ms 57692 KB n=500
52 Correct 12 ms 57692 KB n=500
53 Correct 13 ms 57692 KB n=500
54 Correct 12 ms 57692 KB n=500
55 Correct 11 ms 57692 KB n=278
56 Correct 12 ms 57692 KB n=500
57 Correct 13 ms 57692 KB n=500
58 Correct 11 ms 57760 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 11 ms 57176 KB n=5
2 Correct 11 ms 57436 KB n=100
3 Correct 12 ms 57432 KB n=100
4 Correct 10 ms 57436 KB n=100
5 Correct 11 ms 57436 KB n=100
6 Correct 11 ms 57436 KB n=100
7 Correct 11 ms 57436 KB n=100
8 Correct 11 ms 57432 KB n=100
9 Correct 11 ms 57924 KB n=100
10 Correct 11 ms 57436 KB n=100
11 Correct 11 ms 57432 KB n=100
12 Correct 11 ms 57436 KB n=100
13 Correct 11 ms 57436 KB n=100
14 Correct 11 ms 57436 KB n=100
15 Correct 12 ms 57436 KB n=100
16 Correct 11 ms 57432 KB n=100
17 Correct 11 ms 57436 KB n=100
18 Correct 11 ms 57436 KB n=100
19 Correct 11 ms 57476 KB n=100
20 Correct 10 ms 57436 KB n=100
21 Correct 11 ms 57436 KB n=100
22 Correct 11 ms 57472 KB n=100
23 Correct 11 ms 57436 KB n=100
24 Correct 11 ms 57476 KB n=100
25 Correct 11 ms 57436 KB n=100
26 Correct 10 ms 57436 KB n=12
27 Correct 10 ms 57436 KB n=100
28 Correct 12 ms 57688 KB n=500
29 Correct 12 ms 57692 KB n=500
30 Correct 12 ms 57748 KB n=500
31 Correct 12 ms 57692 KB n=500
32 Correct 15 ms 57692 KB n=500
33 Correct 12 ms 57692 KB n=500
34 Correct 11 ms 57692 KB n=500
35 Correct 12 ms 57692 KB n=500
36 Correct 11 ms 57760 KB n=500
37 Correct 12 ms 57688 KB n=500
38 Correct 12 ms 57692 KB n=500
39 Correct 13 ms 57688 KB n=500
40 Correct 12 ms 57692 KB n=500
41 Correct 11 ms 57688 KB n=500
42 Correct 12 ms 57692 KB n=500
43 Correct 12 ms 57692 KB n=500
44 Correct 12 ms 57620 KB n=500
45 Correct 12 ms 57688 KB n=500
46 Correct 12 ms 57756 KB n=500
47 Correct 13 ms 57688 KB n=500
48 Correct 12 ms 57688 KB n=500
49 Correct 13 ms 57692 KB n=500
50 Correct 11 ms 57692 KB n=500
51 Correct 12 ms 57692 KB n=500
52 Correct 12 ms 57692 KB n=500
53 Correct 13 ms 57692 KB n=500
54 Correct 12 ms 57692 KB n=500
55 Correct 11 ms 57692 KB n=278
56 Correct 12 ms 57692 KB n=500
57 Correct 13 ms 57692 KB n=500
58 Correct 11 ms 57760 KB n=500
59 Correct 16 ms 58716 KB n=2000
60 Correct 18 ms 58944 KB n=2000
61 Correct 18 ms 58792 KB n=2000
62 Correct 18 ms 58716 KB n=2000
63 Correct 17 ms 58712 KB n=2000
64 Correct 21 ms 58716 KB n=2000
65 Correct 17 ms 58928 KB n=2000
66 Correct 20 ms 58716 KB n=2000
67 Correct 19 ms 58712 KB n=2000
68 Correct 18 ms 58912 KB n=2000
69 Correct 14 ms 58716 KB n=2000
70 Correct 15 ms 58716 KB n=2000
71 Correct 14 ms 58716 KB n=2000
72 Correct 15 ms 58716 KB n=2000
73 Correct 15 ms 58716 KB n=2000
74 Correct 16 ms 58716 KB n=1844
75 Correct 14 ms 58712 KB n=2000
76 Correct 18 ms 58716 KB n=2000
77 Correct 18 ms 58712 KB n=2000
78 Correct 17 ms 58716 KB n=2000
79 Correct 18 ms 59072 KB n=2000
80 Correct 20 ms 58712 KB n=2000
81 Correct 19 ms 58716 KB n=2000
82 Correct 17 ms 58936 KB n=2000
83 Correct 18 ms 58712 KB n=2000
84 Correct 17 ms 58800 KB n=2000
85 Correct 18 ms 58716 KB n=2000
86 Correct 19 ms 58712 KB n=2000
87 Correct 17 ms 58716 KB n=2000
88 Correct 18 ms 58716 KB n=2000
89 Correct 19 ms 58716 KB n=2000
90 Correct 18 ms 58716 KB n=2000
91 Correct 15 ms 58712 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 11 ms 57176 KB n=5
2 Correct 11 ms 57436 KB n=100
3 Correct 12 ms 57432 KB n=100
4 Correct 10 ms 57436 KB n=100
5 Correct 11 ms 57436 KB n=100
6 Correct 11 ms 57436 KB n=100
7 Correct 11 ms 57436 KB n=100
8 Correct 11 ms 57432 KB n=100
9 Correct 11 ms 57924 KB n=100
10 Correct 11 ms 57436 KB n=100
11 Correct 11 ms 57432 KB n=100
12 Correct 11 ms 57436 KB n=100
13 Correct 11 ms 57436 KB n=100
14 Correct 11 ms 57436 KB n=100
15 Correct 12 ms 57436 KB n=100
16 Correct 11 ms 57432 KB n=100
17 Correct 11 ms 57436 KB n=100
18 Correct 11 ms 57436 KB n=100
19 Correct 11 ms 57476 KB n=100
20 Correct 10 ms 57436 KB n=100
21 Correct 11 ms 57436 KB n=100
22 Correct 11 ms 57472 KB n=100
23 Correct 11 ms 57436 KB n=100
24 Correct 11 ms 57476 KB n=100
25 Correct 11 ms 57436 KB n=100
26 Correct 10 ms 57436 KB n=12
27 Correct 10 ms 57436 KB n=100
28 Correct 12 ms 57688 KB n=500
29 Correct 12 ms 57692 KB n=500
30 Correct 12 ms 57748 KB n=500
31 Correct 12 ms 57692 KB n=500
32 Correct 15 ms 57692 KB n=500
33 Correct 12 ms 57692 KB n=500
34 Correct 11 ms 57692 KB n=500
35 Correct 12 ms 57692 KB n=500
36 Correct 11 ms 57760 KB n=500
37 Correct 12 ms 57688 KB n=500
38 Correct 12 ms 57692 KB n=500
39 Correct 13 ms 57688 KB n=500
40 Correct 12 ms 57692 KB n=500
41 Correct 11 ms 57688 KB n=500
42 Correct 12 ms 57692 KB n=500
43 Correct 12 ms 57692 KB n=500
44 Correct 12 ms 57620 KB n=500
45 Correct 12 ms 57688 KB n=500
46 Correct 12 ms 57756 KB n=500
47 Correct 13 ms 57688 KB n=500
48 Correct 12 ms 57688 KB n=500
49 Correct 13 ms 57692 KB n=500
50 Correct 11 ms 57692 KB n=500
51 Correct 12 ms 57692 KB n=500
52 Correct 12 ms 57692 KB n=500
53 Correct 13 ms 57692 KB n=500
54 Correct 12 ms 57692 KB n=500
55 Correct 11 ms 57692 KB n=278
56 Correct 12 ms 57692 KB n=500
57 Correct 13 ms 57692 KB n=500
58 Correct 11 ms 57760 KB n=500
59 Correct 16 ms 58716 KB n=2000
60 Correct 18 ms 58944 KB n=2000
61 Correct 18 ms 58792 KB n=2000
62 Correct 18 ms 58716 KB n=2000
63 Correct 17 ms 58712 KB n=2000
64 Correct 21 ms 58716 KB n=2000
65 Correct 17 ms 58928 KB n=2000
66 Correct 20 ms 58716 KB n=2000
67 Correct 19 ms 58712 KB n=2000
68 Correct 18 ms 58912 KB n=2000
69 Correct 14 ms 58716 KB n=2000
70 Correct 15 ms 58716 KB n=2000
71 Correct 14 ms 58716 KB n=2000
72 Correct 15 ms 58716 KB n=2000
73 Correct 15 ms 58716 KB n=2000
74 Correct 16 ms 58716 KB n=1844
75 Correct 14 ms 58712 KB n=2000
76 Correct 18 ms 58716 KB n=2000
77 Correct 18 ms 58712 KB n=2000
78 Correct 17 ms 58716 KB n=2000
79 Correct 18 ms 59072 KB n=2000
80 Correct 20 ms 58712 KB n=2000
81 Correct 19 ms 58716 KB n=2000
82 Correct 17 ms 58936 KB n=2000
83 Correct 18 ms 58712 KB n=2000
84 Correct 17 ms 58800 KB n=2000
85 Correct 18 ms 58716 KB n=2000
86 Correct 19 ms 58712 KB n=2000
87 Correct 17 ms 58716 KB n=2000
88 Correct 18 ms 58716 KB n=2000
89 Correct 19 ms 58716 KB n=2000
90 Correct 18 ms 58716 KB n=2000
91 Correct 15 ms 58712 KB n=2000
92 Runtime error 448 ms 262144 KB Execution killed with signal 9
93 Halted 0 ms 0 KB -