Submission #896342

# Submission time Handle Problem Language Result Execution time Memory
896342 2024-01-01T09:44:44 Z CDuong Tourism (JOI23_tourism) C++17
100 / 100
577 ms 25004 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 1e5 + 5;
const int mxM = 1e5 + 5;
const int mxQ = 1e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

struct FenwickTree {
    int n;
    vector<int> data;

    FenwickTree(int n) : n(n), data(n + 1) {}

    void update(int idx, int val) {
        // cout << idx << " " << val << "\n";
        for (; idx <= n; idx += idx & -idx)
            data[idx] += val;
    }

    int get(int idx) {
        int res = 0;
        for (; idx > 0; idx -= idx & -idx)
            res += data[idx];
        return res;
    }
};

vector<int> G[mxN];
int n, m, q, ss[mxM];
vector<pair<int, int>> queries[mxQ];
int ans[mxQ];

int tdfs, sz[mxN], tin[mxN], head[mxN], tout[mxN], par[mxN], dep[mxN];
set<pair<int, int>> s;

void dfs(int v, int p) {
    sz[v] = 1;
    for (int &u : G[v]) if (u != p) {
        dfs(u, v);
        if (G[v][0] == p or sz[u] > sz[G[v][0]])
            swap(u, G[v][0]);
    }
}

void dfs_hld(int v, int p, int top) {
    tin[v] = ++tdfs;
    head[v] = top;
    par[v] = p;
    dep[v] = dep[p] + 1;

    if (not G[v].empty() and G[v][0] != p)
        dfs_hld(G[v][0], v, top);

    for (int i = 1; i < isz(G[v]); ++i) if (G[v][i] != p)
        dfs_hld(G[v][i], v, G[v][i]);

    tout[v] = tdfs;
}

void solve() {
    cin >> n >> m >> q;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    for (int i = 1; i <= m; ++i) {
        cin >> ss[i];
    }
    for (int i = 1; i <= q; ++i) {
        int l, r;
        cin >> l >> r;
        queries[l].emplace_back(r, i);
    }

    dfs(1, 0);
    dfs_hld(1, 0, 1);

    FenwickTree fenw(m + 1);
    s.insert({2, m + 1}); s.insert({n + 1, -1}); fenw.update(m + 1, n - 1);

    auto del = [&](int l, int r) -> void {
        // cout << l << " -> " << r << endl;
        auto it = prev(s.upper_bound({l, INT_MAX}));
        do {
            // assert(it != s.end());
            int cl = it->ff, cr = next(it)->ff - 1, val = it->ss, len = min(cr, r) - max(cl, l) + 1;
            // cout << len << endl;
            it = s.erase(it);
            if (cl < l) s.insert({cl, val});
            if (r < cr) s.insert({r + 1, val});
            fenw.update(val, -len);
        } while (it->ff <= r);
        // cout << endl;
    };

    auto add = [&](int l, int r, int val) -> void {
        fenw.update(val, r - l + 1);
        s.insert({l, val});
        // cout << endl;
    };

    auto update_hld = [&](int u, int v, int val) -> void {
        // cout << u << " " << v << " " << val << endl;
        while (head[u] != head[v]) {
            // cout << head[u] << " " << head[v] << endl;
            if (dep[head[u]] < dep[head[v]])
                swap(u, v);
            // tin[head[u]] -> tin[u]
            del(tin[head[u]], tin[u]);
            add(tin[head[u]], tin[u], val);
            u = par[head[u]];
        }
        if (u != v) {
            if (dep[u] > dep[v]) swap(u, v);
            // tin[u] + 1 -> tin[v]
            del(tin[u] + 1, tin[v]);
            add(tin[u] + 1, tin[v], val);
        }
    };

    for (int L = m; L >= 1; --L) {
        // cout << L << endl;
        if (L != m) update_hld(ss[L], ss[L + 1], L + 1);
        for (auto [R, idx] : queries[L]) {
            ans[idx] = fenw.get(R) + 1;
            // cout << idx << " " << fenw.get(R) + 1 << "\n";
        }
    }

    for (int i = 1; i <= q; ++i) cout << ans[i] << "\n";
}

