Submission #445724

# Submission time Handle Problem Language Result Execution time Memory
445724 2021-07-19T11:59:58 Z zxcvbnm Birthday gift (IZhO18_treearray) C++14
100 / 100
2079 ms 85612 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxN = 2e5 + 5;
int L, timer = 0;
vector<int> adj[maxN], tin(maxN), tout(maxN);
int up[maxN][21];
void dfs(int v, int p) {
    tin[v] = timer++;
    up[v][0] = p;
    for(int i = 1; i <= L; i++) {
        up[v][i] = up[up[v][i-1]][i-1];
    }

    for(int u : adj[v]) {
        if (u != p) {
            dfs(u, v);
        }
    }
    tout[v] = timer++;
}

bool is_ancestor(int u, int v) {
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v) {
    if (is_ancestor(u, v)) {
        return u;
    }
    if (is_ancestor(v, u)) {
        return v;
    }

    for(int i = L; i >= 0; i--) {
        if (!is_ancestor(up[u][i], v)) {
            u = up[u][i];
        }
    }

    return up[u][0];
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("birthday_gift.txt", "r", stdin);
    int n, m, q;
    cin >> n >> m >> q;
    L = ceil(log2(n));
    for(int i = 0; i < n-1; i++) {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    vector<int> seq(m);
    multiset<array<int, 3>> can;
    auto rem = [&](int x, int l, int r) {
        can.erase({x, l, r});
    };
    auto add = [&](int x, int l, int r) {
        can.insert({x, l, r});
    };

    for(int i = 0; i < m; i++) {
        cin >> seq[i];
    }

    dfs(1, 1);

    for(int pos = 0; pos < m; pos++) {
        add(seq[pos], pos+1, pos+1);
        if (pos != 0) {
            add(lca(seq[pos], seq[pos-1]), pos-1+1, pos+1);
        }
        if (pos != m-1) {
            add(lca(seq[pos], seq[pos+1]), pos+1, pos+1+1);
        }
    }


    while(q--) {
        int type;
        cin >> type;
//        for(auto i : can) {
//            cout << i[0] << " " << i[1] << " " << i[2] << "\n";
//        }

        if (type == 1) {
            int pos, v;
            cin >> pos >> v;
            pos--;
            rem(seq[pos], pos+1, pos+1);
            if (pos != 0) {
                rem(lca(seq[pos], seq[pos-1]), pos-1+1, pos+1);
            }
            if (pos != m-1) {
                rem(lca(seq[pos], seq[pos+1]), pos+1, pos+1+1);
            }

            seq[pos] = v;
            add(seq[pos], pos+1, pos+1);
            if (pos != 0) {
                add(lca(seq[pos], seq[pos-1]), pos-1+1, pos+1);
            }
            if (pos != m-1) {
                add(lca(seq[pos], seq[pos+1]), pos+1, pos+1+1);
            }
        } else if (type == 2) {
            int l, r, v;
            cin >> l >> r >> v;
            auto it = can.lower_bound({v, l, l});
            if (it != can.end() && (*it)[0] == v && (*it)[1] >= l && (*it)[2] <= r) {
                if (lca(seq[(*it)[1]-1], seq[(*it)[2]-1]) != v) {
                    throw;
                }
                cout << (*it)[1] << " " << (*it)[2] << "\n";
            } else {
                cout << "-1 -1\n";
            }
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6476 KB n=5
2 Correct 4 ms 6604 KB n=100
3 Correct 4 ms 6604 KB n=100
4 Correct 4 ms 6600 KB n=100
5 Correct 5 ms 6528 KB n=100
6 Correct 4 ms 6604 KB n=100
7 Correct 4 ms 6600 KB n=100
8 Correct 4 ms 6604 KB n=100
9 Correct 4 ms 6604 KB n=100
10 Correct 4 ms 6604 KB n=100
11 Correct 4 ms 6604 KB n=100
12 Correct 4 ms 6604 KB n=100
13 Correct 5 ms 6604 KB n=100
14 Correct 4 ms 6604 KB n=100
15 Correct 4 ms 6604 KB n=100
16 Correct 4 ms 6600 KB n=100
17 Correct 4 ms 6604 KB n=100
18 Correct 4 ms 6604 KB n=100
19 Correct 5 ms 6524 KB n=100
20 Correct 4 ms 6604 KB n=100
21 Correct 4 ms 6604 KB n=100
22 Correct 4 ms 6604 KB n=100
23 Correct 4 ms 6604 KB n=100
24 Correct 4 ms 6600 KB n=100
25 Correct 4 ms 6604 KB n=100
26 Correct 4 ms 6604 KB n=12
27 Correct 4 ms 6604 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6476 KB n=5
2 Correct 4 ms 6604 KB n=100
3 Correct 4 ms 6604 KB n=100
4 Correct 4 ms 6600 KB n=100
5 Correct 5 ms 6528 KB n=100
6 Correct 4 ms 6604 KB n=100
7 Correct 4 ms 6600 KB n=100
8 Correct 4 ms 6604 KB n=100
9 Correct 4 ms 6604 KB n=100
10 Correct 4 ms 6604 KB n=100
11 Correct 4 ms 6604 KB n=100
12 Correct 4 ms 6604 KB n=100
13 Correct 5 ms 6604 KB n=100
14 Correct 4 ms 6604 KB n=100
15 Correct 4 ms 6604 KB n=100
16 Correct 4 ms 6600 KB n=100
17 Correct 4 ms 6604 KB n=100
18 Correct 4 ms 6604 KB n=100
19 Correct 5 ms 6524 KB n=100
20 Correct 4 ms 6604 KB n=100
21 Correct 4 ms 6604 KB n=100
22 Correct 4 ms 6604 KB n=100
23 Correct 4 ms 6604 KB n=100
24 Correct 4 ms 6600 KB n=100
25 Correct 4 ms 6604 KB n=100
26 Correct 4 ms 6604 KB n=12
27 Correct 4 ms 6604 KB n=100
28 Correct 5 ms 6732 KB n=500
29 Correct 5 ms 6732 KB n=500
30 Correct 5 ms 6740 KB n=500
31 Correct 5 ms 6732 KB n=500
32 Correct 5 ms 6732 KB n=500
33 Correct 5 ms 6732 KB n=500
34 Correct 5 ms 6732 KB n=500
35 Correct 5 ms 6732 KB n=500
36 Correct 5 ms 6732 KB n=500
37 Correct 5 ms 6668 KB n=500
38 Correct 5 ms 6732 KB n=500
39 Correct 5 ms 6740 KB n=500
40 Correct 6 ms 6748 KB n=500
41 Correct 5 ms 6732 KB n=500
42 Correct 5 ms 6732 KB n=500
43 Correct 5 ms 6736 KB n=500
44 Correct 5 ms 6732 KB n=500
45 Correct 5 ms 6736 KB n=500
46 Correct 5 ms 6732 KB n=500
47 Correct 6 ms 6740 KB n=500
48 Correct 5 ms 6744 KB n=500
49 Correct 5 ms 6732 KB n=500
50 Correct 5 ms 6732 KB n=500
51 Correct 5 ms 6732 KB n=500
52 Correct 5 ms 6732 KB n=500
53 Correct 5 ms 6748 KB n=500
54 Correct 5 ms 6740 KB n=500
55 Correct 5 ms 6732 KB n=278
56 Correct 5 ms 6732 KB n=500
57 Correct 5 ms 6732 KB n=500
58 Correct 5 ms 6732 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6476 KB n=5
2 Correct 4 ms 6604 KB n=100
3 Correct 4 ms 6604 KB n=100
4 Correct 4 ms 6600 KB n=100
5 Correct 5 ms 6528 KB n=100
6 Correct 4 ms 6604 KB n=100
7 Correct 4 ms 6600 KB n=100
8 Correct 4 ms 6604 KB n=100
9 Correct 4 ms 6604 KB n=100
10 Correct 4 ms 6604 KB n=100
11 Correct 4 ms 6604 KB n=100
12 Correct 4 ms 6604 KB n=100
13 Correct 5 ms 6604 KB n=100
14 Correct 4 ms 6604 KB n=100
15 Correct 4 ms 6604 KB n=100
16 Correct 4 ms 6600 KB n=100
17 Correct 4 ms 6604 KB n=100
18 Correct 4 ms 6604 KB n=100
19 Correct 5 ms 6524 KB n=100
20 Correct 4 ms 6604 KB n=100
21 Correct 4 ms 6604 KB n=100
22 Correct 4 ms 6604 KB n=100
23 Correct 4 ms 6604 KB n=100
24 Correct 4 ms 6600 KB n=100
25 Correct 4 ms 6604 KB n=100
26 Correct 4 ms 6604 KB n=12
27 Correct 4 ms 6604 KB n=100
28 Correct 5 ms 6732 KB n=500
29 Correct 5 ms 6732 KB n=500
30 Correct 5 ms 6740 KB n=500
31 Correct 5 ms 6732 KB n=500
32 Correct 5 ms 6732 KB n=500
33 Correct 5 ms 6732 KB n=500
34 Correct 5 ms 6732 KB n=500
35 Correct 5 ms 6732 KB n=500
36 Correct 5 ms 6732 KB n=500
37 Correct 5 ms 6668 KB n=500
38 Correct 5 ms 6732 KB n=500
39 Correct 5 ms 6740 KB n=500
40 Correct 6 ms 6748 KB n=500
41 Correct 5 ms 6732 KB n=500
42 Correct 5 ms 6732 KB n=500
43 Correct 5 ms 6736 KB n=500
44 Correct 5 ms 6732 KB n=500
45 Correct 5 ms 6736 KB n=500
46 Correct 5 ms 6732 KB n=500
47 Correct 6 ms 6740 KB n=500
48 Correct 5 ms 6744 KB n=500
49 Correct 5 ms 6732 KB n=500
50 Correct 5 ms 6732 KB n=500
51 Correct 5 ms 6732 KB n=500
52 Correct 5 ms 6732 KB n=500
53 Correct 5 ms 6748 KB n=500
54 Correct 5 ms 6740 KB n=500
55 Correct 5 ms 6732 KB n=278
56 Correct 5 ms 6732 KB n=500
57 Correct 5 ms 6732 KB n=500
58 Correct 5 ms 6732 KB n=500
59 Correct 9 ms 7272 KB n=2000
60 Correct 9 ms 7244 KB n=2000
61 Correct 9 ms 7252 KB n=2000
62 Correct 10 ms 7244 KB n=2000
63 Correct 9 ms 7276 KB n=2000
64 Correct 9 ms 7244 KB n=2000
65 Correct 9 ms 7256 KB n=2000
66 Correct 9 ms 7244 KB n=2000
67 Correct 10 ms 7244 KB n=2000
68 Correct 10 ms 7244 KB n=2000
69 Correct 9 ms 7244 KB n=2000
70 Correct 8 ms 7244 KB n=2000
71 Correct 8 ms 7244 KB n=2000
72 Correct 9 ms 7172 KB n=2000
73 Correct 8 ms 7244 KB n=2000
74 Correct 9 ms 7196 KB n=1844
75 Correct 7 ms 7252 KB n=2000
76 Correct 10 ms 7276 KB n=2000
77 Correct 10 ms 7276 KB n=2000
78 Correct 10 ms 7268 KB n=2000
79 Correct 11 ms 7260 KB n=2000
80 Correct 9 ms 7344 KB n=2000
81 Correct 9 ms 7212 KB n=2000
82 Correct 9 ms 7256 KB n=2000
83 Correct 9 ms 7332 KB n=2000
84 Correct 9 ms 7244 KB n=2000
85 Correct 9 ms 7252 KB n=2000
86 Correct 10 ms 7192 KB n=2000
87 Correct 10 ms 7276 KB n=2000
88 Correct 8 ms 7244 KB n=2000
89 Correct 10 ms 7244 KB n=2000
90 Correct 9 ms 7344 KB n=2000
91 Correct 8 ms 7244 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6476 KB n=5
2 Correct 4 ms 6604 KB n=100
3 Correct 4 ms 6604 KB n=100
4 Correct 4 ms 6600 KB n=100
5 Correct 5 ms 6528 KB n=100
6 Correct 4 ms 6604 KB n=100
7 Correct 4 ms 6600 KB n=100
8 Correct 4 ms 6604 KB n=100
9 Correct 4 ms 6604 KB n=100
10 Correct 4 ms 6604 KB n=100
11 Correct 4 ms 6604 KB n=100
12 Correct 4 ms 6604 KB n=100
13 Correct 5 ms 6604 KB n=100
14 Correct 4 ms 6604 KB n=100
15 Correct 4 ms 6604 KB n=100
16 Correct 4 ms 6600 KB n=100
17 Correct 4 ms 6604 KB n=100
18 Correct 4 ms 6604 KB n=100
19 Correct 5 ms 6524 KB n=100
20 Correct 4 ms 6604 KB n=100
21 Correct 4 ms 6604 KB n=100
22 Correct 4 ms 6604 KB n=100
23 Correct 4 ms 6604 KB n=100
24 Correct 4 ms 6600 KB n=100
25 Correct 4 ms 6604 KB n=100
26 Correct 4 ms 6604 KB n=12
27 Correct 4 ms 6604 KB n=100
28 Correct 5 ms 6732 KB n=500
29 Correct 5 ms 6732 KB n=500
30 Correct 5 ms 6740 KB n=500
31 Correct 5 ms 6732 KB n=500
32 Correct 5 ms 6732 KB n=500
33 Correct 5 ms 6732 KB n=500
34 Correct 5 ms 6732 KB n=500
35 Correct 5 ms 6732 KB n=500
36 Correct 5 ms 6732 KB n=500
37 Correct 5 ms 6668 KB n=500
38 Correct 5 ms 6732 KB n=500
39 Correct 5 ms 6740 KB n=500
40 Correct 6 ms 6748 KB n=500
41 Correct 5 ms 6732 KB n=500
42 Correct 5 ms 6732 KB n=500
43 Correct 5 ms 6736 KB n=500
44 Correct 5 ms 6732 KB n=500
45 Correct 5 ms 6736 KB n=500
46 Correct 5 ms 6732 KB n=500
47 Correct 6 ms 6740 KB n=500
48 Correct 5 ms 6744 KB n=500
49 Correct 5 ms 6732 KB n=500
50 Correct 5 ms 6732 KB n=500
51 Correct 5 ms 6732 KB n=500
52 Correct 5 ms 6732 KB n=500
53 Correct 5 ms 6748 KB n=500
54 Correct 5 ms 6740 KB n=500
55 Correct 5 ms 6732 KB n=278
56 Correct 5 ms 6732 KB n=500
57 Correct 5 ms 6732 KB n=500
58 Correct 5 ms 6732 KB n=500
59 Correct 9 ms 7272 KB n=2000
60 Correct 9 ms 7244 KB n=2000
61 Correct 9 ms 7252 KB n=2000
62 Correct 10 ms 7244 KB n=2000
63 Correct 9 ms 7276 KB n=2000
64 Correct 9 ms 7244 KB n=2000
65 Correct 9 ms 7256 KB n=2000
66 Correct 9 ms 7244 KB n=2000
67 Correct 10 ms 7244 KB n=2000
68 Correct 10 ms 7244 KB n=2000
69 Correct 9 ms 7244 KB n=2000
70 Correct 8 ms 7244 KB n=2000
71 Correct 8 ms 7244 KB n=2000
72 Correct 9 ms 7172 KB n=2000
73 Correct 8 ms 7244 KB n=2000
74 Correct 9 ms 7196 KB n=1844
75 Correct 7 ms 7252 KB n=2000
76 Correct 10 ms 7276 KB n=2000
77 Correct 10 ms 7276 KB n=2000
78 Correct 10 ms 7268 KB n=2000
79 Correct 11 ms 7260 KB n=2000
80 Correct 9 ms 7344 KB n=2000
81 Correct 9 ms 7212 KB n=2000
82 Correct 9 ms 7256 KB n=2000
83 Correct 9 ms 7332 KB n=2000
84 Correct 9 ms 7244 KB n=2000
85 Correct 9 ms 7252 KB n=2000
86 Correct 10 ms 7192 KB n=2000
87 Correct 10 ms 7276 KB n=2000
88 Correct 8 ms 7244 KB n=2000
89 Correct 10 ms 7244 KB n=2000
90 Correct 9 ms 7344 KB n=2000
91 Correct 8 ms 7244 KB n=2000
92 Correct 1460 ms 76248 KB n=200000
93 Correct 1669 ms 81092 KB n=200000
94 Correct 1389 ms 84308 KB n=200000
95 Correct 1418 ms 75892 KB n=200000
96 Correct 1380 ms 75928 KB n=200000
97 Correct 2015 ms 80144 KB n=200000
98 Correct 1434 ms 75964 KB n=200000
99 Correct 1646 ms 76072 KB n=200000
100 Correct 1446 ms 75908 KB n=200000
101 Correct 1224 ms 85612 KB n=200000
102 Correct 823 ms 77164 KB n=200000
103 Correct 845 ms 77232 KB n=200000
104 Correct 835 ms 77152 KB n=200000
105 Correct 824 ms 77464 KB n=200000
106 Correct 804 ms 77544 KB n=200000
107 Correct 874 ms 77500 KB n=200000
108 Correct 1445 ms 76004 KB n=200000
109 Correct 1473 ms 76228 KB n=200000
110 Correct 1436 ms 76136 KB n=200000
111 Correct 1333 ms 75324 KB n=200000
112 Correct 1295 ms 84608 KB n=200000
113 Correct 1758 ms 80056 KB n=200000
114 Correct 1322 ms 75448 KB n=200000
115 Correct 2079 ms 77852 KB n=200000
116 Correct 1395 ms 76108 KB n=200000
117 Correct 1215 ms 84940 KB n=200000
118 Correct 1828 ms 78868 KB n=200000
119 Correct 1328 ms 76144 KB n=200000
120 Correct 1086 ms 84676 KB n=200000
121 Correct 1099 ms 84652 KB n=200000
122 Correct 1101 ms 85084 KB n=200000
123 Correct 795 ms 77380 KB n=200000
124 Correct 361 ms 29892 KB n=25264