Submission #767732

# Submission time Handle Problem Language Result Execution time Memory
767732 2023-06-27T06:07:40 Z PurpleCrayon Tourism (JOI23_tourism) C++17
100 / 100
668 ms 61844 KB
#include <bits/stdc++.h>
using namespace std;
 
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
const int L = 19;

struct FT {
    vector<int> bit;
    FT(int n): bit(n) {}

    void upd(int i, int x) {
        for (; i < sz(bit); i |= i+1) bit[i] += x;
    }

    int qry(int i) {
        int ans = 0;
        for (++i; i > 0; i &= i-1) ans += bit[i-1];
        return ans;
    }
};

int n, m, q, st[N], en[N], depth[N], tt = 0;
vector<int> adj[N];

vector<int> lca_et;
int sparse[2 * N][L], st_lca[N], en_lca[N];

int dep_min(int a, int b) {
    return depth[a] < depth[b] ? a : b;
}

void build_rmq() {
    int k = sz(lca_et);
    for (int i = 0; i < k; i++) sparse[i][0] = lca_et[i];
    for (int c = 2, l = 1; c <= k; c *= 2, l++) {
        for (int i = 0; i + c <= k; i++) {
            sparse[i][l] = dep_min(sparse[i][l-1], sparse[i + c/2][l-1]);
        }
    }
}

int rmq_qry(int l, int r) {
    int lg = 31 - __builtin_clz(r - l + 1);
    int len = 1 << lg;
    return dep_min(sparse[l][lg], sparse[r - len + 1][lg]);
}

int lca(int a, int b) {
    if (st_lca[a] > st_lca[b]) swap(a, b);
    return rmq_qry(st_lca[a], en_lca[b]);
}

void dfs(int c, int p) {
    if (p != -1) depth[c] = depth[p] + 1;
    st[c] = tt++;

    st_lca[c] = en_lca[c] = sz(lca_et);
    lca_et.push_back(c);
    for (int nxt : adj[c]) if (nxt != p) {
        dfs(nxt, c);

        en_lca[c] = sz(lca_et);
        lca_et.push_back(c);
    }

    en[c] = tt-1;
}

int ans[N], c[N], lca_l[N], lca_r[N];
int rev[N], par[N], w[N];
pair<int, int> range[N];
vector<int> aux[N];
vector<pair<int, int>> ev[N];

void dfs_aux_range(int c) {
    for (int nxt : aux[c]) {
        dfs_aux_range(nxt);
        range[c].first = max(range[c].first, range[nxt].first);
        range[c].second = min(range[c].second, range[nxt].second);
    }
}

