Submission #1032630

# Submission time Handle Problem Language Result Execution time Memory
1032630 2024-07-24T04:33:31 Z adaawf Tourism (JOI23_tourism) C++17
100 / 100
505 ms 32344 KB
#include <iostream>
#include <vector>
using namespace std;
vector<int> g[100005];
vector<pair<int, int>> v[100005];
int a[100005], s[100005], d[100005], l[100005][18], pp[100005];
int dd[100005], res[100005], z = 0, h = 2000, c[120005], cc[120005], t[400005];
void dfs(int x, int p) {
    l[x][0] = p;
    s[x] = 1;
    for (int w : g[x]) {
        if (w == p) continue;
        d[w] = d[x] + 1;
        dfs(w, x);
        s[x] += s[w];
    }
    for (int &w : g[x]) {
        if (w == p) continue;
        if (s[w] >= (s[x] + 1) / 2) {
            swap(w, g[x][0]);
        }
    }
}
void dfshld(int x, int p, int h, int flag) {
    dd[x] = ++z;
    pp[x] = h;
    for (int w : g[x]) {
        if (w == p) continue;
        if (s[w] >= (s[x] + 1) / 2) {
            if (flag == 0) h = w;
            dfshld(w, x, h, 1);
        }
        else dfshld(w, x, w, 0);
    }
}
int lca(int x, int y) {
    if (d[x] < d[y]) swap(x, y);
    int k = d[x] - d[y];
    for (int i = 17; i >= 0; i--) {
        if (k & (1 << i)) {
            x = l[x][i];
        }
    }
    if (x == y) return x;
    for (int i = 17; i >= 0; i--) {
        if (l[x][i] != l[y][i]) {
            x = l[x][i];
            y = l[y][i];
        }
    }
    return l[x][0];
}
int rise(int x, int y) {
    for (int i = 0; i <= 17; i++) {
        if (y & (1 << i)) {
            x = l[x][i];
        }
    }
    return x;
}
void update(int v, int tl, int tr, int l, int r, int x) {
    if (l > r) return;
    if (tl == l && tr == r) {
        if (t[v] != -1) {
            //cout << l << " " << r << " " << t[v] << " " << x << '\n';
            c[t[v] / h] -= r - l + 1;
            cc[t[v]] -= r - l + 1;
            t[v] = x;
            c[t[v] / h] += r - l + 1;
            cc[t[v]] += r - l + 1;
            return;
        }
        if (l == r) return;
    }
    if (t[v] != -1) {
        t[v * 2] = t[v * 2 + 1] = t[v];
    }
    int mid = (tl + tr) / 2;
    update(v * 2, tl, mid, l, min(r, mid), x);
    update(v * 2 + 1, mid + 1, tr, max(l, mid + 1), r, x);
    if (t[v * 2] == -1 || t[v * 2 + 1] == -1) t[v] = -1;
    else if (t[v * 2] == t[v * 2 + 1]) t[v] = t[v * 2];
    else t[v] = -1;
}
void hld(int x, int y, int c) {
    int h = lca(x, y);
    while (x != h || y != h) {
        if (d[x] < d[y]) swap(x, y);
        int k = pp[x];
        if (d[k] <= d[h]) {
            k = rise(x, d[x] - d[h] - 1);
        }
        //cout << x << " " << y << " " << h << " " << k << " " << dd[k] << " " << dd[x] << " " << c << '\n';
        update(1, 1, z, dd[k], dd[x], c);
        x = l[k][0];
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
    }
    cc[0] = c[0] = n;
    dfs(1, 0);
    dfshld(1, 0, 0, 0);
    for (int i = 1; i <= 17; i++) {
        for (int j = 1; j <= n; j++) {
            l[j][i] = l[l[j][i - 1]][i - 1];
        }
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        v[r].push_back({l, i});
        if (r == 1) res[i] = 1;
    }
    for (int i = 2; i <= m; i++) {
        hld(a[i], a[i - 1], i - 1);
        for (auto w : v[i]) {
            int l = w.first, ll = 0;
            for (int j = m / h; j > l / h; j--) ll += c[j];
            for (int j = l; j < min((l / h + 1) * h, m); j++) ll += cc[j];
            res[w.second] = ll + 1;
            //for (int j = 0; j < n; j++) cout << cc[j] << " ";
            //cout << '\n';
        }
    }
    for (int i = 0; i < q; i++) cout << res[i] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5160 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5164 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 4 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5164 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5224 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5168 KB Output is correct
23 Correct 2 ms 5164 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5172 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5160 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5164 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 4 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5164 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5224 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5168 KB Output is correct
23 Correct 2 ms 5164 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5172 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 5 ms 5468 KB Output is correct
31 Correct 6 ms 5484 KB Output is correct
32 Correct 7 ms 5468 KB Output is correct
33 Correct 6 ms 5432 KB Output is correct
34 Correct 5 ms 5464 KB Output is correct
35 Correct 6 ms 5468 KB Output is correct
36 Correct 6 ms 5468 KB Output is correct
37 Correct 6 ms 5552 KB Output is correct
38 Correct 5 ms 5468 KB Output is correct
39 Correct 5 ms 5468 KB Output is correct
40 Correct 5 ms 5468 KB Output is correct
41 Correct 5 ms 5464 KB Output is correct
42 Correct 5 ms 5468 KB Output is correct
43 Correct 6 ms 5468 KB Output is correct
44 Correct 6 ms 5620 KB Output is correct
45 Correct 6 ms 5468 KB Output is correct
46 Correct 6 ms 5468 KB Output is correct
47 Correct 6 ms 5468 KB Output is correct
48 Correct 6 ms 5432 KB Output is correct
49 Correct 6 ms 5468 KB Output is correct
50 Correct 4 ms 5468 KB Output is correct
51 Correct 5 ms 5468 KB Output is correct
52 Correct 5 ms 5468 KB Output is correct
53 Correct 4 ms 5576 KB Output is correct
54 Correct 5 ms 5436 KB Output is correct
55 Correct 5 ms 5464 KB Output is correct
56 Correct 4 ms 5212 KB Output is correct
57 Correct 3 ms 5472 KB Output is correct
58 Correct 6 ms 5472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4960 KB Output is correct
2 Correct 2 ms 5216 KB Output is correct
3 Correct 4 ms 5212 KB Output is correct
4 Correct 142 ms 19344 KB Output is correct
5 Correct 118 ms 26192 KB Output is correct
6 Correct 145 ms 28620 KB Output is correct
7 Correct 201 ms 32344 KB Output is correct
8 Correct 212 ms 32344 KB Output is correct
9 Correct 221 ms 32336 KB Output is correct
10 Correct 185 ms 32344 KB Output is correct
11 Correct 176 ms 32336 KB Output is correct
12 Correct 168 ms 31316 KB Output is correct
13 Correct 157 ms 31312 KB Output is correct
14 Correct 143 ms 31312 KB Output is correct
15 Correct 42 ms 27616 KB Output is correct
16 Correct 226 ms 32084 KB Output is correct
17 Correct 65 ms 9296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 143 ms 12456 KB Output is correct
3 Correct 254 ms 13392 KB Output is correct
4 Correct 179 ms 15952 KB Output is correct
5 Correct 281 ms 20308 KB Output is correct
6 Correct 296 ms 20444 KB Output is correct
7 Correct 291 ms 20472 KB Output is correct
8 Correct 281 ms 20680 KB Output is correct
9 Correct 301 ms 20316 KB Output is correct
10 Correct 296 ms 20524 KB Output is correct
11 Correct 326 ms 20480 KB Output is correct
12 Correct 291 ms 20464 KB Output is correct
13 Correct 305 ms 20864 KB Output is correct
14 Correct 307 ms 21912 KB Output is correct
15 Correct 354 ms 25172 KB Output is correct
16 Correct 253 ms 20896 KB Output is correct
17 Correct 287 ms 20820 KB Output is correct
18 Correct 266 ms 20816 KB Output is correct
19 Correct 284 ms 20560 KB Output is correct
20 Correct 284 ms 20528 KB Output is correct
21 Correct 293 ms 20560 KB Output is correct
22 Correct 295 ms 20828 KB Output is correct
23 Correct 290 ms 20564 KB Output is correct
24 Correct 277 ms 20564 KB Output is correct
25 Correct 297 ms 20512 KB Output is correct
26 Correct 299 ms 20448 KB Output is correct
27 Correct 326 ms 20560 KB Output is correct
28 Correct 341 ms 20564 KB Output is correct
29 Correct 281 ms 20564 KB Output is correct
30 Correct 301 ms 20820 KB Output is correct
31 Correct 290 ms 21076 KB Output is correct
32 Correct 339 ms 21588 KB Output is correct
33 Correct 331 ms 22864 KB Output is correct
34 Correct 342 ms 25428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 4 ms 5212 KB Output is correct
4 Correct 458 ms 15632 KB Output is correct
5 Correct 388 ms 18004 KB Output is correct
6 Correct 471 ms 22408 KB Output is correct
7 Correct 453 ms 24532 KB Output is correct
8 Correct 495 ms 24308 KB Output is correct
9 Correct 502 ms 24644 KB Output is correct
10 Correct 500 ms 24400 KB Output is correct
11 Correct 505 ms 24404 KB Output is correct
12 Correct 503 ms 24480 KB Output is correct
13 Correct 504 ms 24368 KB Output is correct
14 Correct 77 ms 9300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5160 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5164 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 4 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 3 ms 5164 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5224 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5168 KB Output is correct
23 Correct 2 ms 5164 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5172 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 5 ms 5468 KB Output is correct
31 Correct 6 ms 5484 KB Output is correct
32 Correct 7 ms 5468 KB Output is correct
33 Correct 6 ms 5432 KB Output is correct
34 Correct 5 ms 5464 KB Output is correct
35 Correct 6 ms 5468 KB Output is correct
36 Correct 6 ms 5468 KB Output is correct
37 Correct 6 ms 5552 KB Output is correct
38 Correct 5 ms 5468 KB Output is correct
39 Correct 5 ms 5468 KB Output is correct
40 Correct 5 ms 5468 KB Output is correct
41 Correct 5 ms 5464 KB Output is correct
42 Correct 5 ms 5468 KB Output is correct
43 Correct 6 ms 5468 KB Output is correct
44 Correct 6 ms 5620 KB Output is correct
45 Correct 6 ms 5468 KB Output is correct
46 Correct 6 ms 5468 KB Output is correct
47 Correct 6 ms 5468 KB Output is correct
48 Correct 6 ms 5432 KB Output is correct
49 Correct 6 ms 5468 KB Output is correct
50 Correct 4 ms 5468 KB Output is correct
51 Correct 5 ms 5468 KB Output is correct
52 Correct 5 ms 5468 KB Output is correct
53 Correct 4 ms 5576 KB Output is correct
54 Correct 5 ms 5436 KB Output is correct
55 Correct 5 ms 5464 KB Output is correct
56 Correct 4 ms 5212 KB Output is correct
57 Correct 3 ms 5472 KB Output is correct
58 Correct 6 ms 5472 KB Output is correct
59 Correct 2 ms 4960 KB Output is correct
60 Correct 2 ms 5216 KB Output is correct
61 Correct 4 ms 5212 KB Output is correct
62 Correct 142 ms 19344 KB Output is correct
63 Correct 118 ms 26192 KB Output is correct
64 Correct 145 ms 28620 KB Output is correct
65 Correct 201 ms 32344 KB Output is correct
66 Correct 212 ms 32344 KB Output is correct
67 Correct 221 ms 32336 KB Output is correct
68 Correct 185 ms 32344 KB Output is correct
69 Correct 176 ms 32336 KB Output is correct
70 Correct 168 ms 31316 KB Output is correct
71 Correct 157 ms 31312 KB Output is correct
72 Correct 143 ms 31312 KB Output is correct
73 Correct 42 ms 27616 KB Output is correct
74 Correct 226 ms 32084 KB Output is correct
75 Correct 65 ms 9296 KB Output is correct
76 Correct 2 ms 4956 KB Output is correct
77 Correct 143 ms 12456 KB Output is correct
78 Correct 254 ms 13392 KB Output is correct
79 Correct 179 ms 15952 KB Output is correct
80 Correct 281 ms 20308 KB Output is correct
81 Correct 296 ms 20444 KB Output is correct
82 Correct 291 ms 20472 KB Output is correct
83 Correct 281 ms 20680 KB Output is correct
84 Correct 301 ms 20316 KB Output is correct
85 Correct 296 ms 20524 KB Output is correct
86 Correct 326 ms 20480 KB Output is correct
87 Correct 291 ms 20464 KB Output is correct
88 Correct 305 ms 20864 KB Output is correct
89 Correct 307 ms 21912 KB Output is correct
90 Correct 354 ms 25172 KB Output is correct
91 Correct 253 ms 20896 KB Output is correct
92 Correct 287 ms 20820 KB Output is correct
93 Correct 266 ms 20816 KB Output is correct
94 Correct 284 ms 20560 KB Output is correct
95 Correct 284 ms 20528 KB Output is correct
96 Correct 293 ms 20560 KB Output is correct
97 Correct 295 ms 20828 KB Output is correct
98 Correct 290 ms 20564 KB Output is correct
99 Correct 277 ms 20564 KB Output is correct
100 Correct 297 ms 20512 KB Output is correct
101 Correct 299 ms 20448 KB Output is correct
102 Correct 326 ms 20560 KB Output is correct
103 Correct 341 ms 20564 KB Output is correct
104 Correct 281 ms 20564 KB Output is correct
105 Correct 301 ms 20820 KB Output is correct
106 Correct 290 ms 21076 KB Output is correct
107 Correct 339 ms 21588 KB Output is correct
108 Correct 331 ms 22864 KB Output is correct
109 Correct 342 ms 25428 KB Output is correct
110 Correct 2 ms 4952 KB Output is correct
111 Correct 2 ms 4956 KB Output is correct
112 Correct 4 ms 5212 KB Output is correct
113 Correct 458 ms 15632 KB Output is correct
114 Correct 388 ms 18004 KB Output is correct
115 Correct 471 ms 22408 KB Output is correct
116 Correct 453 ms 24532 KB Output is correct
117 Correct 495 ms 24308 KB Output is correct
118 Correct 502 ms 24644 KB Output is correct
119 Correct 500 ms 24400 KB Output is correct
120 Correct 505 ms 24404 KB Output is correct
121 Correct 503 ms 24480 KB Output is correct
122 Correct 504 ms 24368 KB Output is correct
123 Correct 77 ms 9300 KB Output is correct
124 Correct 312 ms 24168 KB Output is correct
125 Correct 215 ms 21840 KB Output is correct
126 Correct 354 ms 24464 KB Output is correct
127 Correct 406 ms 24632 KB Output is correct
128 Correct 353 ms 24656 KB Output is correct
129 Correct 347 ms 24660 KB Output is correct
130 Correct 337 ms 24660 KB Output is correct
131 Correct 207 ms 31056 KB Output is correct
132 Correct 236 ms 32196 KB Output is correct
133 Correct 236 ms 28312 KB Output is correct
134 Correct 324 ms 24552 KB Output is correct
135 Correct 371 ms 24560 KB Output is correct
136 Correct 345 ms 24608 KB Output is correct
137 Correct 137 ms 24716 KB Output is correct
138 Correct 151 ms 24852 KB Output is correct
139 Correct 144 ms 24704 KB Output is correct
140 Correct 138 ms 24796 KB Output is correct
141 Correct 138 ms 24772 KB Output is correct
142 Correct 134 ms 24876 KB Output is correct
143 Correct 46 ms 20052 KB Output is correct
144 Correct 369 ms 24372 KB Output is correct