Submission #746356

# Submission time Handle Problem Language Result Execution time Memory
746356 2023-05-22T11:46:56 Z baluteshih Tourism (JOI23_tourism) C++14
100 / 100
486 ms 39492 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

const int MAXN = 100005;
const int K = 320;

struct Node {
    int mi, micnt, smi, lazy;
} seg[MAXN << 2];

int cnt[MAXN], bcnt[MAXN / K + 1];

void up(int rt) {
    seg[rt].mi = min(seg[rt << 1].mi, seg[rt << 1 | 1].mi);
    seg[rt].micnt = 0;
    seg[rt].smi = min(seg[rt << 1].smi, seg[rt << 1 | 1].smi);
    if (seg[rt].mi == seg[rt << 1].mi) seg[rt].micnt += seg[rt << 1].micnt;
    else seg[rt].smi = min(seg[rt].smi, seg[rt << 1].mi);
    if (seg[rt].mi == seg[rt << 1 | 1].mi) seg[rt].micnt += seg[rt << 1 | 1].micnt;
    else seg[rt].smi = min(seg[rt].smi, seg[rt << 1 | 1].mi);
}

void build(int l, int r, int rt) {
    if (l == r) {
        seg[rt].mi = 0;
        seg[rt].micnt = 1;
        seg[rt].smi = MAXN;
        seg[rt].lazy = -1;
        ++cnt[0], ++bcnt[0];
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
    up(rt);
}

void give_tag(int rt, int v, int mdfy = 0) {
    if (mdfy) {
        cnt[seg[rt].mi] -= seg[rt].micnt;
        bcnt[seg[rt].mi / K] -= seg[rt].micnt;
    }
    seg[rt].mi = max(seg[rt].mi, v);
    seg[rt].lazy = max(seg[rt].lazy, v);
    if (mdfy) {
        cnt[seg[rt].mi] += seg[rt].micnt;
        bcnt[seg[rt].mi / K] += seg[rt].micnt;
    }
}

void down(int rt) {
    if (seg[rt].lazy != -1) {
        give_tag(rt << 1, seg[rt].lazy);
        give_tag(rt << 1 | 1, seg[rt].lazy);
        seg[rt].lazy = -1;
    }
}

void modify(int L, int R, int l, int r, int rt, int v) {
    if (v <= seg[rt].mi) return;
    if (L <= l && R >= r && v < seg[rt].smi)
        return give_tag(rt, v, 1);
    down(rt);
    int mid = (l + r) >> 1;
    if (L <= mid) modify(L, R, l, mid, rt << 1, v);
    if (R > mid) modify(L, R, mid + 1, r, rt << 1 | 1, v);
    up(rt);
}

int query(int l) {
    int ans = 0;
    for (int i = 0; (i + 1) * K <= l; ++i)
        ans += bcnt[i];
    for (int i = l / K * K; i < l; ++i)
        ans += cnt[i];
    return ans;
}

struct Heavy_light_Decomposition { // 1-base
    int n, ulink[MAXN], deep[MAXN], mxson[MAXN], w[MAXN], pa[MAXN];
    int t, seq[MAXN], pl[MAXN], out[MAXN];
    vector<int> G[MAXN];
    void init(int _n) {
        n = _n, t = 0;
        for (int i = 1; i <= n; ++i)
            G[i].clear(), mxson[i] = 0;
    }
    void add_edge(int a, int b) {
        G[a].pb(b);
        G[b].pb(a);
    }
    void dfs(int u, int f, int d) {
        w[u] = 1, pa[u] = f, deep[u] = d++;
        for (auto i : G[u])
            if (i != f) {
                dfs(i, u, d), w[u] += w[i];
                if (w[mxson[u]] < w[i]) mxson[u] = i;
            } 
    }
    void cut(int u, int link) {
        seq[pl[u] = ++t] = u, ulink[u] = link;
        if (mxson[u]) {
            cut(mxson[u], link);
            for (auto i : G[u])
                if (i != pa[u] && i != mxson[u])
                    cut(i, i);
        }
        out[u] = t;
    }
    void solve() { 
        dfs(1, 0, 1);
        cut(1, 1);
        build(1, t, 1);
    }
    void mdfy(int a, int v) {
        while (a) {
            int ta = ulink[a];
            modify(pl[ta], pl[a], 1, n, 1, v);
            a = pa[ta];
        }
    }
} hld;

int arr[MAXN], ans[MAXN];
int mi[__lg(MAXN) + 1][MAXN], mx[__lg(MAXN) + 1][MAXN];
int bit[MAXN], bit_all;
vector<pii> qry[MAXN], full[MAXN];

int get_mi(int l, int r) {
    int lg = __lg(r - l + 1);
    return min(mi[lg][l], mi[lg][r - (1 << lg) + 1]);
}

int get_mx(int l, int r) {
    int lg = __lg(r - l + 1);
    return max(mx[lg][l], mx[lg][r - (1 << lg) + 1]);
}

void bit_modify(int x, int v) {
    for (bit_all += v; x <= hld.t; x += x & -x)
        bit[x] += v;
}

int bit_query(int x) {
    int res = 0;
    for (; x; x -= x & -x)
        res += bit[x];
    return res;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    hld.init(n);
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        hld.add_edge(u, v);
    }
    hld.solve();
    for (int i = 1; i <= m; ++i)
        cin >> arr[i];
    for (int i = 1; i <= m; ++i)
        mi[0][i] = mx[0][i] = hld.pl[arr[i]];  
    int L = __lg(m);
    for (int i = 1; i <= L; ++i)
        for (int j = 1; j + (1 << i) <= m + 1; ++j) {
            mi[i][j] = min(mi[i - 1][j], mi[i - 1][j + (1 << (i - 1))]);
            mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << (i - 1))]);
        }
    for (int i = 1; i <= q; ++i) {
        int l, r;
        cin >> l >> r;
        qry[r].pb(pii(l, i));
        int a = get_mi(l, r);
        int b = get_mx(l, r);
        full[a].pb(pii(b, i));
        ans[i] = n + 1;
    }

    // full
    for (int i = 1; i <= n; ++i) {
        bit_modify(hld.out[hld.seq[i]], 1);
        for (auto [r, idx] : full[i])
            ans[idx] -= bit_all - bit_query(r - 1);
    }

    // zero
    for (int i = 1; i <= m; ++i) {
        hld.mdfy(arr[i], i);
        for (auto [l, idx] : qry[i])
            ans[idx] -= query(l);
    }

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

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:192:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  192 |         for (auto [r, idx] : full[i])
      |                   ^
