Submission #957158

# Submission time Handle Problem Language Result Execution time Memory
957158 2024-04-03T05:40:20 Z ZHIRDILBILDIZ Tourism (JOI23_tourism) C++14
100 / 100
589 ms 38860 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = (1 << 17);
int c[N];
int sum[N << 1 ^ 1];
vector<int> gr[N];
vector<pii> qr[N];
///segment_tree begin
void upd_point_seg_tree (int ind, int vall) {
    ind += N - 1;
    sum[ind] += vall;
    ind >>= 1;
    while (ind) {
        sum[ind] = sum[ind << 1] + sum[ind << 1 ^ 1];
        ind >>= 1;
    }
}
int get_sum(int l1, int r1, int l = 1, int r = N, int v = 1) {
    if (l > r1 || r < l1)
        return 0;
    if (l1 <= l && r <= r1)
        return sum[v];
    int mid = (l + r) >> 1;
    return get_sum(l1, r1, l, mid, v << 1) + get_sum(l1, r1, mid + 1, r, v << 1 ^ 1);
}
///segment_tree end

///build_compose begin
int pos[N];
int pr[N];
int big[N];
int head[N];
int tin[N];
int tout[N];
int timer;
int lc[N][20];
vector<int> tr;
bool upper(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int pre_calc (int city = 1, int last = 0) {
    tin[city] = ++timer;
    int cnt = 1;
    pii mx = {0, 0};
    for (int i : gr[city]) {
        if (i == last)
            continue;
        int w = pre_calc(i, city);
        mx = max(mx, {w, i});
        cnt += w;
    }
    tout[city] = ++timer;
    big[city] = mx.se;
    return cnt;
}
void dfs (int city = 1, int last = 0, int h = 1) {
    pr[city] = last;
    lc[city][0] = last;
    for (int i = 1; i < 20; ++i)
        lc[city][i] = lc[lc[city][i - 1]][i - 1];
    head[city] = h;
    pos[city] = tr.size();
    tr.push_back(city);
    if (big[city])
        dfs(big[city], city, h);
    for (int i : gr[city]) {
        if (i == last || i == big[city])
            continue;
        dfs(i, city, i);
    }
}
///build_compose end

///query and update on decompose begin
int color[2 * N + 1];
bool have[2 * N + 1];
void tractor (int v) {
    have[v] = (color[v] > 0);
    if (v < N && (have[v << 1] || have[v << 1 ^ 1]))
        have[v] = 1;
}
void push (int v) {
    if (!color[v] || v >= N)
        return;
    int x = color[v];
    color[v] = 0;
    color[v << 1] = x;
    color[v << 1 ^ 1] = x;
    tractor(v << 1);
    tractor(v << 1 ^ 1);
}
void set_color (int l, int r, int v, int x) {
    if (color[v])
        upd_point_seg_tree(color[v], -(r - l + 1));
    color[v] = x;
    if (color[v])
        upd_point_seg_tree(color[v], (r - l + 1));
    tractor(v);
}
void mega_clear (int l, int r, int v) {
    set_color (l, r, v, 0);
    if (!have[v])
        return;
    int mid = (l + r) >> 1;
    mega_clear (l, mid, v << 1);
    mega_clear (mid + 1, r, v << 1 ^ 1);
    have[v] = 0;
}
void update (int l1, int r1, int x, int l = 0, int r = N - 1, int v = 1) {
    if (l > r1 || r < l1)
        return;
    if (l1 <= l && r <= r1) {
        mega_clear(l, r, v);
        set_color(l, r, v, x);
        return;
    }
    push(v);
    int mid = (l + r) >> 1;
    update(l1, r1, x, l, mid, v << 1);
    update(l1, r1, x, mid + 1, r, v << 1 ^ 1);
    tractor(v);
}

int get_lca(int u, int v) {
    if (upper(u, v))
        return u;
    if (upper(v, u))
        return v;
    for (int i = 19; i >= 0; --i)
        if (lc[u][i] && !upper(lc[u][i], v))
            u = lc[u][i];
    return pr[u];
}

void update_on_path (int u, int v, int x) {
    int lc = get_lca(u, v);
//    cout << "update_on_path " << u << ' ' << v << ' ' << lc << ' ' << x << '\n';
    while (u != pr[lc]) {
        if (!upper(head[u], lc)) {
            update(pos[head[u]], pos[u], x);
//            cout << "sum1 = " << pos[head[u]] << ' ' << pos[u] << ' ' << get_sum(4, 6) << '\n';
            u = pr[head[u]];
        } else {
            update(pos[lc], pos[u], x);
//            cout << "sum2 = " << pos[lc] << ' ' << pos[u] << ' ' << get_sum(4, 6) << '\n';
            u = pr[lc];
        }
    }
    while (v != pr[lc])
        if (!upper(head[v], lc)) {
            update(pos[head[v]], pos[v], x);
//            cout << "sum3 = " << pos[head[v]] << ' ' << pos[v] << ' ' << get_sum(4, 6) << '\n';
            v = pr[head[v]];
        } else {
            update(pos[lc], pos[v], x);
//            cout << "sum4 = " << pos[lc] << ' ' << pos[v] << ' ' << get_sum(4, 6) << '\n';
            v = pr[lc];
        }
}
///query and update on decompose end

signed main () {
    ios_base::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;
        gr[u].push_back(v);
        gr[v].push_back(u);
    }
    for (int i = 1; i <= m; ++i)
        cin >> c[i];

    int ans[q + 1] = {};
    for (int i = 1; i <= q; ++i) {
        int l, r;
        cin >> l >> r;
        if (l == r) {
            ans[i] = 1;
            continue;
        }
        qr[r].push_back({l, i});
    }
    pre_calc();
    dfs();

    for (int i = 2; i <= m; ++i) {
        update_on_path (c[i - 1], c[i], i - 1);
//        cout << "after update_on_path " << c[i - 1] << ' ' << c[i] << ' ' << i - 1 << " " << get_sum(4, 6) << '\n';
//        cout << "_______________________________________________\n";
        for (auto j : qr[i])
            ans[j.se] = get_sum(j.fi, i);
    }

    for (int i = 1; i <= q; ++i)
        cout << ans[i] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 4 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 6 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 3 ms 10764 KB Output is correct
9 Correct 3 ms 10840 KB Output is correct
10 Correct 3 ms 10840 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10812 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 3 ms 10920 KB Output is correct
19 Correct 3 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 3 ms 10844 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10844 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 3 ms 10840 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 2 ms 10584 KB Output is correct
29 Correct 3 ms 10896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 4 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 6 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 3 ms 10764 KB Output is correct
9 Correct 3 ms 10840 KB Output is correct
10 Correct 3 ms 10840 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10812 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 3 ms 10920 KB Output is correct
19 Correct 3 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 3 ms 10844 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10844 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 3 ms 10840 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 2 ms 10584 KB Output is correct
29 Correct 3 ms 10896 KB Output is correct
30 Correct 6 ms 10844 KB Output is correct
31 Correct 6 ms 10816 KB Output is correct
32 Correct 8 ms 11072 KB Output is correct
33 Correct 7 ms 10844 KB Output is correct
34 Correct 7 ms 11100 KB Output is correct
35 Correct 7 ms 10844 KB Output is correct
36 Correct 8 ms 11076 KB Output is correct
37 Correct 7 ms 10844 KB Output is correct
38 Correct 7 ms 11100 KB Output is correct
39 Correct 7 ms 11100 KB Output is correct
40 Correct 7 ms 11100 KB Output is correct
41 Correct 6 ms 11100 KB Output is correct
42 Correct 7 ms 11100 KB Output is correct
43 Correct 7 ms 11100 KB Output is correct
44 Correct 8 ms 11100 KB Output is correct
45 Correct 8 ms 11100 KB Output is correct
46 Correct 8 ms 11100 KB Output is correct
47 Correct 8 ms 11100 KB Output is correct
48 Correct 8 ms 11100 KB Output is correct
49 Correct 7 ms 11100 KB Output is correct
50 Correct 5 ms 10904 KB Output is correct
51 Correct 6 ms 11100 KB Output is correct
52 Correct 5 ms 11100 KB Output is correct
53 Correct 5 ms 11096 KB Output is correct
54 Correct 5 ms 11100 KB Output is correct
55 Correct 5 ms 10916 KB Output is correct
56 Correct 5 ms 10844 KB Output is correct
57 Correct 3 ms 10844 KB Output is correct
58 Correct 6 ms 10908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 4 ms 10872 KB Output is correct
4 Correct 242 ms 27344 KB Output is correct
5 Correct 179 ms 31436 KB Output is correct
6 Correct 239 ms 35276 KB Output is correct
7 Correct 306 ms 38416 KB Output is correct
8 Correct 295 ms 38288 KB Output is correct
9 Correct 309 ms 38392 KB Output is correct
10 Correct 298 ms 38324 KB Output is correct
11 Correct 299 ms 38860 KB Output is correct
12 Correct 168 ms 37576 KB Output is correct
13 Correct 188 ms 37404 KB Output is correct
14 Correct 176 ms 37832 KB Output is correct
15 Correct 50 ms 33224 KB Output is correct
16 Correct 271 ms 36044 KB Output is correct
17 Correct 100 ms 15884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 184 ms 19096 KB Output is correct
3 Correct 315 ms 19920 KB Output is correct
4 Correct 223 ms 22184 KB Output is correct
5 Correct 354 ms 26536 KB Output is correct
6 Correct 371 ms 26624 KB Output is correct
7 Correct 353 ms 26604 KB Output is correct
8 Correct 355 ms 26568 KB Output is correct
9 Correct 375 ms 26564 KB Output is correct
10 Correct 369 ms 26596 KB Output is correct
11 Correct 372 ms 26692 KB Output is correct
12 Correct 355 ms 26572 KB Output is correct
13 Correct 356 ms 27100 KB Output is correct
14 Correct 371 ms 27932 KB Output is correct
15 Correct 416 ms 28420 KB Output is correct
16 Correct 352 ms 27092 KB Output is correct
17 Correct 359 ms 27124 KB Output is correct
18 Correct 351 ms 27340 KB Output is correct
19 Correct 521 ms 26624 KB Output is correct
20 Correct 525 ms 26556 KB Output is correct
21 Correct 546 ms 26796 KB Output is correct
22 Correct 540 ms 26544 KB Output is correct
23 Correct 534 ms 26548 KB Output is correct
24 Correct 529 ms 26588 KB Output is correct
25 Correct 554 ms 26572 KB Output is correct
26 Correct 554 ms 26928 KB Output is correct
27 Correct 533 ms 26580 KB Output is correct
28 Correct 546 ms 27028 KB Output is correct
29 Correct 542 ms 26704 KB Output is correct
30 Correct 551 ms 26964 KB Output is correct
31 Correct 526 ms 27088 KB Output is correct
32 Correct 573 ms 27672 KB Output is correct
33 Correct 561 ms 28740 KB Output is correct
34 Correct 546 ms 28276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 6 ms 10844 KB Output is correct
3 Correct 5 ms 10812 KB Output is correct
4 Correct 429 ms 23688 KB Output is correct
5 Correct 449 ms 23708 KB Output is correct
6 Correct 519 ms 27440 KB Output is correct
7 Correct 551 ms 30608 KB Output is correct
8 Correct 526 ms 30544 KB Output is correct
9 Correct 518 ms 30548 KB Output is correct
10 Correct 530 ms 30452 KB Output is correct
11 Correct 534 ms 30456 KB Output is correct
12 Correct 532 ms 30720 KB Output is correct
13 Correct 550 ms 30404 KB Output is correct
14 Correct 104 ms 15780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 4 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 6 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 3 ms 10764 KB Output is correct
9 Correct 3 ms 10840 KB Output is correct
10 Correct 3 ms 10840 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10812 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10840 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 3 ms 10920 KB Output is correct
19 Correct 3 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 3 ms 10844 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10844 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 3 ms 10840 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 2 ms 10584 KB Output is correct
29 Correct 3 ms 10896 KB Output is correct
30 Correct 6 ms 10844 KB Output is correct
31 Correct 6 ms 10816 KB Output is correct
32 Correct 8 ms 11072 KB Output is correct
33 Correct 7 ms 10844 KB Output is correct
34 Correct 7 ms 11100 KB Output is correct
35 Correct 7 ms 10844 KB Output is correct
36 Correct 8 ms 11076 KB Output is correct
37 Correct 7 ms 10844 KB Output is correct
38 Correct 7 ms 11100 KB Output is correct
39 Correct 7 ms 11100 KB Output is correct
40 Correct 7 ms 11100 KB Output is correct
41 Correct 6 ms 11100 KB Output is correct
42 Correct 7 ms 11100 KB Output is correct
43 Correct 7 ms 11100 KB Output is correct
44 Correct 8 ms 11100 KB Output is correct
45 Correct 8 ms 11100 KB Output is correct
46 Correct 8 ms 11100 KB Output is correct
47 Correct 8 ms 11100 KB Output is correct
48 Correct 8 ms 11100 KB Output is correct
49 Correct 7 ms 11100 KB Output is correct
50 Correct 5 ms 10904 KB Output is correct
51 Correct 6 ms 11100 KB Output is correct
52 Correct 5 ms 11100 KB Output is correct
53 Correct 5 ms 11096 KB Output is correct
54 Correct 5 ms 11100 KB Output is correct
55 Correct 5 ms 10916 KB Output is correct
56 Correct 5 ms 10844 KB Output is correct
57 Correct 3 ms 10844 KB Output is correct
58 Correct 6 ms 10908 KB Output is correct
59 Correct 2 ms 10840 KB Output is correct
60 Correct 3 ms 10844 KB Output is correct
61 Correct 4 ms 10872 KB Output is correct
62 Correct 242 ms 27344 KB Output is correct
63 Correct 179 ms 31436 KB Output is correct
64 Correct 239 ms 35276 KB Output is correct
65 Correct 306 ms 38416 KB Output is correct
66 Correct 295 ms 38288 KB Output is correct
67 Correct 309 ms 38392 KB Output is correct
68 Correct 298 ms 38324 KB Output is correct
69 Correct 299 ms 38860 KB Output is correct
70 Correct 168 ms 37576 KB Output is correct
71 Correct 188 ms 37404 KB Output is correct
72 Correct 176 ms 37832 KB Output is correct
73 Correct 50 ms 33224 KB Output is correct
74 Correct 271 ms 36044 KB Output is correct
75 Correct 100 ms 15884 KB Output is correct
76 Correct 2 ms 10840 KB Output is correct
77 Correct 184 ms 19096 KB Output is correct
78 Correct 315 ms 19920 KB Output is correct
79 Correct 223 ms 22184 KB Output is correct
80 Correct 354 ms 26536 KB Output is correct
81 Correct 371 ms 26624 KB Output is correct
82 Correct 353 ms 26604 KB Output is correct
83 Correct 355 ms 26568 KB Output is correct
84 Correct 375 ms 26564 KB Output is correct
85 Correct 369 ms 26596 KB Output is correct
86 Correct 372 ms 26692 KB Output is correct
87 Correct 355 ms 26572 KB Output is correct
88 Correct 356 ms 27100 KB Output is correct
89 Correct 371 ms 27932 KB Output is correct
90 Correct 416 ms 28420 KB Output is correct
91 Correct 352 ms 27092 KB Output is correct
92 Correct 359 ms 27124 KB Output is correct
93 Correct 351 ms 27340 KB Output is correct
94 Correct 521 ms 26624 KB Output is correct
95 Correct 525 ms 26556 KB Output is correct
96 Correct 546 ms 26796 KB Output is correct
97 Correct 540 ms 26544 KB Output is correct
98 Correct 534 ms 26548 KB Output is correct
99 Correct 529 ms 26588 KB Output is correct
100 Correct 554 ms 26572 KB Output is correct
101 Correct 554 ms 26928 KB Output is correct
102 Correct 533 ms 26580 KB Output is correct
103 Correct 546 ms 27028 KB Output is correct
104 Correct 542 ms 26704 KB Output is correct
105 Correct 551 ms 26964 KB Output is correct
106 Correct 526 ms 27088 KB Output is correct
107 Correct 573 ms 27672 KB Output is correct
108 Correct 561 ms 28740 KB Output is correct
109 Correct 546 ms 28276 KB Output is correct
110 Correct 2 ms 10840 KB Output is correct
111 Correct 6 ms 10844 KB Output is correct
112 Correct 5 ms 10812 KB Output is correct
113 Correct 429 ms 23688 KB Output is correct
114 Correct 449 ms 23708 KB Output is correct
115 Correct 519 ms 27440 KB Output is correct
116 Correct 551 ms 30608 KB Output is correct
117 Correct 526 ms 30544 KB Output is correct
118 Correct 518 ms 30548 KB Output is correct
119 Correct 530 ms 30452 KB Output is correct
120 Correct 534 ms 30456 KB Output is correct
121 Correct 532 ms 30720 KB Output is correct
122 Correct 550 ms 30404 KB Output is correct
123 Correct 104 ms 15780 KB Output is correct
124 Correct 343 ms 30152 KB Output is correct
125 Correct 284 ms 28104 KB Output is correct
126 Correct 419 ms 30660 KB Output is correct
127 Correct 420 ms 30568 KB Output is correct
128 Correct 424 ms 30588 KB Output is correct
129 Correct 423 ms 30680 KB Output is correct
130 Correct 416 ms 30532 KB Output is correct
131 Correct 351 ms 37320 KB Output is correct
132 Correct 323 ms 38368 KB Output is correct
133 Correct 388 ms 34508 KB Output is correct
134 Correct 589 ms 30944 KB Output is correct
135 Correct 583 ms 30728 KB Output is correct
136 Correct 585 ms 30612 KB Output is correct
137 Correct 208 ms 30940 KB Output is correct
138 Correct 199 ms 30916 KB Output is correct
139 Correct 210 ms 31048 KB Output is correct
140 Correct 196 ms 31048 KB Output is correct
141 Correct 196 ms 31000 KB Output is correct
142 Correct 191 ms 31000 KB Output is correct
143 Correct 59 ms 25564 KB Output is correct
144 Correct 372 ms 28336 KB Output is correct