Submission #887766

# Submission time Handle Problem Language Result Execution time Memory
887766 2023-12-15T07:47:29 Z box Tourism (JOI23_tourism) C++17
100 / 100
650 ms 35640 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array
#define sz(v) int(std::size(v))
using pii = pair<int, int>;

const int N = 1e5, L = 17, M = 1e5, Q = 1e5;

int n, m, q;
vector<int> adj[N];
int c[N], tin[N], tt;
vector<int> et;
int depth[N], lg[N], st[N][L];
int ans[Q];

int low(int i, int j) {
    return depth[i] < depth[j] ? i : j;
}

void dfs(int p, int i) {
    tin[i] = tt++;
    for (int j : adj[i]) if (p != j) {
        depth[j] = depth[i] + 1;
        et.push_back(i);
        dfs(i, j);
    }
}

int qlow(int i, int j) {
    int l = lg[j - i + 1];
    return low(st[i][l], st[j - (1 << l) + 1][l]);
}

int lca_(int i, int j) {
    if (i == j) return i;
    if (tin[i] > tin[j]) swap(i, j);
    return qlow(tin[i], tin[j] - 1);
}

int lca(int i, int j) {
    int p = lca_(i, j);
    // cout << i+1 << " + " << j+1 << " => " << p+1 << '\n';
    return p;
}

vector<pii> aux[N];
int tl[N], tr[N], up[N];

void ae(int i, int j, int d) {
    // cout << "\tlink " << i+1 << ' ' << j+1 << ' ' << d << endl;
    aux[i].push_back({j, d});
    aux[j].push_back({i, d});
}

void dfs_t(int p, int i) {
    for (auto [j, d] : aux[i]) if (p != j) {
        up[j] = d;
        dfs_t(i, j);
        tl[i] = max(tl[i], tl[j]);
        tr[i] = min(tr[i], tr[j]);
    }
}

struct FT {
    int t[M];
    void upd(int i, int x) {
        while (i < m) {
            t[i] += x;
            i |= i + 1;
        }
    }
    int qry(int i) {
        int x = 0;
        while (i >= 0) {
            x += t[i];
            i &= i + 1, i--;
        }
        return x;
    }
} ft;

