답안 #847036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
847036 2023-09-09T02:34:33 Z GrandTiger1729 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 38492 KB
#include <bits/stdc++.h>
using namespace std;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    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]--;
    while (q--)
    {
        int ty; cin >> ty;
        if (ty == 1)
        {
            int i, u; cin >> i >> u;
            i--, u--;
            a[i] = u;
        }
        else
        {
            int l, r, u; cin >> l >> r >> u;
            l--, u--;
            int ans = -1;
            for (int i = l; i < r; i++)
            {
                if (a[i] == u)
                    ans = i;
            }
            if (ans != -1)
            {
                cout << ans + 1 << ' ' << ans + 1 << '\n';
                continue;
            }
            for (int i = l; i < r - 1; i++)
            {
                if (lca(a[i], a[i + 1]) == u)
                    ans = i;
            }
            if (ans == -1)
                cout << "-1 -1\n";
            else
                cout << ans + 1 << ' ' << ans + 2 << '\n';
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 344 KB n=100
5 Correct 1 ms 344 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 1 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 1 ms 344 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 344 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 1 ms 600 KB n=12
27 Correct 1 ms 344 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 344 KB n=100
5 Correct 1 ms 344 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 1 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 1 ms 344 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 344 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 1 ms 600 KB n=12
27 Correct 1 ms 344 KB n=100
28 Correct 2 ms 344 KB n=500
29 Correct 1 ms 344 KB n=500
30 Correct 1 ms 348 KB n=500
31 Correct 1 ms 344 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 1 ms 344 KB n=500
34 Correct 1 ms 348 KB n=500
35 Correct 1 ms 344 KB n=500
36 Correct 4 ms 344 KB n=500
37 Correct 5 ms 344 KB n=500
38 Correct 4 ms 344 KB n=500
39 Correct 2 ms 344 KB n=500
40 Correct 2 ms 344 KB n=500
41 Correct 3 ms 348 KB n=500
42 Correct 2 ms 344 KB n=500
43 Correct 3 ms 344 KB n=500
44 Correct 3 ms 344 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 1 ms 348 KB n=500
47 Correct 1 ms 348 KB n=500
48 Correct 1 ms 512 KB n=500
49 Correct 1 ms 348 KB n=500
50 Correct 1 ms 348 KB n=500
51 Correct 1 ms 344 KB n=500
52 Correct 1 ms 348 KB n=500
53 Correct 1 ms 348 KB n=500
54 Correct 2 ms 344 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 2 ms 348 KB n=500
57 Correct 2 ms 348 KB n=500
58 Correct 2 ms 344 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 344 KB n=100
5 Correct 1 ms 344 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 1 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 1 ms 344 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 344 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 1 ms 600 KB n=12
27 Correct 1 ms 344 KB n=100
28 Correct 2 ms 344 KB n=500
29 Correct 1 ms 344 KB n=500
30 Correct 1 ms 348 KB n=500
31 Correct 1 ms 344 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 1 ms 344 KB n=500
34 Correct 1 ms 348 KB n=500
35 Correct 1 ms 344 KB n=500
36 Correct 4 ms 344 KB n=500
37 Correct 5 ms 344 KB n=500
38 Correct 4 ms 344 KB n=500
39 Correct 2 ms 344 KB n=500
40 Correct 2 ms 344 KB n=500
41 Correct 3 ms 348 KB n=500
42 Correct 2 ms 344 KB n=500
43 Correct 3 ms 344 KB n=500
44 Correct 3 ms 344 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 1 ms 348 KB n=500
47 Correct 1 ms 348 KB n=500
48 Correct 1 ms 512 KB n=500
49 Correct 1 ms 348 KB n=500
50 Correct 1 ms 348 KB n=500
51 Correct 1 ms 344 KB n=500
52 Correct 1 ms 348 KB n=500
53 Correct 1 ms 348 KB n=500
54 Correct 2 ms 344 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 2 ms 348 KB n=500
57 Correct 2 ms 348 KB n=500
58 Correct 2 ms 344 KB n=500
59 Correct 8 ms 720 KB n=2000
60 Correct 6 ms 964 KB n=2000
61 Correct 11 ms 860 KB n=2000
62 Correct 16 ms 724 KB n=2000
63 Correct 17 ms 604 KB n=2000
64 Correct 14 ms 1112 KB n=2000
65 Correct 18 ms 832 KB n=2000
66 Correct 9 ms 860 KB n=2000
67 Correct 19 ms 860 KB n=2000
68 Correct 11 ms 860 KB n=2000
69 Correct 125 ms 808 KB n=2000
70 Correct 117 ms 600 KB n=2000
71 Correct 109 ms 600 KB n=2000
72 Correct 33 ms 616 KB n=2000
73 Correct 28 ms 604 KB n=2000
74 Correct 8 ms 796 KB n=1844
75 Correct 25 ms 604 KB n=2000
76 Correct 58 ms 604 KB n=2000
77 Correct 58 ms 800 KB n=2000
78 Correct 64 ms 976 KB n=2000
79 Correct 2 ms 824 KB n=2000
80 Correct 8 ms 724 KB n=2000
81 Correct 11 ms 728 KB n=2000
82 Correct 2 ms 604 KB n=2000
83 Correct 8 ms 720 KB n=2000
84 Correct 10 ms 604 KB n=2000
85 Correct 22 ms 980 KB n=2000
86 Correct 21 ms 864 KB n=2000
87 Correct 13 ms 604 KB n=2000
88 Correct 97 ms 860 KB n=2000
89 Correct 100 ms 948 KB n=2000
90 Correct 76 ms 948 KB n=2000
91 Correct 55 ms 604 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 344 KB n=100
5 Correct 1 ms 344 KB n=100
6 Correct 1 ms 344 KB n=100
7 Correct 1 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 1 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 1 ms 344 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 344 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 348 KB n=100
26 Correct 1 ms 600 KB n=12
27 Correct 1 ms 344 KB n=100
28 Correct 2 ms 344 KB n=500
29 Correct 1 ms 344 KB n=500
30 Correct 1 ms 348 KB n=500
31 Correct 1 ms 344 KB n=500
32 Correct 1 ms 348 KB n=500
33 Correct 1 ms 344 KB n=500
34 Correct 1 ms 348 KB n=500
35 Correct 1 ms 344 KB n=500
36 Correct 4 ms 344 KB n=500
37 Correct 5 ms 344 KB n=500
38 Correct 4 ms 344 KB n=500
39 Correct 2 ms 344 KB n=500
40 Correct 2 ms 344 KB n=500
41 Correct 3 ms 348 KB n=500
42 Correct 2 ms 344 KB n=500
43 Correct 3 ms 344 KB n=500
44 Correct 3 ms 344 KB n=500
45 Correct 1 ms 348 KB n=500
46 Correct 1 ms 348 KB n=500
47 Correct 1 ms 348 KB n=500
48 Correct 1 ms 512 KB n=500
49 Correct 1 ms 348 KB n=500
50 Correct 1 ms 348 KB n=500
51 Correct 1 ms 344 KB n=500
52 Correct 1 ms 348 KB n=500
53 Correct 1 ms 348 KB n=500
54 Correct 2 ms 344 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 2 ms 348 KB n=500
57 Correct 2 ms 348 KB n=500
58 Correct 2 ms 344 KB n=500
59 Correct 8 ms 720 KB n=2000
60 Correct 6 ms 964 KB n=2000
61 Correct 11 ms 860 KB n=2000
62 Correct 16 ms 724 KB n=2000
63 Correct 17 ms 604 KB n=2000
64 Correct 14 ms 1112 KB n=2000
65 Correct 18 ms 832 KB n=2000
66 Correct 9 ms 860 KB n=2000
67 Correct 19 ms 860 KB n=2000
68 Correct 11 ms 860 KB n=2000
69 Correct 125 ms 808 KB n=2000
70 Correct 117 ms 600 KB n=2000
71 Correct 109 ms 600 KB n=2000
72 Correct 33 ms 616 KB n=2000
73 Correct 28 ms 604 KB n=2000
74 Correct 8 ms 796 KB n=1844
75 Correct 25 ms 604 KB n=2000
76 Correct 58 ms 604 KB n=2000
77 Correct 58 ms 800 KB n=2000
78 Correct 64 ms 976 KB n=2000
79 Correct 2 ms 824 KB n=2000
80 Correct 8 ms 724 KB n=2000
81 Correct 11 ms 728 KB n=2000
82 Correct 2 ms 604 KB n=2000
83 Correct 8 ms 720 KB n=2000
84 Correct 10 ms 604 KB n=2000
85 Correct 22 ms 980 KB n=2000
86 Correct 21 ms 864 KB n=2000
87 Correct 13 ms 604 KB n=2000
88 Correct 97 ms 860 KB n=2000
89 Correct 100 ms 948 KB n=2000
90 Correct 76 ms 948 KB n=2000
91 Correct 55 ms 604 KB n=2000
92 Execution timed out 4043 ms 38492 KB Time limit exceeded
93 Halted 0 ms 0 KB -