void rec(int l, int r, vector<ar<int, 3>> qs) {
    if (!sz(qs)) return;
    if (r <= l) return;

    int m = (l + r) / 2; // [l..m], [m+1..r]
    vector<ar<int, 3>> one, two, mine;
    for (auto qi : qs) {
        if (qi[1] <= m)
            one.push_back(qi);
        else if (qi[0] > m)
            two.push_back(qi);
        else
            mine.push_back(qi);
    }

    rec(l, m, one), rec(m+1, r, two);

    vector<int> use;
    for (int i = l; i <= r; i++) use.push_back(c[i]);

    sort(use.begin(), use.end(), [&](int x, int y) {
        return st[x] < st[y];
    });

    use.resize(unique(use.begin(), use.end()) - use.begin());
    int base = sz(use);
    for (int i = 0; i < base; i++) {
        use.push_back(lca(use[i], use[(i+1) % base]));
    }

    sort(use.begin(), use.end(), [&](int x, int y) {
        return st[x] < st[y];
    });

    use.resize(unique(use.begin(), use.end()) - use.begin());
    for (int i = 0; i < sz(use); i++) {
        rev[use[i]] = i;
        aux[i].clear();
    }

    for (int i = 1; i < sz(use); i++) {
        par[i] = rev[lca(use[i-1], use[i])];
        aux[par[i]].push_back(i);
        w[i] = depth[use[i]] - depth[use[par[i]]];
    }
    
    int k = sz(use);
    // max index of something on the left, min index of something on the right
    for (int i = 0; i < k; i++) {
        range[i] = {-1, MOD};
    }

    for (int i = l; i <= m; i++) {
        int cur = rev[c[i]];
        range[cur].first = max(range[cur].first, i);
    }

    for (int i = m+1; i <= r; i++) {
        int cur = rev[c[i]];
        range[cur].second = min(range[cur].second, i);
    }

    dfs_aux_range(0);
    for (int i = m; i >= l; i--) {
        lca_l[i] = c[i];
        if (i < m) lca_l[i] = lca(lca_l[i], lca_l[i+1]);
    }

    for (int i = m+1; i <= r; i++) {
        lca_r[i] = c[i];
        if (i > m+1) lca_r[i] = lca(lca_r[i], lca_r[i-1]);
    }

    int sum = 0;
    for (int j = 1; j < k; j++) sum += w[j];
    int top = use[0];

    for (int i = l; i <= r; i++) ev[i].clear();

    FT bit(r - l + 1);
    auto add = [&](int L, int R, int x) {
        if (R < L) return;
        L -= l;
        R -= l;
        bit.upd(L, +x);
        bit.upd(R+1, -x);
    };

    auto qry = [&](int i) {
        return bit.qry(i - l);
    };

    for (int j = 1; j < k; j++) {
        if (range[j].first < l) {
            ev[l].push_back({range[j].second, -w[j]});
        }
    }

    for (auto [L, R, i] : mine) {
        ans[i] += sum;
        ans[i] -= depth[lca(lca_l[L], lca_r[R])] - depth[top];

        ev[L].push_back({R, i});

        /*
        for (int j = 1; j < k; j++) {
            if (range[j].first < L && R < range[j].second) {
                ans[i] -= w[j];
            }
        }
        */
    }

    for (int j = 1; j < k; j++) {
        if (range[j].first >= l) {
            ev[range[j].first].push_back({range[j].second, -w[j]});
        }
    }

    for (int i = l; i <= r; i++) {
        for (auto [R, x] : ev[i]) {
            if (x >= 0) {
                ans[x] += qry(R);
            } else {
                x *= -1;
                add(l, min(r, R-1), -x);
            }
        }
    }
}

