Submission #853679

# Submission time Handle Problem Language Result Execution time Memory
853679 2023-09-25T02:12:19 Z anha3k25cvp Birthday gift (IZhO18_treearray) C++14
100 / 100
862 ms 77120 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>

using namespace std;

const int N = 5 + 1e5;
const int inf = 7 + 1e9;

int logn;
vector <int> h;
vector <vector <int>> adj, p;

void dfs(int u) {
    for (int v : adj[u])
        if (!h[v]) {
            p[0][v] = u;
            h[v] = h[u] + 1;
            dfs(v);
        }
}

int lca(int u, int v) {
    if (h[u] < h[v])
        swap(u, v);
    for (int si = h[u] - h[v]; si > 0; si ^= si & -si) {
        int i = __builtin_ctz(si & -si);
        u = p[i][u];
    }
    if (u == v)
        return u;
    for (int i = logn - 1; i >= 0; i --)
        if (p[i][u] != p[i][v]) {
            u = p[i][u];
            v = p[i][v];
        }
    return p[0][u];
}

int main() {
#define TASKNAME ""
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    if ( fopen( TASKNAME".inp", "r" ) ) {
        freopen (TASKNAME".inp", "r", stdin);
        freopen (TASKNAME".out", "w", stdout);
    }
    int n, m, t;
    cin >> n >> m >> t;
    adj.resize(n + 1);
    for (int i = 1; i < n; i ++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (logn = 1; 1 << logn <= n; logn ++);
    p.assign(logn, vector <int> (n + 1, 0));
    h.assign(n + 1, 0);
    h[1] = 1;
    dfs(1);
    for (int i = 1; i < logn; i ++)
        for (int u = 1; u <= n; u ++)
            p[i][u] = p[i - 1][p[i - 1][u]];
    vector <int> a(m + 1, 0), b(m, 0);
    vector <set <int>> q(n + 1), g(n + 1);
    for (int i = 1; i <= m; i ++) {
        cin >> a[i];
        g[a[i]].insert(i);
        if (i > 1) {
            b[i - 1] = lca(a[i - 1], a[i]);
            q[b[i - 1]].insert(i - 1);
        }
    }
    while (t --) {
        int type;
        cin >> type;
        if (type == 1) {
            int pos, val;
            cin >> pos >> val;
            g[a[pos]].erase(pos);
            a[pos] = val;
            g[val].insert(pos);
            if (pos > 1) {
                q[b[pos - 1]].erase(pos - 1);
                b[pos - 1] = lca(a[pos - 1], a[pos]);
                q[b[pos - 1]].insert(pos - 1);
            }
            if (pos < m) {
                q[b[pos]].erase(pos);
                b[pos] = lca(a[pos], a[pos + 1]);
                q[b[pos]].insert(pos);
            }
        }
        else {
            int u, v, val;
            cin >> u >> v >> val;
            auto it = q[val].lower_bound(u), it1 = g[val].lower_bound(u);
            if (it != q[val].end() &&  *it < v)
                cout << *it << ' ' << *it + 1 << '\n';
            else if (it1 != g[val].end() && *it1 <= v)
                cout << *it1 << ' ' << *it1 << '\n';
            else
                cout << "-1 -1\n";
        }
    }
    return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:49:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen (TASKNAME".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:50:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen (TASKNAME".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 0 ms 600 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 0 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 0 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 1 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 0 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 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 600 KB n=100
24 Correct 0 ms 344 KB n=100
25 Correct 1 ms 344 KB n=100
26 Correct 0 ms 344 KB n=12
27 Correct 0 ms 344 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 0 ms 600 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 0 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 0 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 1 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 0 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 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 600 KB n=100
24 Correct 0 ms 344 KB n=100
25 Correct 1 ms 344 KB n=100
26 Correct 0 ms 344 KB n=12
27 Correct 0 ms 344 KB n=100
28 Correct 1 ms 600 KB n=500
29 Correct 1 ms 600 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 600 KB n=500
33 Correct 1 ms 600 KB n=500
34 Correct 1 ms 600 KB n=500
35 Correct 1 ms 600 KB n=500
36 Correct 1 ms 600 KB n=500
37 Correct 1 ms 600 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 600 KB n=500
40 Correct 1 ms 600 KB n=500
41 Correct 1 ms 600 KB n=500
42 Correct 1 ms 600 KB n=500
43 Correct 1 ms 600 KB n=500
44 Correct 1 ms 600 KB n=500
45 Correct 1 ms 600 KB n=500
46 Correct 1 ms 600 KB n=500
47 Correct 1 ms 600 KB n=500
48 Correct 1 ms 600 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 600 KB n=500
51 Correct 1 ms 600 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 600 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 1 ms 600 KB n=500
57 Correct 1 ms 600 KB n=500
58 Correct 1 ms 604 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 0 ms 600 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 0 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 0 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 1 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 0 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 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 600 KB n=100
24 Correct 0 ms 344 KB n=100
25 Correct 1 ms 344 KB n=100
26 Correct 0 ms 344 KB n=12
27 Correct 0 ms 344 KB n=100
28 Correct 1 ms 600 KB n=500
29 Correct 1 ms 600 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 600 KB n=500
33 Correct 1 ms 600 KB n=500
34 Correct 1 ms 600 KB n=500
35 Correct 1 ms 600 KB n=500
36 Correct 1 ms 600 KB n=500
37 Correct 1 ms 600 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 600 KB n=500
40 Correct 1 ms 600 KB n=500
41 Correct 1 ms 600 KB n=500
42 Correct 1 ms 600 KB n=500
43 Correct 1 ms 600 KB n=500
44 Correct 1 ms 600 KB n=500
45 Correct 1 ms 600 KB n=500
46 Correct 1 ms 600 KB n=500
47 Correct 1 ms 600 KB n=500
48 Correct 1 ms 600 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 600 KB n=500
51 Correct 1 ms 600 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 600 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 1 ms 600 KB n=500
57 Correct 1 ms 600 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 3 ms 856 KB n=2000
60 Correct 2 ms 1116 KB n=2000
61 Correct 2 ms 1112 KB n=2000
62 Correct 3 ms 856 KB n=2000
63 Correct 2 ms 856 KB n=2000
64 Correct 2 ms 860 KB n=2000
65 Correct 2 ms 860 KB n=2000
66 Correct 2 ms 1112 KB n=2000
67 Correct 2 ms 856 KB n=2000
68 Correct 2 ms 856 KB n=2000
69 Correct 2 ms 856 KB n=2000
70 Correct 2 ms 856 KB n=2000
71 Correct 2 ms 856 KB n=2000
72 Correct 2 ms 856 KB n=2000
73 Correct 2 ms 856 KB n=2000
74 Correct 3 ms 856 KB n=1844
75 Correct 3 ms 860 KB n=2000
76 Correct 3 ms 856 KB n=2000
77 Correct 2 ms 860 KB n=2000
78 Correct 2 ms 856 KB n=2000
79 Correct 3 ms 860 KB n=2000
80 Correct 2 ms 1112 KB n=2000
81 Correct 2 ms 856 KB n=2000
82 Correct 3 ms 856 KB n=2000
83 Correct 3 ms 1112 KB n=2000
84 Correct 3 ms 856 KB n=2000
85 Correct 2 ms 856 KB n=2000
86 Correct 2 ms 856 KB n=2000
87 Correct 2 ms 856 KB n=2000
88 Correct 2 ms 1112 KB n=2000
89 Correct 2 ms 1116 KB n=2000
90 Correct 2 ms 1112 KB n=2000
91 Correct 2 ms 860 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 0 ms 600 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 0 ms 344 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 1 ms 344 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 0 ms 344 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 1 ms 344 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 0 ms 344 KB n=100
17 Correct 1 ms 344 KB n=100
18 Correct 0 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 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 600 KB n=100
24 Correct 0 ms 344 KB n=100
25 Correct 1 ms 344 KB n=100
26 Correct 0 ms 344 KB n=12
27 Correct 0 ms 344 KB n=100
28 Correct 1 ms 600 KB n=500
29 Correct 1 ms 600 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 600 KB n=500
33 Correct 1 ms 600 KB n=500
34 Correct 1 ms 600 KB n=500
35 Correct 1 ms 600 KB n=500
36 Correct 1 ms 600 KB n=500
37 Correct 1 ms 600 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 600 KB n=500
40 Correct 1 ms 600 KB n=500
41 Correct 1 ms 600 KB n=500
42 Correct 1 ms 600 KB n=500
43 Correct 1 ms 600 KB n=500
44 Correct 1 ms 600 KB n=500
45 Correct 1 ms 600 KB n=500
46 Correct 1 ms 600 KB n=500
47 Correct 1 ms 600 KB n=500
48 Correct 1 ms 600 KB n=500
49 Correct 1 ms 604 KB n=500
50 Correct 1 ms 600 KB n=500
51 Correct 1 ms 600 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 600 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 1 ms 600 KB n=500
57 Correct 1 ms 600 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 3 ms 856 KB n=2000
60 Correct 2 ms 1116 KB n=2000
61 Correct 2 ms 1112 KB n=2000
62 Correct 3 ms 856 KB n=2000
63 Correct 2 ms 856 KB n=2000
64 Correct 2 ms 860 KB n=2000
65 Correct 2 ms 860 KB n=2000
66 Correct 2 ms 1112 KB n=2000
67 Correct 2 ms 856 KB n=2000
68 Correct 2 ms 856 KB n=2000
69 Correct 2 ms 856 KB n=2000
70 Correct 2 ms 856 KB n=2000
71 Correct 2 ms 856 KB n=2000
72 Correct 2 ms 856 KB n=2000
73 Correct 2 ms 856 KB n=2000
74 Correct 3 ms 856 KB n=1844
75 Correct 3 ms 860 KB n=2000
76 Correct 3 ms 856 KB n=2000
77 Correct 2 ms 860 KB n=2000
78 Correct 2 ms 856 KB n=2000
79 Correct 3 ms 860 KB n=2000
80 Correct 2 ms 1112 KB n=2000
81 Correct 2 ms 856 KB n=2000
82 Correct 3 ms 856 KB n=2000
83 Correct 3 ms 1112 KB n=2000
84 Correct 3 ms 856 KB n=2000
85 Correct 2 ms 856 KB n=2000
86 Correct 2 ms 856 KB n=2000
87 Correct 2 ms 856 KB n=2000
88 Correct 2 ms 1112 KB n=2000
89 Correct 2 ms 1116 KB n=2000
90 Correct 2 ms 1112 KB n=2000
91 Correct 2 ms 860 KB n=2000
92 Correct 614 ms 67360 KB n=200000
93 Correct 643 ms 71036 KB n=200000
94 Correct 674 ms 74592 KB n=200000
95 Correct 573 ms 67564 KB n=200000
96 Correct 539 ms 67252 KB n=200000
97 Correct 750 ms 70588 KB n=200000
98 Correct 552 ms 67092 KB n=200000
99 Correct 737 ms 66652 KB n=200000
100 Correct 587 ms 67304 KB n=200000
101 Correct 622 ms 75520 KB n=200000
102 Correct 348 ms 68160 KB n=200000
103 Correct 335 ms 68304 KB n=200000
104 Correct 350 ms 68304 KB n=200000
105 Correct 350 ms 68120 KB n=200000
106 Correct 354 ms 67804 KB n=200000
107 Correct 345 ms 68032 KB n=200000
108 Correct 632 ms 66880 KB n=200000
109 Correct 649 ms 67044 KB n=200000
110 Correct 667 ms 66880 KB n=200000
111 Correct 556 ms 67128 KB n=200000
112 Correct 601 ms 74584 KB n=200000
113 Correct 711 ms 69988 KB n=200000
114 Correct 610 ms 67256 KB n=200000
115 Correct 862 ms 68260 KB n=200000
116 Correct 520 ms 66996 KB n=200000
117 Correct 627 ms 75072 KB n=200000
118 Correct 754 ms 68928 KB n=200000
119 Correct 535 ms 66996 KB n=200000
120 Correct 532 ms 77120 KB n=200000
121 Correct 562 ms 75284 KB n=200000
122 Correct 572 ms 75900 KB n=200000
123 Correct 364 ms 67748 KB n=200000
124 Correct 94 ms 16208 KB n=25264