Submission #847042

# Submission time Handle Problem Language Result Execution time Memory
847042 2023-09-09T02:48:42 Z GrandTiger1729 Birthday gift (IZhO18_treearray) C++17
100 / 100
1076 ms 78944 KB
#include <bits/stdc++.h>
using namespace std;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n, m, q; cin >> n >> m >> q;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        int u, v; cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> in(n), out(n);
    vector<vector<int>> up(n, vector<int>(__lg(n) + 1));
    {
        int t = 0;
        vector<bool> vis(n);
        function<void(int)> dfs = [&](int u)
        {
            vis[u] = 1;
            in[u] = t++;
            for (int i =  1; i <= __lg(n); i++)
                up[u][i] = up[up[u][i - 1]][i - 1];
            for (auto &v : g[u])
            {
                if (vis[v])
                    continue;
                up[v][0] = u;
                dfs(v);
            }
            out[u] = t;
        };
        dfs(0);
    }
    auto anc = [&](int u, int v) -> bool
    {
        return in[u] <= in[v] && out[u] >= out[v];
    };
    auto lca = [&](int u, int v) -> int
    {
        if (anc(u, v))
            return u;
        for (int i = __lg(n); i >= 0; i--)
            if (!anc(up[u][i], v))
                u = up[u][i];
        return up[u][0];
    };
    vector<int> a(m);
    for (int i = 0; i < m; i++)
        cin >> a[i], a[i]--;
    vector<set<pair<int, int>>> mp(n);
    for (int i = 0; i < m; i++)
        mp[a[i]].emplace(i, i);
    for (int i = 0; i < m - 1; i++)
        mp[lca(a[i], a[i + 1])].emplace(i, i + 1);
    while (q--)
    {
        int ty; cin >> ty;
        if (ty == 1)
        {
            int i, u; cin >> i >> u;
            i--, u--;
            if (i > 0)
                mp[lca(a[i - 1], a[i])].erase(make_pair(i - 1, i));
            if (i < m - 1)
                mp[lca(a[i], a[i + 1])].erase(make_pair(i, i + 1));
            mp[a[i]].erase(make_pair(i, i));
            a[i] = u;
            if (i > 0)
                mp[lca(a[i - 1], a[i])].emplace(i - 1, i);
            if (i < m - 1)
                mp[lca(a[i], a[i + 1])].emplace(i, i + 1);
            mp[a[i]].emplace(i, i);
        }
        else
        {
            int l, r, u; cin >> l >> r >> u;
            l--, u--;
            auto it = mp[u].lower_bound(make_pair(l, -1));
            if (it != mp[u].end() && it->first < r && it->second < r)
                cout << it->first + 1 << ' ' << it->second + 1 << '\n';
            else
                cout << "-1 -1\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 0 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 0 ms 344 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 600 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 1 ms 544 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 0 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 0 ms 344 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 600 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 1 ms 544 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 344 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 2 ms 604 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 344 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 344 KB n=500
37 Correct 1 ms 348 KB n=500
38 Correct 1 ms 348 KB n=500
39 Correct 1 ms 348 KB n=500
40 Correct 1 ms 348 KB n=500
41 Correct 1 ms 348 KB n=500
42 Correct 1 ms 344 KB n=500
43 Correct 1 ms 344 KB n=500
44 Correct 1 ms 348 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 348 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 600 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 348 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 0 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 0 ms 344 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 600 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 1 ms 544 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 344 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 2 ms 604 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 344 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 344 KB n=500
37 Correct 1 ms 348 KB n=500
38 Correct 1 ms 348 KB n=500
39 Correct 1 ms 348 KB n=500
40 Correct 1 ms 348 KB n=500
41 Correct 1 ms 348 KB n=500
42 Correct 1 ms 344 KB n=500
43 Correct 1 ms 344 KB n=500
44 Correct 1 ms 348 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 348 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 600 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 348 KB n=500
59 Correct 3 ms 860 KB n=2000
60 Correct 3 ms 1116 KB n=2000
61 Correct 3 ms 1116 KB n=2000
62 Correct 3 ms 856 KB n=2000
63 Correct 2 ms 856 KB n=2000
64 Correct 3 ms 860 KB n=2000
65 Correct 3 ms 860 KB n=2000
66 Correct 3 ms 1116 KB n=2000
67 Correct 3 ms 856 KB n=2000
68 Correct 3 ms 1112 KB n=2000
69 Correct 2 ms 860 KB n=2000
70 Correct 2 ms 860 KB n=2000
71 Correct 2 ms 856 KB n=2000
72 Correct 2 ms 860 KB n=2000
73 Correct 2 ms 860 KB n=2000
74 Correct 2 ms 860 KB n=1844
75 Correct 2 ms 860 KB n=2000
76 Correct 3 ms 856 KB n=2000
77 Correct 3 ms 860 KB n=2000
78 Correct 3 ms 860 KB n=2000
79 Correct 3 ms 860 KB n=2000
80 Correct 3 ms 1112 KB n=2000
81 Correct 3 ms 1112 KB n=2000
82 Correct 4 ms 860 KB n=2000
83 Correct 3 ms 1116 KB n=2000
84 Correct 2 ms 860 KB n=2000
85 Correct 4 ms 856 KB n=2000
86 Correct 3 ms 856 KB n=2000
87 Correct 3 ms 860 KB n=2000
88 Correct 2 ms 1112 KB n=2000
89 Correct 3 ms 1116 KB n=2000
90 Correct 3 ms 1372 KB n=2000
91 Correct 2 ms 860 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 0 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 0 ms 344 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 600 KB n=100
14 Correct 0 ms 348 KB n=100
15 Correct 1 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 1 ms 544 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 348 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 348 KB n=100
28 Correct 1 ms 344 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 2 ms 604 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 344 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 344 KB n=500
37 Correct 1 ms 348 KB n=500
38 Correct 1 ms 348 KB n=500
39 Correct 1 ms 348 KB n=500
40 Correct 1 ms 348 KB n=500
41 Correct 1 ms 348 KB n=500
42 Correct 1 ms 344 KB n=500
43 Correct 1 ms 344 KB n=500
44 Correct 1 ms 348 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 348 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 600 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 348 KB n=500
59 Correct 3 ms 860 KB n=2000
60 Correct 3 ms 1116 KB n=2000
61 Correct 3 ms 1116 KB n=2000
62 Correct 3 ms 856 KB n=2000
63 Correct 2 ms 856 KB n=2000
64 Correct 3 ms 860 KB n=2000
65 Correct 3 ms 860 KB n=2000
66 Correct 3 ms 1116 KB n=2000
67 Correct 3 ms 856 KB n=2000
68 Correct 3 ms 1112 KB n=2000
69 Correct 2 ms 860 KB n=2000
70 Correct 2 ms 860 KB n=2000
71 Correct 2 ms 856 KB n=2000
72 Correct 2 ms 860 KB n=2000
73 Correct 2 ms 860 KB n=2000
74 Correct 2 ms 860 KB n=1844
75 Correct 2 ms 860 KB n=2000
76 Correct 3 ms 856 KB n=2000
77 Correct 3 ms 860 KB n=2000
78 Correct 3 ms 860 KB n=2000
79 Correct 3 ms 860 KB n=2000
80 Correct 3 ms 1112 KB n=2000
81 Correct 3 ms 1112 KB n=2000
82 Correct 4 ms 860 KB n=2000
83 Correct 3 ms 1116 KB n=2000
84 Correct 2 ms 860 KB n=2000
85 Correct 4 ms 856 KB n=2000
86 Correct 3 ms 856 KB n=2000
87 Correct 3 ms 860 KB n=2000
88 Correct 2 ms 1112 KB n=2000
89 Correct 3 ms 1116 KB n=2000
90 Correct 3 ms 1372 KB n=2000
91 Correct 2 ms 860 KB n=2000
92 Correct 601 ms 64080 KB n=200000
93 Correct 860 ms 71076 KB n=200000
94 Correct 836 ms 76628 KB n=200000
95 Correct 645 ms 64140 KB n=200000
96 Correct 647 ms 64220 KB n=200000
97 Correct 997 ms 69364 KB n=200000
98 Correct 665 ms 64196 KB n=200000
99 Correct 687 ms 63416 KB n=200000
100 Correct 601 ms 64140 KB n=200000
101 Correct 991 ms 78720 KB n=200000
102 Correct 314 ms 65104 KB n=200000
103 Correct 347 ms 65156 KB n=200000
104 Correct 328 ms 64952 KB n=200000
105 Correct 301 ms 64612 KB n=200000
106 Correct 349 ms 64700 KB n=200000
107 Correct 364 ms 64828 KB n=200000
108 Correct 688 ms 63828 KB n=200000
109 Correct 595 ms 63764 KB n=200000
110 Correct 595 ms 63568 KB n=200000
111 Correct 610 ms 64080 KB n=200000
112 Correct 849 ms 76976 KB n=200000
113 Correct 978 ms 69376 KB n=200000
114 Correct 574 ms 64236 KB n=200000
115 Correct 1076 ms 65796 KB n=200000
116 Correct 593 ms 63884 KB n=200000
117 Correct 909 ms 77692 KB n=200000
118 Correct 891 ms 67636 KB n=200000
119 Correct 617 ms 63876 KB n=200000
120 Correct 798 ms 78916 KB n=200000
121 Correct 825 ms 78416 KB n=200000
122 Correct 845 ms 78944 KB n=200000
123 Correct 328 ms 64472 KB n=200000
124 Correct 134 ms 16464 KB n=25264