void solve() {
    cin >> n >> m >> q;
    for (int i = 0; i < n-1; i++) {
        int a, b; cin >> a >> b, --a, --b;
        adj[a].push_back(b), adj[b].push_back(a);
    }

    dfs(0, -1);
    build_rmq();

    for (int i = 0; i < m; i++) {
        cin >> c[i], --c[i];
    }

    vector<ar<int, 3>> qs(q);
    for (int i = 0; i < q; i++) {
        int l, r; cin >> l >> r, --l, --r;
        qs[i] = {l, r, i};
    }

    rec(0, m-1, qs);

    for (int i = 0; i < q; i++) cout << ans[i]+1 << '\n';
}
 
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 3 ms 7508 KB Output is correct
8 Correct 3 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 3 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 3 ms 7508 KB Output is correct
15 Correct 3 ms 7508 KB Output is correct
16 Correct 4 ms 7508 KB Output is correct
17 Correct 4 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 4 ms 7520 KB Output is correct
20 Correct 3 ms 7508 KB Output is correct
21 Correct 4 ms 7552 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 5 ms 7484 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 3 ms 7508 KB Output is correct
8 Correct 3 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 3 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 3 ms 7508 KB Output is correct
15 Correct 3 ms 7508 KB Output is correct
16 Correct 4 ms 7508 KB Output is correct
17 Correct 4 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 4 ms 7520 KB Output is correct
20 Correct 3 ms 7508 KB Output is correct
21 Correct 4 ms 7552 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 5 ms 7484 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 6 ms 7896 KB Output is correct
31 Correct 7 ms 7896 KB Output is correct
32 Correct 7 ms 8164 KB Output is correct
33 Correct 7 ms 8132 KB Output is correct
34 Correct 8 ms 8140 KB Output is correct
35 Correct 6 ms 8020 KB Output is correct
36 Correct 7 ms 8040 KB Output is correct
37 Correct 5 ms 8020 KB Output is correct
38 Correct 7 ms 8236 KB Output is correct
39 Correct 7 ms 8296 KB Output is correct
40 Correct 7 ms 8276 KB Output is correct
41 Correct 5 ms 8148 KB Output is correct
42 Correct 5 ms 8220 KB Output is correct
43 Correct 5 ms 8148 KB Output is correct
44 Correct 7 ms 8148 KB Output is correct
45 Correct 9 ms 8212 KB Output is correct
46 Correct 7 ms 8276 KB Output is correct
47 Correct 5 ms 8020 KB Output is correct
48 Correct 5 ms 8020 KB Output is correct
49 Correct 5 ms 8020 KB Output is correct
50 Correct 6 ms 8128 KB Output is correct
51 Correct 6 ms 8152 KB Output is correct
52 Correct 6 ms 8148 KB Output is correct
53 Correct 8 ms 8148 KB Output is correct
54 Correct 6 ms 8116 KB Output is correct
55 Correct 6 ms 8148 KB Output is correct
56 Correct 4 ms 7508 KB Output is correct
57 Correct 4 ms 7800 KB Output is correct
58 Correct 9 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7560 KB Output is correct
4 Correct 274 ms 39156 KB Output is correct
5 Correct 229 ms 42688 KB Output is correct
6 Correct 286 ms 48780 KB Output is correct
7 Correct 363 ms 55872 KB Output is correct
8 Correct 372 ms 55896 KB Output is correct
9 Correct 370 ms 56124 KB Output is correct
10 Correct 371 ms 56016 KB Output is correct
11 Correct 365 ms 55888 KB Output is correct
12 Correct 98 ms 46888 KB Output is correct
13 Correct 96 ms 46908 KB Output is correct
14 Correct 98 ms 46956 KB Output is correct
15 Correct 54 ms 38376 KB Output is correct
16 Correct 510 ms 61844 KB Output is correct
17 Correct 62 ms 15168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 208 ms 26180 KB Output is correct
3 Correct 354 ms 31272 KB Output is correct
4 Correct 268 ms 31284 KB Output is correct
5 Correct 92 ms 33616 KB Output is correct
6 Correct 144 ms 35492 KB Output is correct
7 Correct 195 ms 37240 KB Output is correct
8 Correct 256 ms 38376 KB Output is correct
9 Correct 334 ms 39504 KB Output is correct
10 Correct 390 ms 40312 KB Output is correct
11 Correct 457 ms 41212 KB Output is correct
12 Correct 471 ms 42044 KB Output is correct
13 Correct 529 ms 43072 KB Output is correct
14 Correct 590 ms 44824 KB Output is correct
15 Correct 668 ms 48812 KB Output is correct
16 Correct 419 ms 41104 KB Output is correct
17 Correct 436 ms 41112 KB Output is correct
18 Correct 433 ms 40984 KB Output is correct
19 Correct 99 ms 33992 KB Output is correct
20 Correct 125 ms 34500 KB Output is correct
21 Correct 179 ms 35776 KB Output is correct
22 Correct 194 ms 36856 KB Output is correct
23 Correct 214 ms 37824 KB Output is correct
24 Correct 259 ms 39328 KB Output is correct
25 Correct 291 ms 40320 KB Output is correct
26 Correct 337 ms 41372 KB Output is correct
27 Correct 379 ms 42760 KB Output is correct
28 Correct 399 ms 43536 KB Output is correct
29 Correct 452 ms 44488 KB Output is correct
30 Correct 480 ms 45504 KB Output is correct
31 Correct 508 ms 46076 KB Output is correct
32 Correct 553 ms 46844 KB Output is correct
33 Correct 596 ms 48024 KB Output is correct
34 Correct 614 ms 50988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7508 KB Output is correct
4 Correct 327 ms 35284 KB Output is correct
5 Correct 333 ms 36024 KB Output is correct
6 Correct 397 ms 43284 KB Output is correct
7 Correct 428 ms 49484 KB Output is correct
8 Correct 438 ms 49552 KB Output is correct
9 Correct 453 ms 49976 KB Output is correct
10 Correct 459 ms 49768 KB Output is correct
11 Correct 433 ms 49860 KB Output is correct
12 Correct 450 ms 49976 KB Output is correct
13 Correct 439 ms 49904 KB Output is correct
14 Correct 68 ms 16604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 3 ms 7508 KB Output is correct
8 Correct 3 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 3 ms 7508 KB Output is correct
13 Correct 4 ms 7508 KB Output is correct
14 Correct 3 ms 7508 KB Output is correct
15 Correct 3 ms 7508 KB Output is correct
16 Correct 4 ms 7508 KB Output is correct
17 Correct 4 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 4 ms 7520 KB Output is correct
20 Correct 3 ms 7508 KB Output is correct
21 Correct 4 ms 7552 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 5 ms 7484 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 4 ms 7508 KB Output is correct
30 Correct 6 ms 7896 KB Output is correct
31 Correct 7 ms 7896 KB Output is correct
32 Correct 7 ms 8164 KB Output is correct
33 Correct 7 ms 8132 KB Output is correct
34 Correct 8 ms 8140 KB Output is correct
35 Correct 6 ms 8020 KB Output is correct
36 Correct 7 ms 8040 KB Output is correct
37 Correct 5 ms 8020 KB Output is correct
38 Correct 7 ms 8236 KB Output is correct
39 Correct 7 ms 8296 KB Output is correct
40 Correct 7 ms 8276 KB Output is correct
41 Correct 5 ms 8148 KB Output is correct
42 Correct 5 ms 8220 KB Output is correct
43 Correct 5 ms 8148 KB Output is correct
44 Correct 7 ms 8148 KB Output is correct
45 Correct 9 ms 8212 KB Output is correct
46 Correct 7 ms 8276 KB Output is correct
47 Correct 5 ms 8020 KB Output is correct
48 Correct 5 ms 8020 KB Output is correct
49 Correct 5 ms 8020 KB Output is correct
50 Correct 6 ms 8128 KB Output is correct
51 Correct 6 ms 8152 KB Output is correct
52 Correct 6 ms 8148 KB Output is correct
53 Correct 8 ms 8148 KB Output is correct
54 Correct 6 ms 8116 KB Output is correct
55 Correct 6 ms 8148 KB Output is correct
56 Correct 4 ms 7508 KB Output is correct
57 Correct 4 ms 7800 KB Output is correct
58 Correct 9 ms 8148 KB Output is correct
59 Correct 3 ms 7380 KB Output is correct
60 Correct 3 ms 7380 KB Output is correct
61 Correct 4 ms 7560 KB Output is correct
62 Correct 274 ms 39156 KB Output is correct
63 Correct 229 ms 42688 KB Output is correct
64 Correct 286 ms 48780 KB Output is correct
65 Correct 363 ms 55872 KB Output is correct
66 Correct 372 ms 55896 KB Output is correct
67 Correct 370 ms 56124 KB Output is correct
68 Correct 371 ms 56016 KB Output is correct
69 Correct 365 ms 55888 KB Output is correct
70 Correct 98 ms 46888 KB Output is correct
71 Correct 96 ms 46908 KB Output is correct
72 Correct 98 ms 46956 KB Output is correct
73 Correct 54 ms 38376 KB Output is correct
74 Correct 510 ms 61844 KB Output is correct
75 Correct 62 ms 15168 KB Output is correct
76 Correct 3 ms 7380 KB Output is correct
77 Correct 208 ms 26180 KB Output is correct
78 Correct 354 ms 31272 KB Output is correct
79 Correct 268 ms 31284 KB Output is correct
80 Correct 92 ms 33616 KB Output is correct
81 Correct 144 ms 35492 KB Output is correct
82 Correct 195 ms 37240 KB Output is correct
83 Correct 256 ms 38376 KB Output is correct
84 Correct 334 ms 39504 KB Output is correct
85 Correct 390 ms 40312 KB Output is correct
86 Correct 457 ms 41212 KB Output is correct
87 Correct 471 ms 42044 KB Output is correct
88 Correct 529 ms 43072 KB Output is correct
89 Correct 590 ms 44824 KB Output is correct
90 Correct 668 ms 48812 KB Output is correct
91 Correct 419 ms 41104 KB Output is correct
92 Correct 436 ms 41112 KB Output is correct
93 Correct 433 ms 40984 KB Output is correct
94 Correct 99 ms 33992 KB Output is correct
95 Correct 125 ms 34500 KB Output is correct
96 Correct 179 ms 35776 KB Output is correct
97 Correct 194 ms 36856 KB Output is correct
98 Correct 214 ms 37824 KB Output is correct
99 Correct 259 ms 39328 KB Output is correct
100 Correct 291 ms 40320 KB Output is correct
101 Correct 337 ms 41372 KB Output is correct
102 Correct 379 ms 42760 KB Output is correct
103 Correct 399 ms 43536 KB Output is correct
104 Correct 452 ms 44488 KB Output is correct
105 Correct 480 ms 45504 KB Output is correct
106 Correct 508 ms 46076 KB Output is correct
107 Correct 553 ms 46844 KB Output is correct
108 Correct 596 ms 48024 KB Output is correct
109 Correct 614 ms 50988 KB Output is correct
110 Correct 3 ms 7380 KB Output is correct
111 Correct 4 ms 7380 KB Output is correct
112 Correct 4 ms 7508 KB Output is correct
113 Correct 327 ms 35284 KB Output is correct
114 Correct 333 ms 36024 KB Output is correct
115 Correct 397 ms 43284 KB Output is correct
116 Correct 428 ms 49484 KB Output is correct
117 Correct 438 ms 49552 KB Output is correct
118 Correct 453 ms 49976 KB Output is correct
119 Correct 459 ms 49768 KB Output is correct
120 Correct 433 ms 49860 KB Output is correct
121 Correct 450 ms 49976 KB Output is correct
122 Correct 439 ms 49904 KB Output is correct
123 Correct 68 ms 16604 KB Output is correct
124 Correct 385 ms 46580 KB Output is correct
125 Correct 284 ms 41208 KB Output is correct
126 Correct 433 ms 48352 KB Output is correct
127 Correct 437 ms 48704 KB Output is correct
128 Correct 442 ms 48732 KB Output is correct
129 Correct 449 ms 48372 KB Output is correct
130 Correct 450 ms 48380 KB Output is correct
131 Correct 375 ms 57916 KB Output is correct
132 Correct 384 ms 58760 KB Output is correct
133 Correct 388 ms 55152 KB Output is correct
134 Correct 414 ms 50376 KB Output is correct
135 Correct 411 ms 50272 KB Output is correct
136 Correct 422 ms 50240 KB Output is correct
137 Correct 288 ms 46576 KB Output is correct
138 Correct 307 ms 46552 KB Output is correct
139 Correct 290 ms 45980 KB Output is correct
140 Correct 299 ms 46360 KB Output is correct
141 Correct 302 ms 45956 KB Output is correct
142 Correct 337 ms 45840 KB Output is correct
143 Correct 60 ms 32168 KB Output is correct
144 Correct 646 ms 51524 KB Output is correct