Submission #1097144

# Submission time Handle Problem Language Result Execution time Memory
1097144 2024-10-06T09:27:10 Z lmToT27 Birthday gift (IZhO18_treearray) C++17
100 / 100
651 ms 101456 KB
#include <bits/stdc++.h>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long

using namespace std;
namespace std {
        template <typename T, int D>
        struct _vector : public vector <_vector <T, D - 1>> {
                static_assert(D >= 1, "Dimension must be positive!");
                template <typename... Args>
                _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
        };
        // _vector <int, 3> a(n, m, k);: int a[n][m][k].
        // _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
        template <typename T>
        struct _vector <T, 1> : public vector <T> {
                _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
        };
}

const int MAX = 2e5 + 3;
const ll MOD[] = {1000000007, 998244353};

int n, m, q, a[MAX];
vector <int> adj[MAX];
int depth[MAX], pa[MAX][18];
set <int> f[MAX], g[MAX];

void dfs(int u, int pre) {
        depth[u] = depth[pre] + 1;
        pa[u][0] = pre;
        for (int i = 1; i <= 17; i++) pa[u][i] = pa[pa[u][i - 1]][i - 1];
        for (int &v : adj[u]) if (v != pre) dfs(v, u);
}

int __lca(int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        int dist = depth[u] - depth[v];

        while (dist) {
                u = pa[u][__builtin_ctz(dist)];
                dist -= dist & -dist;
        }
        
        if (u == v) {
                return u;
        } else {
                for (int i = 17; i >= 0; i--) {
                        if (pa[u][i] != pa[v][i]) {
                                u = pa[u][i];
                                v = pa[v][i];
                        }
                }
                return pa[u][0];
        }
}

void query(int l, int r, int lca) {
        auto p = f[lca].lower_bound(l);
        if (*p <= r) return void(cout << *p << ' ' << *p << '\n');
        auto q = g[lca].lower_bound(l);
        if (*q < r) return void(cout << *q << ' ' << *q + 1 << '\n');
        cout << "-1 -1\n";
}

