Submission #1060447

# Submission time Handle Problem Language Result Execution time Memory
1060447 2024-08-15T14:39:20 Z _callmelucian Tourism (JOI23_tourism) C++14
100 / 100
173 ms 41552 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int mn = 1e5 + 5;
int up[mn][18], spt[mn][18], depth[mn], ans[mn], chain[mn], sz[mn];
vector<pii> qry[mn], block[mn];
vector<int> adj[mn];

struct BIT {
    vector<int> tr;
    BIT (int sz) : tr(sz + 1) {}

    int p (int k) { return k & -k; };

    void update (int k, int val) {
        for (; k < tr.size(); k += p(k)) tr[k] += val;
    }

    int preSum (int k, int ans = 0) {
        for (; k > 0; k -= p(k)) ans += tr[k];
        return ans;
    }

    int query (int l, int r) {
        return preSum(r) - preSum(l - 1);
    }
} freqTable(mn);

void preDfs (int u, int p, int d) {
    depth[u] = d, up[u][0] = p, sz[u] = 1;
    for (int i = 1; i < 18; i++)
        up[u][i] = up[up[u][i - 1]][i - 1];
    for (int v : adj[u]) {
        if (v == p) continue;
        preDfs(v, u, d + 1);
        sz[u] += sz[v];
    }
}

void dfs (int u, int p, bool toParent) {
    if (toParent) chain[u] = chain[p];
    else chain[u] = u;

    function<bool(int,int)> cmp = [&] (int a, int b) {
        return sz[a] > sz[b];
    };
    bool heavy = 1;
    for (int v : adj[u])
        if (v != p) dfs(v, u, heavy), heavy = 0;
}

int goUp (int u, int k) {
    for (int i = 0; i < 18; i++)
        if (k & (1 << i)) u = up[u][i];
    return u;
}

int lca (int a, int b) {
    if (depth[a] < depth[b]) swap(a, b);
    a = goUp(a, depth[a] - depth[b]);
    if (a == b) return a;
    for (int i = 17; i >= 0; i--)
        if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
    return up[a][0];
}

int queryLCA (int l, int r) {
    int p = 31 - __builtin_clz(r - l + 1);
    return lca(spt[l][p], spt[r - (1 << p) + 1][p]);
}

void change (int gr, int prf, int val) {
    int prv = 0;
    while (block[gr].size() && block[gr].back().first <= prf) {
        int pos, curv; tie(pos, curv) = block[gr].back();
        block[gr].pop_back();
        freqTable.update(curv, -(pos - prv));
        prv = pos;
    }
    if (block[gr].size())
        freqTable.update(block[gr].back().second, -(prf - prv));
    block[gr].push_back({prf, val});
    freqTable.update(val, prf);
}

void update (int u, int val) {
    while (u) {
        change(chain[u], depth[u] - depth[chain[u]] + 1, val);
        u = up[chain[u]][0];
    }
}

/*
    For each query, count the number of nodes that is
    the parent of at least one of the queried nodes,
    minus the depth of the queried nodes' LCA.
*/

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

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

    for (int i = 1; i <= m; i++) cin >> spt[i][0];
    for (int s = 1; s < 18; s++) {
        for (int i = 1; i + (1 << s) - 1 <= m; i++) {
            int p = s - 1;
            spt[i][s] = lca(spt[i][p], spt[i + (1 << p)][p]);
        }
    }

    for (int i = 0; i < q; i++) {
        int l, r; cin >> l >> r;
        qry[r].push_back({l, i});
    }

    for (int r = 1; r <= m; r++) {
        update(spt[r][0], r);
        for (auto [l, id] : qry[r])
            ans[id] = freqTable.query(l, r) - depth[queryLCA(l, r)];
    }
    for (int i = 0; i < q; i++) cout << ans[i] << "\n";

    return 0;
}

Compilation message

tourism.cpp: In member function 'void BIT::update(int, int)':
tourism.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         for (; k < tr.size(); k += p(k)) tr[k] += val;
      |                ~~^~~~~~~~~~~
tourism.cpp: In function 'int main()':
tourism.cpp:137:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  137 |         for (auto [l, id] : qry[r])
      |                   ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9252 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 2 ms 9308 KB Output is correct