tourism.cpp:199:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  199 |         for (auto [l, idx] : qry[i])
      |                   ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7512 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 6 ms 7520 KB Output is correct
13 Correct 7 ms 7516 KB Output is correct
14 Correct 5 ms 7512 KB Output is correct
15 Correct 6 ms 7516 KB Output is correct
16 Correct 5 ms 7512 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 6 ms 7512 KB Output is correct
19 Correct 5 ms 7512 KB Output is correct
20 Correct 7 ms 7508 KB Output is correct
21 Correct 7 ms 7580 KB Output is correct
22 Correct 6 ms 7508 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7496 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 5 ms 7508 KB Output is correct
28 Correct 4 ms 7384 KB Output is correct
29 Correct 6 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7512 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 6 ms 7520 KB Output is correct
13 Correct 7 ms 7516 KB Output is correct
14 Correct 5 ms 7512 KB Output is correct
15 Correct 6 ms 7516 KB Output is correct
16 Correct 5 ms 7512 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 6 ms 7512 KB Output is correct
19 Correct 5 ms 7512 KB Output is correct
20 Correct 7 ms 7508 KB Output is correct
21 Correct 7 ms 7580 KB Output is correct
22 Correct 6 ms 7508 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7496 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 5 ms 7508 KB Output is correct
28 Correct 4 ms 7384 KB Output is correct
29 Correct 6 ms 7508 KB Output is correct
30 Correct 6 ms 7892 KB Output is correct
31 Correct 8 ms 7852 KB Output is correct
32 Correct 8 ms 8004 KB Output is correct
33 Correct 10 ms 8020 KB Output is correct
34 Correct 7 ms 8020 KB Output is correct
35 Correct 10 ms 8020 KB Output is correct
36 Correct 7 ms 7904 KB Output is correct
37 Correct 7 ms 7960 KB Output is correct
38 Correct 9 ms 8020 KB Output is correct
39 Correct 8 ms 8020 KB Output is correct
40 Correct 9 ms 8028 KB Output is correct
41 Correct 7 ms 8020 KB Output is correct
42 Correct 7 ms 8020 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 8 ms 8004 KB Output is correct
45 Correct 8 ms 8020 KB Output is correct
46 Correct 8 ms 8020 KB Output is correct
47 Correct 7 ms 8020 KB Output is correct
48 Correct 7 ms 8024 KB Output is correct
49 Correct 9 ms 8020 KB Output is correct
50 Correct 6 ms 8032 KB Output is correct
51 Correct 9 ms 8020 KB Output is correct
52 Correct 8 ms 8032 KB Output is correct
53 Correct 7 ms 8020 KB Output is correct
54 Correct 7 ms 8020 KB Output is correct
55 Correct 9 ms 8036 KB Output is correct
56 Correct 6 ms 7808 KB Output is correct
57 Correct 6 ms 7640 KB Output is correct
58 Correct 7 ms 8028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 6 ms 7508 KB Output is correct
3 Correct 7 ms 7764 KB Output is correct
4 Correct 129 ms 29848 KB Output is correct
5 Correct 120 ms 29696 KB Output is correct
6 Correct 138 ms 34080 KB Output is correct
7 Correct 207 ms 39048 KB Output is correct
8 Correct 201 ms 39164 KB Output is correct
9 Correct 178 ms 38944 KB Output is correct
10 Correct 257 ms 38936 KB Output is correct
11 Correct 229 ms 38832 KB Output is correct
12 Correct 121 ms 38272 KB Output is correct
13 Correct 122 ms 38224 KB Output is correct
14 Correct 113 ms 38272 KB Output is correct
15 Correct 54 ms 24312 KB Output is correct
16 Correct 233 ms 39492 KB Output is correct
17 Correct 83 ms 24140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 129 ms 20280 KB Output is correct
3 Correct 161 ms 25356 KB Output is correct
4 Correct 166 ms 25004 KB Output is correct
5 Correct 254 ms 31612 KB Output is correct
6 Correct 245 ms 31612 KB Output is correct
7 Correct 274 ms 31676 KB Output is correct
8 Correct 249 ms 31524 KB Output is correct
9 Correct 292 ms 31616 KB Output is correct
10 Correct 262 ms 31588 KB Output is correct
11 Correct 278 ms 31640 KB Output is correct
12 Correct 238 ms 31732 KB Output is correct
13 Correct 223 ms 32208 KB Output is correct
14 Correct 269 ms 33448 KB Output is correct
15 Correct 356 ms 37452 KB Output is correct
16 Correct 216 ms 32268 KB Output is correct
17 Correct 295 ms 32216 KB Output is correct
18 Correct 250 ms 32272 KB Output is correct
19 Correct 212 ms 31464 KB Output is correct
20 Correct 241 ms 31564 KB Output is correct
21 Correct 257 ms 31532 KB Output is correct
22 Correct 267 ms 31648 KB Output is correct
23 Correct 222 ms 31568 KB Output is correct
24 Correct 252 ms 31460 KB Output is correct
25 Correct 259 ms 31556 KB Output is correct
26 Correct 253 ms 31520 KB Output is correct
27 Correct 256 ms 31652 KB Output is correct
28 Correct 232 ms 31740 KB Output is correct
29 Correct 251 ms 31692 KB Output is correct
30 Correct 304 ms 31884 KB Output is correct
31 Correct 354 ms 32136 KB Output is correct
32 Correct 309 ms 32748 KB Output is correct
33 Correct 344 ms 34656 KB Output is correct
34 Correct 328 ms 37356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7508 KB Output is correct
3 Correct 6 ms 7776 KB Output is correct
4 Correct 298 ms 28592 KB Output is correct
5 Correct 335 ms 29132 KB Output is correct
6 Correct 368 ms 34288 KB Output is correct
7 Correct 415 ms 35388 KB Output is correct
8 Correct 372 ms 35580 KB Output is correct
9 Correct 448 ms 35428 KB Output is correct
10 Correct 426 ms 35444 KB Output is correct
11 Correct 411 ms 35404 KB Output is correct
12 Correct 486 ms 35396 KB Output is correct
13 Correct 402 ms 35372 KB Output is correct
14 Correct 111 ms 24220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7512 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 6 ms 7520 KB Output is correct
13 Correct 7 ms 7516 KB Output is correct
14 Correct 5 ms 7512 KB Output is correct
15 Correct 6 ms 7516 KB Output is correct
16 Correct 5 ms 7512 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 6 ms 7512 KB Output is correct
19 Correct 5 ms 7512 KB Output is correct
20 Correct 7 ms 7508 KB Output is correct
21 Correct 7 ms 7580 KB Output is correct
22 Correct 6 ms 7508 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7496 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 5 ms 7508 KB Output is correct
28 Correct 4 ms 7384 KB Output is correct
29 Correct 6 ms 7508 KB Output is correct
30 Correct 6 ms 7892 KB Output is correct
31 Correct 8 ms 7852 KB Output is correct
32 Correct 8 ms 8004 KB Output is correct
33 Correct 10 ms 8020 KB Output is correct
34 Correct 7 ms 8020 KB Output is correct
35 Correct 10 ms 8020 KB Output is correct
36 Correct 7 ms 7904 KB Output is correct
37 Correct 7 ms 7960 KB Output is correct
38 Correct 9 ms 8020 KB Output is correct
39 Correct 8 ms 8020 KB Output is correct
40 Correct 9 ms 8028 KB Output is correct
41 Correct 7 ms 8020 KB Output is correct
42 Correct 7 ms 8020 KB Output is correct
43 Correct 7 ms 8020 KB Output is correct
44 Correct 8 ms 8004 KB Output is correct
45 Correct 8 ms 8020 KB Output is correct
46 Correct 8 ms 8020 KB Output is correct
47 Correct 7 ms 8020 KB Output is correct
48 Correct 7 ms 8024 KB Output is correct
49 Correct 9 ms 8020 KB Output is correct
50 Correct 6 ms 8032 KB Output is correct
51 Correct 9 ms 8020 KB Output is correct
52 Correct 8 ms 8032 KB Output is correct
53 Correct 7 ms 8020 KB Output is correct
54 Correct 7 ms 8020 KB Output is correct
55 Correct 9 ms 8036 KB Output is correct
56 Correct 6 ms 7808 KB Output is correct
57 Correct 6 ms 7640 KB Output is correct
58 Correct 7 ms 8028 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 6 ms 7508 KB Output is correct
61 Correct 7 ms 7764 KB Output is correct
62 Correct 129 ms 29848 KB Output is correct
63 Correct 120 ms 29696 KB Output is correct
64 Correct 138 ms 34080 KB Output is correct
65 Correct 207 ms 39048 KB Output is correct
66 Correct 201 ms 39164 KB Output is correct
67 Correct 178 ms 38944 KB Output is correct
68 Correct 257 ms 38936 KB Output is correct
69 Correct 229 ms 38832 KB Output is correct
70 Correct 121 ms 38272 KB Output is correct
71 Correct 122 ms 38224 KB Output is correct
72 Correct 113 ms 38272 KB Output is correct
73 Correct 54 ms 24312 KB Output is correct
74 Correct 233 ms 39492 KB Output is correct
75 Correct 83 ms 24140 KB Output is correct
76 Correct 5 ms 7380 KB Output is correct
77 Correct 129 ms 20280 KB Output is correct
78 Correct 161 ms 25356 KB Output is correct
79 Correct 166 ms 25004 KB Output is correct
80 Correct 254 ms 31612 KB Output is correct
81 Correct 245 ms 31612 KB Output is correct
82 Correct 274 ms 31676 KB Output is correct
83 Correct 249 ms 31524 KB Output is correct
84 Correct 292 ms 31616 KB Output is correct
85 Correct 262 ms 31588 KB Output is correct
86 Correct 278 ms 31640 KB Output is correct
87 Correct 238 ms 31732 KB Output is correct
88 Correct 223 ms 32208 KB Output is correct
89 Correct 269 ms 33448 KB Output is correct
90 Correct 356 ms 37452 KB Output is correct
91 Correct 216 ms 32268 KB Output is correct
92 Correct 295 ms 32216 KB Output is correct
93 Correct 250 ms 32272 KB Output is correct
94 Correct 212 ms 31464 KB Output is correct
95 Correct 241 ms 31564 KB Output is correct
96 Correct 257 ms 31532 KB Output is correct
97 Correct 267 ms 31648 KB Output is correct
98 Correct 222 ms 31568 KB Output is correct
99 Correct 252 ms 31460 KB Output is correct
100 Correct 259 ms 31556 KB Output is correct
101 Correct 253 ms 31520 KB Output is correct
102 Correct 256 ms 31652 KB Output is correct
103 Correct 232 ms 31740 KB Output is correct
104 Correct 251 ms 31692 KB Output is correct
105 Correct 304 ms 31884 KB Output is correct
106 Correct 354 ms 32136 KB Output is correct
107 Correct 309 ms 32748 KB Output is correct
108 Correct 344 ms 34656 KB Output is correct
109 Correct 328 ms 37356 KB Output is correct
110 Correct 4 ms 7380 KB Output is correct
111 Correct 5 ms 7508 KB Output is correct
112 Correct 6 ms 7776 KB Output is correct
113 Correct 298 ms 28592 KB Output is correct
114 Correct 335 ms 29132 KB Output is correct
115 Correct 368 ms 34288 KB Output is correct
116 Correct 415 ms 35388 KB Output is correct
117 Correct 372 ms 35580 KB Output is correct
118 Correct 448 ms 35428 KB Output is correct
119 Correct 426 ms 35444 KB Output is correct
120 Correct 411 ms 35404 KB Output is correct
121 Correct 486 ms 35396 KB Output is correct
122 Correct 402 ms 35372 KB Output is correct
123 Correct 111 ms 24220 KB Output is correct
124 Correct 307 ms 33072 KB Output is correct
125 Correct 215 ms 28716 KB Output is correct
126 Correct 344 ms 35572 KB Output is correct
127 Correct 383 ms 35532 KB Output is correct
128 Correct 429 ms 35540 KB Output is correct
129 Correct 343 ms 35440 KB Output is correct
130 Correct 366 ms 35556 KB Output is correct
131 Correct 252 ms 38420 KB Output is correct
132 Correct 298 ms 39064 KB Output is correct
133 Correct 262 ms 37072 KB Output is correct
134 Correct 358 ms 35516 KB Output is correct
135 Correct 394 ms 35516 KB Output is correct
136 Correct 328 ms 35524 KB Output is correct
137 Correct 172 ms 36120 KB Output is correct
138 Correct 184 ms 36024 KB Output is correct
139 Correct 170 ms 35820 KB Output is correct
140 Correct 192 ms 35816 KB Output is correct
141 Correct 157 ms 35788 KB Output is correct
142 Correct 208 ms 35760 KB Output is correct
143 Correct 76 ms 20852 KB Output is correct
144 Correct 297 ms 36208 KB Output is correct