void Solve() {
        cin >> n >> m >> q;
        for (int i = 1; i < n; i++) {
                int u, v;
                cin >> u >> v;
                adj[u].push_back(v);
                adj[v].push_back(u);
        }
        dfs(1, 0);
        for (int i = 1; i <= n; i++) {
                f[i].insert(m + 1);
                g[i].insert(m + 1);
        }
        for (int i = 1; i <= m; i++) {
                cin >> a[i];
                f[a[i]].insert(i);
        }
        for (int i = 1; i < m; i++) {
                g[__lca(a[i], a[i + 1])].insert(i);
        }
        int type, u, v, lca;
        while (q--) {
                cin >> type >> u >> v;
                if (type == 2) {
                        cin >> lca;
                        query(u, v, lca);
                } else {
                        f[a[u]].erase(u);
                        if (u > 1) {
                                g[__lca(a[u - 1], a[u])].erase(u - 1);
                        }
                        if (u < m) {
                                g[__lca(a[u], a[u + 1])].erase(u);
                        }
                        a[u] = v;
                        f[a[u]].insert(u);
                        if (u < m) {
                                g[__lca(a[u], a[u + 1])].insert(u);
                        }
                        if (u > 1) {
                                g[__lca(a[u - 1], a[u])].insert(u - 1);
                        }
                }
        }
}

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        #define TASK "TASK"
        if (fopen(TASK".INP", "r")) {
                freopen(TASK".INP", "r", stdin);
                freopen(TASK".OUT", "w", stdout);
        }
        
        /* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();

        cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
        return 0;
}

Compilation message

treearray.cpp: In function 'int32_t main()':
treearray.cpp:118:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |                 freopen(TASK".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:119:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |                 freopen(TASK".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 11 ms 23900 KB n=100
4 Correct 10 ms 23900 KB n=100
5 Correct 11 ms 23896 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 10 ms 23896 KB n=100
13 Correct 9 ms 23904 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 11 ms 23900 KB n=100
16 Correct 10 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23896 KB n=100
19 Correct 10 ms 23896 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23896 KB n=100
22 Correct 10 ms 23900 KB n=100
23 Correct 9 ms 23928 KB n=100
24 Correct 10 ms 23900 KB n=100
25 Correct 12 ms 23900 KB n=100
26 Correct 10 ms 23896 KB n=12
27 Correct 10 ms 23784 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 11 ms 23900 KB n=100
4 Correct 10 ms 23900 KB n=100
5 Correct 11 ms 23896 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 10 ms 23896 KB n=100
13 Correct 9 ms 23904 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 11 ms 23900 KB n=100
16 Correct 10 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23896 KB n=100
19 Correct 10 ms 23896 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23896 KB n=100
22 Correct 10 ms 23900 KB n=100
23 Correct 9 ms 23928 KB n=100
24 Correct 10 ms 23900 KB n=100
25 Correct 12 ms 23900 KB n=100
26 Correct 10 ms 23896 KB n=12
27 Correct 10 ms 23784 KB n=100
28 Correct 10 ms 23896 KB n=500
29 Correct 12 ms 23960 KB n=500
30 Correct 10 ms 23900 KB n=500
31 Correct 10 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 10 ms 23948 KB n=500
34 Correct 10 ms 23896 KB n=500
35 Correct 10 ms 24152 KB n=500
36 Correct 12 ms 23900 KB n=500
37 Correct 11 ms 23900 KB n=500
38 Correct 11 ms 23896 KB n=500
39 Correct 11 ms 23896 KB n=500
40 Correct 14 ms 23896 KB n=500
41 Correct 11 ms 23900 KB n=500
42 Correct 10 ms 23900 KB n=500
43 Correct 11 ms 23900 KB n=500
44 Correct 12 ms 24004 KB n=500
45 Correct 10 ms 23900 KB n=500
46 Correct 11 ms 24152 KB n=500
47 Correct 11 ms 23900 KB n=500
48 Correct 11 ms 23900 KB n=500
49 Correct 12 ms 23900 KB n=500
50 Correct 16 ms 23900 KB n=500
51 Correct 11 ms 23900 KB n=500
52 Correct 11 ms 24100 KB n=500
53 Correct 11 ms 23900 KB n=500
54 Correct 11 ms 23900 KB n=500
55 Correct 11 ms 23900 KB n=278
56 Correct 11 ms 23900 KB n=500
57 Correct 14 ms 23896 KB n=500
58 Correct 9 ms 23896 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 11 ms 23900 KB n=100
4 Correct 10 ms 23900 KB n=100
5 Correct 11 ms 23896 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 10 ms 23896 KB n=100
13 Correct 9 ms 23904 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 11 ms 23900 KB n=100
16 Correct 10 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23896 KB n=100
19 Correct 10 ms 23896 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23896 KB n=100
22 Correct 10 ms 23900 KB n=100
23 Correct 9 ms 23928 KB n=100
24 Correct 10 ms 23900 KB n=100
25 Correct 12 ms 23900 KB n=100
26 Correct 10 ms 23896 KB n=12
27 Correct 10 ms 23784 KB n=100
28 Correct 10 ms 23896 KB n=500
29 Correct 12 ms 23960 KB n=500
30 Correct 10 ms 23900 KB n=500
31 Correct 10 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 10 ms 23948 KB n=500
34 Correct 10 ms 23896 KB n=500
35 Correct 10 ms 24152 KB n=500
36 Correct 12 ms 23900 KB n=500
37 Correct 11 ms 23900 KB n=500
38 Correct 11 ms 23896 KB n=500
39 Correct 11 ms 23896 KB n=500
40 Correct 14 ms 23896 KB n=500
41 Correct 11 ms 23900 KB n=500
42 Correct 10 ms 23900 KB n=500
43 Correct 11 ms 23900 KB n=500
44 Correct 12 ms 24004 KB n=500
45 Correct 10 ms 23900 KB n=500
46 Correct 11 ms 24152 KB n=500
47 Correct 11 ms 23900 KB n=500
48 Correct 11 ms 23900 KB n=500
49 Correct 12 ms 23900 KB n=500
50 Correct 16 ms 23900 KB n=500
51 Correct 11 ms 23900 KB n=500
52 Correct 11 ms 24100 KB n=500
53 Correct 11 ms 23900 KB n=500
54 Correct 11 ms 23900 KB n=500
55 Correct 11 ms 23900 KB n=278
56 Correct 11 ms 23900 KB n=500
57 Correct 14 ms 23896 KB n=500
58 Correct 9 ms 23896 KB n=500
59 Correct 13 ms 24408 KB n=2000
60 Correct 13 ms 24668 KB n=2000
61 Correct 13 ms 24480 KB n=2000
62 Correct 13 ms 24408 KB n=2000
63 Correct 15 ms 24412 KB n=2000
64 Correct 13 ms 24668 KB n=2000
65 Correct 15 ms 24408 KB n=2000
66 Correct 13 ms 24664 KB n=2000
67 Correct 14 ms 24412 KB n=2000
68 Correct 14 ms 24472 KB n=2000
69 Correct 14 ms 24412 KB n=2000
70 Correct 12 ms 24592 KB n=2000
71 Correct 13 ms 24412 KB n=2000
72 Correct 16 ms 24412 KB n=2000
73 Correct 12 ms 24412 KB n=2000
74 Correct 12 ms 24492 KB n=1844
75 Correct 13 ms 24576 KB n=2000
76 Correct 13 ms 24412 KB n=2000
77 Correct 14 ms 24412 KB n=2000
78 Correct 17 ms 24412 KB n=2000
79 Correct 12 ms 24408 KB n=2000
80 Correct 13 ms 24668 KB n=2000
81 Correct 15 ms 24412 KB n=2000
82 Correct 14 ms 24540 KB n=2000
83 Correct 13 ms 24668 KB n=2000
84 Correct 13 ms 24412 KB n=2000
85 Correct 14 ms 24576 KB n=2000
86 Correct 15 ms 24412 KB n=2000
87 Correct 13 ms 24412 KB n=2000
88 Correct 12 ms 24476 KB n=2000
89 Correct 13 ms 24664 KB n=2000
90 Correct 13 ms 24668 KB n=2000
91 Correct 13 ms 24408 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 11 ms 23900 KB n=100
4 Correct 10 ms 23900 KB n=100
5 Correct 11 ms 23896 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 10 ms 23896 KB n=100
13 Correct 9 ms 23904 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 11 ms 23900 KB n=100
16 Correct 10 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23896 KB n=100
19 Correct 10 ms 23896 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23896 KB n=100
22 Correct 10 ms 23900 KB n=100
23 Correct 9 ms 23928 KB n=100
24 Correct 10 ms 23900 KB n=100
25 Correct 12 ms 23900 KB n=100
26 Correct 10 ms 23896 KB n=12
27 Correct 10 ms 23784 KB n=100
28 Correct 10 ms 23896 KB n=500
29 Correct 12 ms 23960 KB n=500
30 Correct 10 ms 23900 KB n=500
31 Correct 10 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 10 ms 23948 KB n=500
34 Correct 10 ms 23896 KB n=500
35 Correct 10 ms 24152 KB n=500
36 Correct 12 ms 23900 KB n=500
37 Correct 11 ms 23900 KB n=500
38 Correct 11 ms 23896 KB n=500
39 Correct 11 ms 23896 KB n=500
40 Correct 14 ms 23896 KB n=500
41 Correct 11 ms 23900 KB n=500
42 Correct 10 ms 23900 KB n=500
43 Correct 11 ms 23900 KB n=500
44 Correct 12 ms 24004 KB n=500
45 Correct 10 ms 23900 KB n=500
46 Correct 11 ms 24152 KB n=500
47 Correct 11 ms 23900 KB n=500
48 Correct 11 ms 23900 KB n=500
49 Correct 12 ms 23900 KB n=500
50 Correct 16 ms 23900 KB n=500
51 Correct 11 ms 23900 KB n=500
52 Correct 11 ms 24100 KB n=500
53 Correct 11 ms 23900 KB n=500
54 Correct 11 ms 23900 KB n=500
55 Correct 11 ms 23900 KB n=278
56 Correct 11 ms 23900 KB n=500
57 Correct 14 ms 23896 KB n=500
58 Correct 9 ms 23896 KB n=500
59 Correct 13 ms 24408 KB n=2000
60 Correct 13 ms 24668 KB n=2000
61 Correct 13 ms 24480 KB n=2000
62 Correct 13 ms 24408 KB n=2000
63 Correct 15 ms 24412 KB n=2000
64 Correct 13 ms 24668 KB n=2000
65 Correct 15 ms 24408 KB n=2000
66 Correct 13 ms 24664 KB n=2000
67 Correct 14 ms 24412 KB n=2000
68 Correct 14 ms 24472 KB n=2000
69 Correct 14 ms 24412 KB n=2000
70 Correct 12 ms 24592 KB n=2000
71 Correct 13 ms 24412 KB n=2000
72 Correct 16 ms 24412 KB n=2000
73 Correct 12 ms 24412 KB n=2000
74 Correct 12 ms 24492 KB n=1844
75 Correct 13 ms 24576 KB n=2000
76 Correct 13 ms 24412 KB n=2000
77 Correct 14 ms 24412 KB n=2000
78 Correct 17 ms 24412 KB n=2000
79 Correct 12 ms 24408 KB n=2000
80 Correct 13 ms 24668 KB n=2000
81 Correct 15 ms 24412 KB n=2000
82 Correct 14 ms 24540 KB n=2000
83 Correct 13 ms 24668 KB n=2000
84 Correct 13 ms 24412 KB n=2000
85 Correct 14 ms 24576 KB n=2000
86 Correct 15 ms 24412 KB n=2000
87 Correct 13 ms 24412 KB n=2000
88 Correct 12 ms 24476 KB n=2000
89 Correct 13 ms 24664 KB n=2000
90 Correct 13 ms 24668 KB n=2000
91 Correct 13 ms 24408 KB n=2000
92 Correct 502 ms 91724 KB n=200000
93 Correct 616 ms 96596 KB n=200000
94 Correct 637 ms 99928 KB n=200000
95 Correct 473 ms 91516 KB n=200000
96 Correct 462 ms 91680 KB n=200000
97 Correct 632 ms 95904 KB n=200000
98 Correct 478 ms 91588 KB n=200000
99 Correct 531 ms 91728 KB n=200000
100 Correct 457 ms 91596 KB n=200000
101 Correct 610 ms 101456 KB n=200000
102 Correct 281 ms 92860 KB n=200000
103 Correct 292 ms 92876 KB n=200000
104 Correct 284 ms 92756 KB n=200000
105 Correct 306 ms 93272 KB n=200000
106 Correct 290 ms 93332 KB n=200000
107 Correct 302 ms 93268 KB n=200000
108 Correct 515 ms 91616 KB n=200000
109 Correct 548 ms 91684 KB n=200000
110 Correct 522 ms 91564 KB n=200000
111 Correct 445 ms 91208 KB n=200000
112 Correct 638 ms 100180 KB n=200000
113 Correct 651 ms 95828 KB n=200000
114 Correct 458 ms 91080 KB n=200000
115 Correct 604 ms 93680 KB n=200000
116 Correct 475 ms 91820 KB n=200000
117 Correct 580 ms 100688 KB n=200000
118 Correct 587 ms 94544 KB n=200000
119 Correct 438 ms 91844 KB n=200000
120 Correct 579 ms 100248 KB n=200000
121 Correct 600 ms 100128 KB n=200000
122 Correct 564 ms 100472 KB n=200000
123 Correct 301 ms 93096 KB n=200000
124 Correct 166 ms 40812 KB n=25264