6 Correct 2 ms 9308 KB Output is correct
7 Correct 2 ms 9308 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 2 ms 9308 KB Output is correct
10 Correct 2 ms 9304 KB Output is correct
11 Correct 3 ms 9308 KB Output is correct
12 Correct 2 ms 9308 KB Output is correct
13 Correct 3 ms 9308 KB Output is correct
14 Correct 3 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 2 ms 9308 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9332 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 2 ms 9308 KB Output is correct
21 Correct 2 ms 9308 KB Output is correct
22 Correct 2 ms 9308 KB Output is correct
23 Correct 2 ms 9560 KB Output is correct
24 Correct 2 ms 9304 KB Output is correct
25 Correct 2 ms 9308 KB Output is correct
26 Correct 3 ms 9304 KB Output is correct
27 Correct 3 ms 9052 KB Output is correct
28 Correct 3 ms 9308 KB Output is correct
29 Correct 2 ms 9148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9252 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 2 ms 9308 KB Output is correct
6 Correct 2 ms 9308 KB Output is correct
7 Correct 2 ms 9308 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 2 ms 9308 KB Output is correct
10 Correct 2 ms 9304 KB Output is correct
11 Correct 3 ms 9308 KB Output is correct
12 Correct 2 ms 9308 KB Output is correct
13 Correct 3 ms 9308 KB Output is correct
14 Correct 3 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 2 ms 9308 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9332 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 2 ms 9308 KB Output is correct
21 Correct 2 ms 9308 KB Output is correct
22 Correct 2 ms 9308 KB Output is correct
23 Correct 2 ms 9560 KB Output is correct
24 Correct 2 ms 9304 KB Output is correct
25 Correct 2 ms 9308 KB Output is correct
26 Correct 3 ms 9304 KB Output is correct
27 Correct 3 ms 9052 KB Output is correct
28 Correct 3 ms 9308 KB Output is correct
29 Correct 2 ms 9148 KB Output is correct
30 Correct 4 ms 9560 KB Output is correct
31 Correct 4 ms 9564 KB Output is correct
32 Correct 4 ms 9564 KB Output is correct
33 Correct 4 ms 9564 KB Output is correct
34 Correct 4 ms 9524 KB Output is correct
35 Correct 4 ms 9564 KB Output is correct
36 Correct 4 ms 9564 KB Output is correct
37 Correct 4 ms 9564 KB Output is correct
38 Correct 4 ms 9792 KB Output is correct
39 Correct 4 ms 9820 KB Output is correct
40 Correct 4 ms 9808 KB Output is correct
41 Correct 4 ms 9820 KB Output is correct
42 Correct 4 ms 9784 KB Output is correct
43 Correct 4 ms 9572 KB Output is correct
44 Correct 4 ms 9564 KB Output is correct
45 Correct 4 ms 9564 KB Output is correct
46 Correct 4 ms 9564 KB Output is correct
47 Correct 4 ms 9564 KB Output is correct
48 Correct 4 ms 9524 KB Output is correct
49 Correct 4 ms 9564 KB Output is correct
50 Correct 4 ms 9568 KB Output is correct
51 Correct 4 ms 9700 KB Output is correct
52 Correct 4 ms 9560 KB Output is correct
53 Correct 4 ms 9560 KB Output is correct
54 Correct 3 ms 9564 KB Output is correct
55 Correct 4 ms 9728 KB Output is correct
56 Correct 4 ms 9312 KB Output is correct
57 Correct 3 ms 9308 KB Output is correct
58 Correct 4 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 3 ms 9308 KB Output is correct
4 Correct 97 ms 30200 KB Output is correct
5 Correct 82 ms 32772 KB Output is correct
6 Correct 97 ms 36692 KB Output is correct
7 Correct 129 ms 41300 KB Output is correct
8 Correct 128 ms 41552 KB Output is correct
9 Correct 127 ms 41296 KB Output is correct
10 Correct 125 ms 41552 KB Output is correct
11 Correct 128 ms 41472 KB Output is correct
12 Correct 111 ms 40528 KB Output is correct
13 Correct 105 ms 40644 KB Output is correct
14 Correct 98 ms 40528 KB Output is correct
15 Correct 44 ms 31688 KB Output is correct
16 Correct 132 ms 41172 KB Output is correct
17 Correct 57 ms 19796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 70 ms 20508 KB Output is correct
3 Correct 100 ms 23704 KB Output is correct
4 Correct 83 ms 23124 KB Output is correct
5 Correct 135 ms 29776 KB Output is correct
6 Correct 134 ms 29612 KB Output is correct
7 Correct 137 ms 29780 KB Output is correct
8 Correct 137 ms 29788 KB Output is correct
9 Correct 139 ms 29780 KB Output is correct
10 Correct 137 ms 29776 KB Output is correct
11 Correct 138 ms 29776 KB Output is correct
12 Correct 136 ms 29780 KB Output is correct
13 Correct 135 ms 30292 KB Output is correct
14 Correct 141 ms 31056 KB Output is correct
15 Correct 155 ms 34128 KB Output is correct
16 Correct 132 ms 30292 KB Output is correct
17 Correct 134 ms 30288 KB Output is correct
18 Correct 136 ms 30292 KB Output is correct
19 Correct 135 ms 28596 KB Output is correct
20 Correct 124 ms 28500 KB Output is correct
21 Correct 129 ms 28564 KB Output is correct
22 Correct 138 ms 28496 KB Output is correct
23 Correct 123 ms 28580 KB Output is correct
24 Correct 131 ms 28496 KB Output is correct
25 Correct 133 ms 28648 KB Output is correct
26 Correct 130 ms 28612 KB Output is correct
27 Correct 130 ms 28560 KB Output is correct
28 Correct 127 ms 28548 KB Output is correct
29 Correct 139 ms 28756 KB Output is correct
30 Correct 141 ms 28760 KB Output is correct
31 Correct 137 ms 29008 KB Output is correct
32 Correct 142 ms 29588 KB Output is correct
33 Correct 150 ms 30864 KB Output is correct
34 Correct 147 ms 33108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 2 ms 9248 KB Output is correct
3 Correct 3 ms 9308 KB Output is correct
4 Correct 139 ms 26672 KB Output is correct
5 Correct 144 ms 27216 KB Output is correct
6 Correct 157 ms 31312 KB Output is correct
7 Correct 173 ms 33360 KB Output is correct
8 Correct 165 ms 33364 KB Output is correct
9 Correct 169 ms 33360 KB Output is correct
10 Correct 165 ms 33360 KB Output is correct
11 Correct 166 ms 33360 KB Output is correct
12 Correct 167 ms 33360 KB Output is correct
13 Correct 164 ms 33440 KB Output is correct
14 Correct 58 ms 19740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9252 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 2 ms 9308 KB Output is correct
6 Correct 2 ms 9308 KB Output is correct
7 Correct 2 ms 9308 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 2 ms 9308 KB Output is correct
10 Correct 2 ms 9304 KB Output is correct
11 Correct 3 ms 9308 KB Output is correct
12 Correct 2 ms 9308 KB Output is correct
13 Correct 3 ms 9308 KB Output is correct
14 Correct 3 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 2 ms 9308 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9332 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 2 ms 9308 KB Output is correct
21 Correct 2 ms 9308 KB Output is correct
22 Correct 2 ms 9308 KB Output is correct
23 Correct 2 ms 9560 KB Output is correct
24 Correct 2 ms 9304 KB Output is correct
25 Correct 2 ms 9308 KB Output is correct
26 Correct 3 ms 9304 KB Output is correct
27 Correct 3 ms 9052 KB Output is correct
28 Correct 3 ms 9308 KB Output is correct
29 Correct 2 ms 9148 KB Output is correct
30 Correct 4 ms 9560 KB Output is correct
31 Correct 4 ms 9564 KB Output is correct
32 Correct 4 ms 9564 KB Output is correct
33 Correct 4 ms 9564 KB Output is correct
34 Correct 4 ms 9524 KB Output is correct
35 Correct 4 ms 9564 KB Output is correct
36 Correct 4 ms 9564 KB Output is correct
37 Correct 4 ms 9564 KB Output is correct
38 Correct 4 ms 9792 KB Output is correct
39 Correct 4 ms 9820 KB Output is correct
40 Correct 4 ms 9808 KB Output is correct
41 Correct 4 ms 9820 KB Output is correct
42 Correct 4 ms 9784 KB Output is correct
43 Correct 4 ms 9572 KB Output is correct
44 Correct 4 ms 9564 KB Output is correct
45 Correct 4 ms 9564 KB Output is correct
46 Correct 4 ms 9564 KB Output is correct
47 Correct 4 ms 9564 KB Output is correct
48 Correct 4 ms 9524 KB Output is correct
49 Correct 4 ms 9564 KB Output is correct
50 Correct 4 ms 9568 KB Output is correct
51 Correct 4 ms 9700 KB Output is correct
52 Correct 4 ms 9560 KB Output is correct
53 Correct 4 ms 9560 KB Output is correct
54 Correct 3 ms 9564 KB Output is correct
55 Correct 4 ms 9728 KB Output is correct
56 Correct 4 ms 9312 KB Output is correct
57 Correct 3 ms 9308 KB Output is correct
58 Correct 4 ms 9564 KB Output is correct
59 Correct 2 ms 9048 KB Output is correct
60 Correct 2 ms 9052 KB Output is correct
61 Correct 3 ms 9308 KB Output is correct
62 Correct 97 ms 30200 KB Output is correct
63 Correct 82 ms 32772 KB Output is correct
64 Correct 97 ms 36692 KB Output is correct
65 Correct 129 ms 41300 KB Output is correct
66 Correct 128 ms 41552 KB Output is correct
67 Correct 127 ms 41296 KB Output is correct
68 Correct 125 ms 41552 KB Output is correct
69 Correct 128 ms 41472 KB Output is correct
70 Correct 111 ms 40528 KB Output is correct
71 Correct 105 ms 40644 KB Output is correct
72 Correct 98 ms 40528 KB Output is correct
73 Correct 44 ms 31688 KB Output is correct
74 Correct 132 ms 41172 KB Output is correct
75 Correct 57 ms 19796 KB Output is correct
76 Correct 2 ms 9048 KB Output is correct
77 Correct 70 ms 20508 KB Output is correct
78 Correct 100 ms 23704 KB Output is correct
79 Correct 83 ms 23124 KB Output is correct
80 Correct 135 ms 29776 KB Output is correct
81 Correct 134 ms 29612 KB Output is correct
82 Correct 137 ms 29780 KB Output is correct
83 Correct 137 ms 29788 KB Output is correct
84 Correct 139 ms 29780 KB Output is correct
85 Correct 137 ms 29776 KB Output is correct
86 Correct 138 ms 29776 KB Output is correct
87 Correct 136 ms 29780 KB Output is correct
88 Correct 135 ms 30292 KB Output is correct
89 Correct 141 ms 31056 KB Output is correct
90 Correct 155 ms 34128 KB Output is correct
91 Correct 132 ms 30292 KB Output is correct
92 Correct 134 ms 30288 KB Output is correct
93 Correct 136 ms 30292 KB Output is correct
94 Correct 135 ms 28596 KB Output is correct
95 Correct 124 ms 28500 KB Output is correct
96 Correct 129 ms 28564 KB Output is correct
97 Correct 138 ms 28496 KB Output is correct
98 Correct 123 ms 28580 KB Output is correct
99 Correct 131 ms 28496 KB Output is correct
100 Correct 133 ms 28648 KB Output is correct
101 Correct 130 ms 28612 KB Output is correct
102 Correct 130 ms 28560 KB Output is correct
103 Correct 127 ms 28548 KB Output is correct
104 Correct 139 ms 28756 KB Output is correct
105 Correct 141 ms 28760 KB Output is correct
106 Correct 137 ms 29008 KB Output is correct
107 Correct 142 ms 29588 KB Output is correct
108 Correct 150 ms 30864 KB Output is correct
109 Correct 147 ms 33108 KB Output is correct
110 Correct 2 ms 9052 KB Output is correct
111 Correct 2 ms 9248 KB Output is correct
112 Correct 3 ms 9308 KB Output is correct
113 Correct 139 ms 26672 KB Output is correct
114 Correct 144 ms 27216 KB Output is correct
115 Correct 157 ms 31312 KB Output is correct
116 Correct 173 ms 33360 KB Output is correct
117 Correct 165 ms 33364 KB Output is correct
118 Correct 169 ms 33360 KB Output is correct
119 Correct 165 ms 33360 KB Output is correct
120 Correct 166 ms 33360 KB Output is correct
121 Correct 167 ms 33360 KB Output is correct
122 Correct 164 ms 33440 KB Output is correct
123 Correct 58 ms 19740 KB Output is correct
124 Correct 157 ms 32060 KB Output is correct
125 Correct 136 ms 28632 KB Output is correct
126 Correct 173 ms 33364 KB Output is correct
127 Correct 172 ms 33360 KB Output is correct
128 Correct 167 ms 33360 KB Output is correct
129 Correct 166 ms 33364 KB Output is correct
130 Correct 169 ms 33360 KB Output is correct
131 Correct 165 ms 40016 KB Output is correct
132 Correct 137 ms 41384 KB Output is correct
133 Correct 136 ms 36616 KB Output is correct
134 Correct 161 ms 32336 KB Output is correct
135 Correct 159 ms 32300 KB Output is correct
136 Correct 160 ms 32340 KB Output is correct
137 Correct 114 ms 34504 KB Output is correct
138 Correct 114 ms 34324 KB Output is correct
139 Correct 107 ms 34352 KB Output is correct
140 Correct 108 ms 34240 KB Output is correct
141 Correct 119 ms 34252 KB Output is correct
142 Correct 105 ms 34252 KB Output is correct
143 Correct 47 ms 22220 KB Output is correct
144 Correct 160 ms 33108 KB Output is correct