signed main() {

#ifndef CDuongg
    if (fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
   auto end = chrono::high_resolution_clock::now();
   cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
   cout << "Check array size pls sir" << endl;
#endif

}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8112 KB Output is correct
2 Correct 2 ms 8024 KB Output is correct
3 Correct 2 ms 8032 KB Output is correct
4 Correct 2 ms 8024 KB Output is correct
5 Correct 3 ms 8024 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8112 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 3 ms 8028 KB Output is correct
11 Correct 3 ms 8036 KB Output is correct
12 Correct 2 ms 8024 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 2 ms 8024 KB Output is correct
15 Correct 2 ms 8028 KB Output is correct
16 Correct 3 ms 8028 KB Output is correct
17 Correct 3 ms 8024 KB Output is correct
18 Correct 2 ms 8028 KB Output is correct
19 Correct 2 ms 8024 KB Output is correct
20 Correct 3 ms 8028 KB Output is correct
21 Correct 3 ms 8028 KB Output is correct
22 Correct 2 ms 8408 KB Output is correct
23 Correct 3 ms 8112 KB Output is correct
24 Correct 3 ms 8280 KB Output is correct
25 Correct 2 ms 8028 KB Output is correct
26 Correct 2 ms 8028 KB Output is correct
27 Correct 2 ms 8028 KB Output is correct
28 Correct 2 ms 8028 KB Output is correct
29 Correct 2 ms 8028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8112 KB Output is correct
2 Correct 2 ms 8024 KB Output is correct
3 Correct 2 ms 8032 KB Output is correct
4 Correct 2 ms 8024 KB Output is correct
5 Correct 3 ms 8024 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8112 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 3 ms 8028 KB Output is correct
11 Correct 3 ms 8036 KB Output is correct
12 Correct 2 ms 8024 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 2 ms 8024 KB Output is correct
15 Correct 2 ms 8028 KB Output is correct
16 Correct 3 ms 8028 KB Output is correct
17 Correct 3 ms 8024 KB Output is correct
18 Correct 2 ms 8028 KB Output is correct
19 Correct 2 ms 8024 KB Output is correct
20 Correct 3 ms 8028 KB Output is correct
21 Correct 3 ms 8028 KB Output is correct
22 Correct 2 ms 8408 KB Output is correct
23 Correct 3 ms 8112 KB Output is correct
24 Correct 3 ms 8280 KB Output is correct
25 Correct 2 ms 8028 KB Output is correct
26 Correct 2 ms 8028 KB Output is correct
27 Correct 2 ms 8028 KB Output is correct
28 Correct 2 ms 8028 KB Output is correct
29 Correct 2 ms 8028 KB Output is correct
30 Correct 5 ms 8284 KB Output is correct
31 Correct 5 ms 8284 KB Output is correct
32 Correct 6 ms 8280 KB Output is correct
33 Correct 7 ms 8348 KB Output is correct
34 Correct 6 ms 8284 KB Output is correct
35 Correct 6 ms 8284 KB Output is correct
36 Correct 7 ms 8124 KB Output is correct
37 Correct 6 ms 8260 KB Output is correct
38 Correct 4 ms 8280 KB Output is correct
39 Correct 4 ms 8284 KB Output is correct
40 Correct 4 ms 8280 KB Output is correct
41 Correct 4 ms 8372 KB Output is correct
42 Correct 3 ms 8284 KB Output is correct
43 Correct 3 ms 8292 KB Output is correct
44 Correct 6 ms 8296 KB Output is correct
45 Correct 6 ms 8296 KB Output is correct
46 Correct 5 ms 8296 KB Output is correct
47 Correct 4 ms 8224 KB Output is correct
48 Correct 5 ms 8296 KB Output is correct
49 Correct 4 ms 8140 KB Output is correct
50 Correct 4 ms 8296 KB Output is correct
51 Correct 4 ms 8296 KB Output is correct
52 Correct 6 ms 8296 KB Output is correct
53 Correct 4 ms 8296 KB Output is correct
54 Correct 4 ms 8296 KB Output is correct
55 Correct 4 ms 8360 KB Output is correct
56 Correct 3 ms 8040 KB Output is correct
57 Correct 3 ms 8040 KB Output is correct
58 Correct 6 ms 8232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8028 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 3 ms 8168 KB Output is correct
4 Correct 72 ms 18568 KB Output is correct
5 Correct 75 ms 20944 KB Output is correct
6 Correct 68 ms 22248 KB Output is correct
7 Correct 86 ms 25004 KB Output is correct
8 Correct 85 ms 24992 KB Output is correct
9 Correct 98 ms 24904 KB Output is correct
10 Correct 94 ms 24912 KB Output is correct
11 Correct 90 ms 24996 KB Output is correct
12 Correct 80 ms 24404 KB Output is correct
13 Correct 76 ms 24400 KB Output is correct
14 Correct 85 ms 24376 KB Output is correct
15 Correct 43 ms 21712 KB Output is correct
16 Correct 108 ms 24660 KB Output is correct
17 Correct 35 ms 12120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 195 ms 13156 KB Output is correct
3 Correct 308 ms 13776 KB Output is correct
4 Correct 236 ms 14160 KB Output is correct
5 Correct 427 ms 16796 KB Output is correct
6 Correct 431 ms 16736 KB Output is correct
7 Correct 431 ms 16952 KB Output is correct
8 Correct 439 ms 16808 KB Output is correct
9 Correct 425 ms 16792 KB Output is correct
10 Correct 431 ms 16688 KB Output is correct
11 Correct 417 ms 16792 KB Output is correct
12 Correct 379 ms 17236 KB Output is correct
13 Correct 351 ms 17344 KB Output is correct
14 Correct 387 ms 18288 KB Output is correct
15 Correct 444 ms 21328 KB Output is correct
16 Correct 432 ms 17476 KB Output is correct
17 Correct 442 ms 17236 KB Output is correct
18 Correct 428 ms 17200 KB Output is correct
19 Correct 318 ms 13912 KB Output is correct
20 Correct 282 ms 13884 KB Output is correct
21 Correct 276 ms 13764 KB Output is correct
22 Correct 300 ms 14152 KB Output is correct
23 Correct 284 ms 13772 KB Output is correct
24 Correct 279 ms 13880 KB Output is correct
25 Correct 323 ms 13824 KB Output is correct
26 Correct 280 ms 13892 KB Output is correct
27 Correct 278 ms 14040 KB Output is correct
28 Correct 283 ms 13856 KB Output is correct
29 Correct 321 ms 13996 KB Output is correct
30 Correct 263 ms 14128 KB Output is correct
31 Correct 293 ms 14320 KB Output is correct
32 Correct 314 ms 14752 KB Output is correct
33 Correct 310 ms 15952 KB Output is correct
34 Correct 282 ms 18456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 425 ms 17040 KB Output is correct
5 Correct 437 ms 17148 KB Output is correct
6 Correct 526 ms 19536 KB Output is correct
7 Correct 565 ms 20952 KB Output is correct
8 Correct 529 ms 20696 KB Output is correct
9 Correct 539 ms 20628 KB Output is correct
10 Correct 552 ms 20600 KB Output is correct
11 Correct 577 ms 20812 KB Output is correct
12 Correct 543 ms 20776 KB Output is correct
13 Correct 563 ms 20992 KB Output is correct
14 Correct 30 ms 12000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8112 KB Output is correct
2 Correct 2 ms 8024 KB Output is correct
3 Correct 2 ms 8032 KB Output is correct
4 Correct 2 ms 8024 KB Output is correct
5 Correct 3 ms 8024 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8112 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 3 ms 8028 KB Output is correct
11 Correct 3 ms 8036 KB Output is correct
12 Correct 2 ms 8024 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 2 ms 8024 KB Output is correct
15 Correct 2 ms 8028 KB Output is correct
16 Correct 3 ms 8028 KB Output is correct
17 Correct 3 ms 8024 KB Output is correct
18 Correct 2 ms 8028 KB Output is correct
19 Correct 2 ms 8024 KB Output is correct
20 Correct 3 ms 8028 KB Output is correct
21 Correct 3 ms 8028 KB Output is correct
22 Correct 2 ms 8408 KB Output is correct
23 Correct 3 ms 8112 KB Output is correct
24 Correct 3 ms 8280 KB Output is correct
25 Correct 2 ms 8028 KB Output is correct
26 Correct 2 ms 8028 KB Output is correct
27 Correct 2 ms 8028 KB Output is correct
28 Correct 2 ms 8028 KB Output is correct
29 Correct 2 ms 8028 KB Output is correct
30 Correct 5 ms 8284 KB Output is correct
31 Correct 5 ms 8284 KB Output is correct
32 Correct 6 ms 8280 KB Output is correct
33 Correct 7 ms 8348 KB Output is correct
34 Correct 6 ms 8284 KB Output is correct
35 Correct 6 ms 8284 KB Output is correct
36 Correct 7 ms 8124 KB Output is correct
37 Correct 6 ms 8260 KB Output is correct
38 Correct 4 ms 8280 KB Output is correct
39 Correct 4 ms 8284 KB Output is correct
40 Correct 4 ms 8280 KB Output is correct
41 Correct 4 ms 8372 KB Output is correct
42 Correct 3 ms 8284 KB Output is correct
43 Correct 3 ms 8292 KB Output is correct
44 Correct 6 ms 8296 KB Output is correct
45 Correct 6 ms 8296 KB Output is correct
46 Correct 5 ms 8296 KB Output is correct
47 Correct 4 ms 8224 KB Output is correct
48 Correct 5 ms 8296 KB Output is correct
49 Correct 4 ms 8140 KB Output is correct
50 Correct 4 ms 8296 KB Output is correct
51 Correct 4 ms 8296 KB Output is correct
52 Correct 6 ms 8296 KB Output is correct
53 Correct 4 ms 8296 KB Output is correct
54 Correct 4 ms 8296 KB Output is correct
55 Correct 4 ms 8360 KB Output is correct
56 Correct 3 ms 8040 KB Output is correct
57 Correct 3 ms 8040 KB Output is correct
58 Correct 6 ms 8232 KB Output is correct
59 Correct 3 ms 8028 KB Output is correct
60 Correct 2 ms 8028 KB Output is correct
61 Correct 3 ms 8168 KB Output is correct
62 Correct 72 ms 18568 KB Output is correct
63 Correct 75 ms 20944 KB Output is correct
64 Correct 68 ms 22248 KB Output is correct
65 Correct 86 ms 25004 KB Output is correct
66 Correct 85 ms 24992 KB Output is correct
67 Correct 98 ms 24904 KB Output is correct
68 Correct 94 ms 24912 KB Output is correct
69 Correct 90 ms 24996 KB Output is correct
70 Correct 80 ms 24404 KB Output is correct
71 Correct 76 ms 24400 KB Output is correct
72 Correct 85 ms 24376 KB Output is correct
73 Correct 43 ms 21712 KB Output is correct
74 Correct 108 ms 24660 KB Output is correct
75 Correct 35 ms 12120 KB Output is correct
76 Correct 2 ms 8028 KB Output is correct
77 Correct 195 ms 13156 KB Output is correct
78 Correct 308 ms 13776 KB Output is correct
79 Correct 236 ms 14160 KB Output is correct
80 Correct 427 ms 16796 KB Output is correct
81 Correct 431 ms 16736 KB Output is correct
82 Correct 431 ms 16952 KB Output is correct
83 Correct 439 ms 16808 KB Output is correct
84 Correct 425 ms 16792 KB Output is correct
85 Correct 431 ms 16688 KB Output is correct
86 Correct 417 ms 16792 KB Output is correct
87 Correct 379 ms 17236 KB Output is correct
88 Correct 351 ms 17344 KB Output is correct
89 Correct 387 ms 18288 KB Output is correct
90 Correct 444 ms 21328 KB Output is correct
91 Correct 432 ms 17476 KB Output is correct
92 Correct 442 ms 17236 KB Output is correct
93 Correct 428 ms 17200 KB Output is correct
94 Correct 318 ms 13912 KB Output is correct
95 Correct 282 ms 13884 KB Output is correct
96 Correct 276 ms 13764 KB Output is correct
97 Correct 300 ms 14152 KB Output is correct
98 Correct 284 ms 13772 KB Output is correct
99 Correct 279 ms 13880 KB Output is correct
100 Correct 323 ms 13824 KB Output is correct
101 Correct 280 ms 13892 KB Output is correct
102 Correct 278 ms 14040 KB Output is correct
103 Correct 283 ms 13856 KB Output is correct
104 Correct 321 ms 13996 KB Output is correct
105 Correct 263 ms 14128 KB Output is correct
106 Correct 293 ms 14320 KB Output is correct
107 Correct 314 ms 14752 KB Output is correct
108 Correct 310 ms 15952 KB Output is correct
109 Correct 282 ms 18456 KB Output is correct
110 Correct 2 ms 8028 KB Output is correct
111 Correct 2 ms 8028 KB Output is correct
112 Correct 3 ms 8028 KB Output is correct
113 Correct 425 ms 17040 KB Output is correct
114 Correct 437 ms 17148 KB Output is correct
115 Correct 526 ms 19536 KB Output is correct
116 Correct 565 ms 20952 KB Output is correct
117 Correct 529 ms 20696 KB Output is correct
118 Correct 539 ms 20628 KB Output is correct
119 Correct 552 ms 20600 KB Output is correct
120 Correct 577 ms 20812 KB Output is correct
121 Correct 543 ms 20776 KB Output is correct
122 Correct 563 ms 20992 KB Output is correct
123 Correct 30 ms 12000 KB Output is correct
124 Correct 372 ms 20052 KB Output is correct
125 Correct 262 ms 18004 KB Output is correct
126 Correct 435 ms 20868 KB Output is correct
127 Correct 416 ms 20472 KB Output is correct
128 Correct 416 ms 20564 KB Output is correct
129 Correct 398 ms 20536 KB Output is correct
130 Correct 416 ms 20548 KB Output is correct
131 Correct 111 ms 23708 KB Output is correct
132 Correct 95 ms 24916 KB Output is correct
133 Correct 110 ms 21172 KB Output is correct
134 Correct 304 ms 17596 KB Output is correct
135 Correct 295 ms 17500 KB Output is correct
136 Correct 303 ms 17488 KB Output is correct
137 Correct 133 ms 21704 KB Output is correct
138 Correct 166 ms 21656 KB Output is correct
139 Correct 138 ms 21708 KB Output is correct
140 Correct 139 ms 21520 KB Output is correct
141 Correct 138 ms 21452 KB Output is correct
142 Correct 150 ms 21452 KB Output is correct
143 Correct 49 ms 14164 KB Output is correct
144 Correct 393 ms 20144 KB Output is correct