void rec(int low, int hi, vector<ar<int, 3>> qr) {
    if (sz(qr)) {
        int mid = (low + hi) / 2;
        vector<ar<int, 3>> me;
        {
            vector<ar<int, 3>> one, two;
            for (auto [l, r, h] : qr)
                if (r < mid) one.push_back({l, r, h});
                else if (l > mid) two.push_back({l, r, h});
                else me.push_back({l, r, h});
            rec(low, mid, one);
            rec(mid + 1, hi, two);
        }
        // cout << low << ' ' << hi << endl;
        // for (auto [l, r, h] : qr) cout << "\t" << l << ' ' << r << ' ' << h << endl;
        vector<int> v(c + low, c + hi + 1);
        sort(begin(v), end(v), [&](int i, int j) { return tin[i] < tin[j]; });
        v.erase(unique(begin(v), end(v)), end(v));
        int base = sz(v);
        for (int i = 0; i < base - 1; i++) v.push_back(lca(v[i], v[i + 1]));
        sort(begin(v), end(v), [&](int i, int j) { return tin[i] < tin[j]; });
        v.erase(unique(begin(v), end(v)), end(v));
        for (int i = 1; i < sz(v); i++) {
            int p = lca(v[i - 1], v[i]);
            ae(p, v[i], depth[v[i]] - depth[p]);
        }
        for (int i : v) tl[i] = low - 1, tr[i] = hi + 1;
        for (int i = low; i <= hi; i++) {
            if (i <= mid) tl[c[i]] = max(tl[c[i]], i);
            if (i >= mid) tr[c[i]] = min(tr[c[i]], i);
        }
        dfs_t(-1, c[mid]);
        // cout << "ROOT AT " << c[mid]+1 << endl;
        vector<int> one(mid - low + 1), two(hi - mid + 1);
        vector<ar<int, 3>> upd;
        for (int i : v) if (i != c[mid]) {
            // cout << i+1 << " => " << tl[i] << ' ' << tr[i] << " | val = " << up[i] << endl;
            if (tl[i] >= low) one[tl[i] - low] += up[i];
            if (tr[i] <= hi) two[tr[i] - mid] += up[i];
            if (tl[i] >= low && tr[i] <= hi) upd.push_back({tl[i], tr[i], up[i]});
        }
        for (int i = mid - 1; i >= low; i--) one[i - low] += one[i - low + 1];
        for (int i = mid + 1; i <= hi; i++) two[i - mid] += two[i - mid - 1];
        sort(begin(upd), end(upd), greater<>());
        sort(begin(me), end(me), greater<>());
        int p = 0;
        for (auto [l, r, h] : me) {
            ans[h] += one[l - low] + two[r - mid];
            while (p < sz(upd) && l <= upd[p][0]) {
                auto [i, j, x] = upd[p++];
                ft.upd(j, x);
            }
            ans[h] -= ft.qry(r);
        }
        while (p > 0) {
            auto [i, j, x] = upd[--p];
            ft.upd(j, -x);
        }
        for (int i : v) vector<pii>().swap(aux[i]);
        // cout << endl << endl << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m >> q;
    for (int h = 0; h < n - 1; h++) {
        int i, j;
        cin >> i >> j, i--, j--;
        adj[i].push_back(j);
        adj[j].push_back(i);
    }
    dfs(-1, 0);
    lg[1] = 0;
    for (int i = 2; i <= sz(et); i++) lg[i] = lg[i / 2] + 1;
    for (int i = 0; i < sz(et); i++) st[i][0] = et[i];
    for (int l = 1; 1 << l <= sz(et); l++) for (int i = 0; i + (1 << l) <= sz(et); i++)
        st[i][l] = low(st[i][l - 1], st[i + (1 << (l - 1))][l - 1]);
    for (int i = 0; i < m; i++) cin >> c[i], c[i]--;
    vector<ar<int, 3>> qr;
    for (int h = 0; h < q; h++) {
        int l, r;
        cin >> l >> r, l--, r--;
        qr.push_back({l, r, h});
    }
    rec(0, m - 1, qr);
    for (int h = 0; h < q; h++) cout << ans[h] + 1 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 11060 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11060 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11352 KB Output is correct
11 Correct 2 ms 11100 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11100 KB Output is correct
15 Correct 2 ms 11068 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11096 KB Output is correct
19 Correct 2 ms 11100 KB Output is correct
20 Correct 2 ms 11100 KB Output is correct
21 Correct 3 ms 11096 KB Output is correct
22 Correct 2 ms 11100 KB Output is correct
23 Correct 2 ms 11100 KB Output is correct
24 Correct 2 ms 11024 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 2 ms 11100 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 11096 KB Output is correct
29 Correct 2 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 11060 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11060 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11352 KB Output is correct
11 Correct 2 ms 11100 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11100 KB Output is correct
15 Correct 2 ms 11068 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11096 KB Output is correct
19 Correct 2 ms 11100 KB Output is correct
20 Correct 2 ms 11100 KB Output is correct
21 Correct 3 ms 11096 KB Output is correct
22 Correct 2 ms 11100 KB Output is correct
23 Correct 2 ms 11100 KB Output is correct
24 Correct 2 ms 11024 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 2 ms 11100 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 11096 KB Output is correct
29 Correct 2 ms 11100 KB Output is correct
30 Correct 5 ms 11100 KB Output is correct
31 Correct 5 ms 11276 KB Output is correct
32 Correct 6 ms 11356 KB Output is correct
33 Correct 6 ms 11164 KB Output is correct
34 Correct 7 ms 11332 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 3 ms 11356 KB Output is correct
37 Correct 3 ms 11356 KB Output is correct
38 Correct 6 ms 11356 KB Output is correct
39 Correct 6 ms 11328 KB Output is correct
40 Correct 6 ms 11356 KB Output is correct
41 Correct 3 ms 11356 KB Output is correct
42 Correct 3 ms 11356 KB Output is correct
43 Correct 4 ms 11356 KB Output is correct
44 Correct 6 ms 11336 KB Output is correct
45 Correct 7 ms 11356 KB Output is correct
46 Correct 6 ms 11356 KB Output is correct
47 Correct 4 ms 11328 KB Output is correct
48 Correct 3 ms 11356 KB Output is correct
49 Correct 3 ms 11392 KB Output is correct
50 Correct 5 ms 11356 KB Output is correct
51 Correct 5 ms 11356 KB Output is correct
52 Correct 5 ms 11356 KB Output is correct
53 Correct 5 ms 11284 KB Output is correct
54 Correct 5 ms 11356 KB Output is correct
55 Correct 5 ms 11356 KB Output is correct
56 Correct 2 ms 9052 KB Output is correct
57 Correct 3 ms 11100 KB Output is correct
58 Correct 8 ms 11356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 306 ms 26176 KB Output is correct
5 Correct 222 ms 30284 KB Output is correct
6 Correct 303 ms 32196 KB Output is correct
7 Correct 389 ms 35304 KB Output is correct
8 Correct 397 ms 35524 KB Output is correct
9 Correct 405 ms 35524 KB Output is correct
10 Correct 401 ms 35240 KB Output is correct
11 Correct 401 ms 35640 KB Output is correct
12 Correct 84 ms 33984 KB Output is correct
13 Correct 84 ms 33980 KB Output is correct
14 Correct 88 ms 33980 KB Output is correct
15 Correct 45 ms 31044 KB Output is correct
16 Correct 548 ms 35392 KB Output is correct
17 Correct 57 ms 15036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 221 ms 18636 KB Output is correct
3 Correct 365 ms 19776 KB Output is correct
4 Correct 274 ms 22004 KB Output is correct
5 Correct 69 ms 25008 KB Output is correct
6 Correct 108 ms 25232 KB Output is correct
7 Correct 165 ms 25352 KB Output is correct
8 Correct 227 ms 25656 KB Output is correct
9 Correct 283 ms 25628 KB Output is correct
10 Correct 363 ms 25328 KB Output is correct
11 Correct 414 ms 25568 KB Output is correct
12 Correct 468 ms 25268 KB Output is correct
13 Correct 526 ms 26152 KB Output is correct
14 Correct 540 ms 26344 KB Output is correct
15 Correct 607 ms 29252 KB Output is correct
16 Correct 357 ms 25432 KB Output is correct
17 Correct 380 ms 25748 KB Output is correct
18 Correct 358 ms 26132 KB Output is correct
19 Correct 67 ms 24508 KB Output is correct
20 Correct 86 ms 25192 KB Output is correct
21 Correct 123 ms 25236 KB Output is correct
22 Correct 162 ms 24904 KB Output is correct
23 Correct 197 ms 24940 KB Output is correct
24 Correct 281 ms 24916 KB Output is correct
25 Correct 283 ms 24848 KB Output is correct
26 Correct 333 ms 24936 KB Output is correct
27 Correct 396 ms 24920 KB Output is correct
28 Correct 417 ms 24904 KB Output is correct
29 Correct 462 ms 25240 KB Output is correct
30 Correct 502 ms 25420 KB Output is correct
31 Correct 521 ms 25468 KB Output is correct
32 Correct 585 ms 26052 KB Output is correct
33 Correct 608 ms 27076 KB Output is correct
34 Correct 650 ms 29164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 3 ms 9076 KB Output is correct
4 Correct 360 ms 24228 KB Output is correct
5 Correct 368 ms 24496 KB Output is correct
6 Correct 440 ms 28848 KB Output is correct
7 Correct 440 ms 29976 KB Output is correct
8 Correct 447 ms 30384 KB Output is correct
9 Correct 445 ms 30048 KB Output is correct
10 Correct 439 ms 29808 KB Output is correct
11 Correct 453 ms 29872 KB Output is correct
12 Correct 447 ms 30200 KB Output is correct
13 Correct 444 ms 29820 KB Output is correct
14 Correct 46 ms 15056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 11060 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 2 ms 11096 KB Output is correct
6 Correct 2 ms 11060 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11352 KB Output is correct
11 Correct 2 ms 11100 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11100 KB Output is correct
15 Correct 2 ms 11068 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11096 KB Output is correct
19 Correct 2 ms 11100 KB Output is correct
20 Correct 2 ms 11100 KB Output is correct
21 Correct 3 ms 11096 KB Output is correct
22 Correct 2 ms 11100 KB Output is correct
23 Correct 2 ms 11100 KB Output is correct
24 Correct 2 ms 11024 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 2 ms 11100 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 11096 KB Output is correct
29 Correct 2 ms 11100 KB Output is correct
30 Correct 5 ms 11100 KB Output is correct
31 Correct 5 ms 11276 KB Output is correct
32 Correct 6 ms 11356 KB Output is correct
33 Correct 6 ms 11164 KB Output is correct
34 Correct 7 ms 11332 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 3 ms 11356 KB Output is correct
37 Correct 3 ms 11356 KB Output is correct
38 Correct 6 ms 11356 KB Output is correct
39 Correct 6 ms 11328 KB Output is correct
40 Correct 6 ms 11356 KB Output is correct
41 Correct 3 ms 11356 KB Output is correct
42 Correct 3 ms 11356 KB Output is correct
43 Correct 4 ms 11356 KB Output is correct
44 Correct 6 ms 11336 KB Output is correct
45 Correct 7 ms 11356 KB Output is correct
46 Correct 6 ms 11356 KB Output is correct
47 Correct 4 ms 11328 KB Output is correct
48 Correct 3 ms 11356 KB Output is correct
49 Correct 3 ms 11392 KB Output is correct
50 Correct 5 ms 11356 KB Output is correct
51 Correct 5 ms 11356 KB Output is correct
52 Correct 5 ms 11356 KB Output is correct
53 Correct 5 ms 11284 KB Output is correct
54 Correct 5 ms 11356 KB Output is correct
55 Correct 5 ms 11356 KB Output is correct
56 Correct 2 ms 9052 KB Output is correct
57 Correct 3 ms 11100 KB Output is correct
58 Correct 8 ms 11356 KB Output is correct
59 Correct 2 ms 10844 KB Output is correct
60 Correct 2 ms 8796 KB Output is correct
61 Correct 2 ms 9052 KB Output is correct
62 Correct 306 ms 26176 KB Output is correct
63 Correct 222 ms 30284 KB Output is correct
64 Correct 303 ms 32196 KB Output is correct
65 Correct 389 ms 35304 KB Output is correct
66 Correct 397 ms 35524 KB Output is correct
67 Correct 405 ms 35524 KB Output is correct
68 Correct 401 ms 35240 KB Output is correct
69 Correct 401 ms 35640 KB Output is correct
70 Correct 84 ms 33984 KB Output is correct
71 Correct 84 ms 33980 KB Output is correct
72 Correct 88 ms 33980 KB Output is correct
73 Correct 45 ms 31044 KB Output is correct
74 Correct 548 ms 35392 KB Output is correct
75 Correct 57 ms 15036 KB Output is correct
76 Correct 2 ms 10844 KB Output is correct
77 Correct 221 ms 18636 KB Output is correct
78 Correct 365 ms 19776 KB Output is correct
79 Correct 274 ms 22004 KB Output is correct
80 Correct 69 ms 25008 KB Output is correct
81 Correct 108 ms 25232 KB Output is correct
82 Correct 165 ms 25352 KB Output is correct
83 Correct 227 ms 25656 KB Output is correct
84 Correct 283 ms 25628 KB Output is correct
85 Correct 363 ms 25328 KB Output is correct
86 Correct 414 ms 25568 KB Output is correct
87 Correct 468 ms 25268 KB Output is correct
88 Correct 526 ms 26152 KB Output is correct
89 Correct 540 ms 26344 KB Output is correct
90 Correct 607 ms 29252 KB Output is correct
91 Correct 357 ms 25432 KB Output is correct
92 Correct 380 ms 25748 KB Output is correct
93 Correct 358 ms 26132 KB Output is correct
94 Correct 67 ms 24508 KB Output is correct
95 Correct 86 ms 25192 KB Output is correct
96 Correct 123 ms 25236 KB Output is correct
97 Correct 162 ms 24904 KB Output is correct
98 Correct 197 ms 24940 KB Output is correct
99 Correct 281 ms 24916 KB Output is correct
100 Correct 283 ms 24848 KB Output is correct
101 Correct 333 ms 24936 KB Output is correct
102 Correct 396 ms 24920 KB Output is correct
103 Correct 417 ms 24904 KB Output is correct
104 Correct 462 ms 25240 KB Output is correct
105 Correct 502 ms 25420 KB Output is correct
106 Correct 521 ms 25468 KB Output is correct
107 Correct 585 ms 26052 KB Output is correct
108 Correct 608 ms 27076 KB Output is correct
109 Correct 650 ms 29164 KB Output is correct
110 Correct 2 ms 10840 KB Output is correct
111 Correct 2 ms 8796 KB Output is correct
112 Correct 3 ms 9076 KB Output is correct
113 Correct 360 ms 24228 KB Output is correct
114 Correct 368 ms 24496 KB Output is correct
115 Correct 440 ms 28848 KB Output is correct
116 Correct 440 ms 29976 KB Output is correct
117 Correct 447 ms 30384 KB Output is correct
118 Correct 445 ms 30048 KB Output is correct
119 Correct 439 ms 29808 KB Output is correct
120 Correct 453 ms 29872 KB Output is correct
121 Correct 447 ms 30200 KB Output is correct
122 Correct 444 ms 29820 KB Output is correct
123 Correct 46 ms 15056 KB Output is correct
124 Correct 385 ms 29128 KB Output is correct
125 Correct 278 ms 25960 KB Output is correct
126 Correct 417 ms 29664 KB Output is correct
127 Correct 460 ms 29856 KB Output is correct
128 Correct 423 ms 29592 KB Output is correct
129 Correct 424 ms 29732 KB Output is correct
130 Correct 419 ms 29768 KB Output is correct
131 Correct 416 ms 34692 KB Output is correct
132 Correct 418 ms 35392 KB Output is correct
133 Correct 412 ms 34024 KB Output is correct
134 Correct 455 ms 29088 KB Output is correct
135 Correct 443 ms 29456 KB Output is correct
136 Correct 432 ms 29140 KB Output is correct
137 Correct 238 ms 29148 KB Output is correct
138 Correct 233 ms 28924 KB Output is correct
139 Correct 225 ms 29076 KB Output is correct
140 Correct 278 ms 28884 KB Output is correct
141 Correct 232 ms 29080 KB Output is correct
142 Correct 233 ms 28972 KB Output is correct
143 Correct 48 ms 24888 KB Output is correct
144 Correct 618 ms 29252 KB Output is correct