Submission #746353

# Submission time Handle Problem Language Result Execution time Memory
746353 2023-05-22T11:45:01 Z baluteshih Tourism (JOI23_tourism) C++14
100 / 100
390 ms 41932 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) {
        //cerr << "remove " << seg[rt].mi << " " << seg[rt].micnt << "\n";
        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) {
        //cerr << "add " << seg[rt].mi << " " << seg[rt].micnt << "\n";
        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];
            //cerr << "mdfy " << "[" << pl[ta] << ", " << pl[a] << "] " << v << endl; 
            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;
    }

    /*for (int i = 1; i <= n; ++i)
        cerr << hld.seq[i] << " ";
    cerr << endl;*/

    // 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);
    }

    /*for (int i = 1; i <= q; ++i)
        cerr << ans[i] << " ";
    cerr << endl;*/

    // zero
    for (int i = 1; i <= m; ++i) {
        hld.mdfy(arr[i], i);
        //cerr << i << " done\n";
        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:199:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  199 |         for (auto [r, idx] : full[i])
      |                   ^
tourism.cpp:211:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  211 |         for (auto [l, idx] : qry[i])
      |                   ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7452 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 5 ms 7508 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7492 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 5 ms 7636 KB Output is correct
13 Correct 5 ms 7512 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7500 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 4 ms 7500 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 7508 KB Output is correct
27 Correct 4 ms 7508 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 6 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7452 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 5 ms 7508 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7492 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 5 ms 7636 KB Output is correct
13 Correct 5 ms 7512 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7500 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 4 ms 7500 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 7508 KB Output is correct
27 Correct 4 ms 7508 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 6 ms 7508 KB Output is correct
30 Correct 6 ms 7900 KB Output is correct
31 Correct 6 ms 7876 KB Output is correct
32 Correct 8 ms 8020 KB Output is correct
33 Correct 8 ms 8020 KB Output is correct
34 Correct 7 ms 8036 KB Output is correct
35 Correct 7 ms 8020 KB Output is correct
36 Correct 7 ms 8020 KB Output is correct
37 Correct 8 ms 7980 KB Output is correct
38 Correct 7 ms 8028 KB Output is correct
39 Correct 7 ms 8028 KB Output is correct
40 Correct 6 ms 8020 KB Output is correct
41 Correct 6 ms 7992 KB Output is correct
42 Correct 8 ms 8032 KB Output is correct
43 Correct 6 ms 8032 KB Output is correct
44 Correct 9 ms 8020 KB Output is correct
45 Correct 8 ms 8028 KB Output is correct
46 Correct 9 ms 7984 KB Output is correct
47 Correct 8 ms 8020 KB Output is correct
48 Correct 9 ms 8020 KB Output is correct
49 Correct 7 ms 8020 KB Output is correct
50 Correct 6 ms 8028 KB Output is correct
51 Correct 6 ms 8020 KB Output is correct
52 Correct 7 ms 8020 KB Output is correct
53 Correct 7 ms 8036 KB Output is correct
54 Correct 8 ms 8024 KB Output is correct
55 Correct 6 ms 8040 KB Output is correct
56 Correct 5 ms 7780 KB Output is correct
57 Correct 5 ms 7648 KB Output is correct
58 Correct 7 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 129 ms 29692 KB Output is correct
5 Correct 114 ms 31204 KB Output is correct
6 Correct 141 ms 35656 KB Output is correct
7 Correct 231 ms 41320 KB Output is correct
8 Correct 181 ms 41440 KB Output is correct
9 Correct 241 ms 41284 KB Output is correct
10 Correct 192 ms 41252 KB Output is correct
11 Correct 172 ms 40516 KB Output is correct
12 Correct 117 ms 40232 KB Output is correct
13 Correct 116 ms 40292 KB Output is correct
14 Correct 120 ms 39820 KB Output is correct
15 Correct 59 ms 25220 KB Output is correct
16 Correct 198 ms 41932 KB Output is correct
17 Correct 71 ms 25080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 123 ms 20132 KB Output is correct
3 Correct 147 ms 25272 KB Output is correct
4 Correct 129 ms 25776 KB Output is correct
5 Correct 253 ms 32456 KB Output is correct
6 Correct 249 ms 32796 KB Output is correct
7 Correct 204 ms 32772 KB Output is correct
8 Correct 216 ms 32724 KB Output is correct
9 Correct 200 ms 32940 KB Output is correct
10 Correct 213 ms 32860 KB Output is correct
11 Correct 207 ms 32520 KB Output is correct
12 Correct 218 ms 32916 KB Output is correct
13 Correct 227 ms 33288 KB Output is correct
14 Correct 218 ms 34816 KB Output is correct
15 Correct 267 ms 39572 KB Output is correct
16 Correct 220 ms 33740 KB Output is correct
17 Correct 223 ms 33696 KB Output is correct
18 Correct 215 ms 33712 KB Output is correct
19 Correct 235 ms 32832 KB Output is correct
20 Correct 218 ms 33000 KB Output is correct
21 Correct 238 ms 32896 KB Output is correct
22 Correct 214 ms 32664 KB Output is correct
23 Correct 217 ms 32664 KB Output is correct
24 Correct 230 ms 32988 KB Output is correct
25 Correct 222 ms 32992 KB Output is correct
26 Correct 216 ms 32764 KB Output is correct
27 Correct 249 ms 32712 KB Output is correct
28 Correct 227 ms 32972 KB Output is correct
29 Correct 225 ms 33156 KB Output is correct
30 Correct 237 ms 33356 KB Output is correct
31 Correct 233 ms 33556 KB Output is correct
32 Correct 235 ms 34460 KB Output is correct
33 Correct 247 ms 36620 KB Output is correct
34 Correct 355 ms 40004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 254 ms 28444 KB Output is correct
5 Correct 278 ms 30936 KB Output is correct
6 Correct 354 ms 36340 KB Output is correct
7 Correct 336 ms 37620 KB Output is correct
8 Correct 385 ms 37800 KB Output is correct
9 Correct 334 ms 37792 KB Output is correct
10 Correct 312 ms 37332 KB Output is correct
11 Correct 331 ms 38052 KB Output is correct
12 Correct 390 ms 37380 KB Output is correct
13 Correct 343 ms 37792 KB Output is correct
14 Correct 100 ms 25128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7452 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 5 ms 7508 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 6 ms 7492 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 5 ms 7636 KB Output is correct
13 Correct 5 ms 7512 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7500 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 4 ms 7508 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7508 KB Output is correct
21 Correct 5 ms 7508 KB Output is correct
22 Correct 4 ms 7500 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 7508 KB Output is correct
27 Correct 4 ms 7508 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 6 ms 7508 KB Output is correct
30 Correct 6 ms 7900 KB Output is correct
31 Correct 6 ms 7876 KB Output is correct
32 Correct 8 ms 8020 KB Output is correct
33 Correct 8 ms 8020 KB Output is correct
34 Correct 7 ms 8036 KB Output is correct
35 Correct 7 ms 8020 KB Output is correct
36 Correct 7 ms 8020 KB Output is correct
37 Correct 8 ms 7980 KB Output is correct
38 Correct 7 ms 8028 KB Output is correct
39 Correct 7 ms 8028 KB Output is correct
40 Correct 6 ms 8020 KB Output is correct
41 Correct 6 ms 7992 KB Output is correct
42 Correct 8 ms 8032 KB Output is correct
43 Correct 6 ms 8032 KB Output is correct
44 Correct 9 ms 8020 KB Output is correct
45 Correct 8 ms 8028 KB Output is correct
46 Correct 9 ms 7984 KB Output is correct
47 Correct 8 ms 8020 KB Output is correct
48 Correct 9 ms 8020 KB Output is correct
49 Correct 7 ms 8020 KB Output is correct
50 Correct 6 ms 8028 KB Output is correct
51 Correct 6 ms 8020 KB Output is correct
52 Correct 7 ms 8020 KB Output is correct
53 Correct 7 ms 8036 KB Output is correct
54 Correct 8 ms 8024 KB Output is correct
55 Correct 6 ms 8040 KB Output is correct
56 Correct 5 ms 7780 KB Output is correct
57 Correct 5 ms 7648 KB Output is correct
58 Correct 7 ms 8020 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 4 ms 7508 KB Output is correct
61 Correct 5 ms 7764 KB Output is correct
62 Correct 129 ms 29692 KB Output is correct
63 Correct 114 ms 31204 KB Output is correct
64 Correct 141 ms 35656 KB Output is correct
65 Correct 231 ms 41320 KB Output is correct
66 Correct 181 ms 41440 KB Output is correct
67 Correct 241 ms 41284 KB Output is correct
68 Correct 192 ms 41252 KB Output is correct
69 Correct 172 ms 40516 KB Output is correct
70 Correct 117 ms 40232 KB Output is correct
71 Correct 116 ms 40292 KB Output is correct
72 Correct 120 ms 39820 KB Output is correct
73 Correct 59 ms 25220 KB Output is correct
74 Correct 198 ms 41932 KB Output is correct
75 Correct 71 ms 25080 KB Output is correct
76 Correct 4 ms 7380 KB Output is correct
77 Correct 123 ms 20132 KB Output is correct
78 Correct 147 ms 25272 KB Output is correct
79 Correct 129 ms 25776 KB Output is correct
80 Correct 253 ms 32456 KB Output is correct
81 Correct 249 ms 32796 KB Output is correct
82 Correct 204 ms 32772 KB Output is correct
83 Correct 216 ms 32724 KB Output is correct
84 Correct 200 ms 32940 KB Output is correct
85 Correct 213 ms 32860 KB Output is correct
86 Correct 207 ms 32520 KB Output is correct
87 Correct 218 ms 32916 KB Output is correct
88 Correct 227 ms 33288 KB Output is correct
89 Correct 218 ms 34816 KB Output is correct
90 Correct 267 ms 39572 KB Output is correct
91 Correct 220 ms 33740 KB Output is correct
92 Correct 223 ms 33696 KB Output is correct
93 Correct 215 ms 33712 KB Output is correct
94 Correct 235 ms 32832 KB Output is correct
95 Correct 218 ms 33000 KB Output is correct
96 Correct 238 ms 32896 KB Output is correct
97 Correct 214 ms 32664 KB Output is correct
98 Correct 217 ms 32664 KB Output is correct
99 Correct 230 ms 32988 KB Output is correct
100 Correct 222 ms 32992 KB Output is correct
101 Correct 216 ms 32764 KB Output is correct
102 Correct 249 ms 32712 KB Output is correct
103 Correct 227 ms 32972 KB Output is correct
104 Correct 225 ms 33156 KB Output is correct
105 Correct 237 ms 33356 KB Output is correct
106 Correct 233 ms 33556 KB Output is correct
107 Correct 235 ms 34460 KB Output is correct
108 Correct 247 ms 36620 KB Output is correct
109 Correct 355 ms 40004 KB Output is correct
110 Correct 3 ms 7380 KB Output is correct
111 Correct 4 ms 7508 KB Output is correct
112 Correct 5 ms 7764 KB Output is correct
113 Correct 254 ms 28444 KB Output is correct
114 Correct 278 ms 30936 KB Output is correct
115 Correct 354 ms 36340 KB Output is correct
116 Correct 336 ms 37620 KB Output is correct
117 Correct 385 ms 37800 KB Output is correct
118 Correct 334 ms 37792 KB Output is correct
119 Correct 312 ms 37332 KB Output is correct
120 Correct 331 ms 38052 KB Output is correct
121 Correct 390 ms 37380 KB Output is correct
122 Correct 343 ms 37792 KB Output is correct
123 Correct 100 ms 25128 KB Output is correct
124 Correct 232 ms 35368 KB Output is correct
125 Correct 199 ms 30504 KB Output is correct
126 Correct 280 ms 37984 KB Output is correct
127 Correct 304 ms 37672 KB Output is correct
128 Correct 289 ms 37808 KB Output is correct
129 Correct 252 ms 37756 KB Output is correct
130 Correct 329 ms 38000 KB Output is correct
131 Correct 197 ms 41008 KB Output is correct
132 Correct 214 ms 41428 KB Output is correct
133 Correct 200 ms 39044 KB Output is correct
134 Correct 263 ms 37980 KB Output is correct
135 Correct 329 ms 38120 KB Output is correct
136 Correct 288 ms 37888 KB Output is correct
137 Correct 182 ms 38600 KB Output is correct
138 Correct 179 ms 38544 KB Output is correct
139 Correct 173 ms 37864 KB Output is correct
140 Correct 157 ms 38332 KB Output is correct
141 Correct 156 ms 38136 KB Output is correct
142 Correct 184 ms 38304 KB Output is correct
143 Correct 82 ms 21964 KB Output is correct
144 Correct 275 ms 38188 KB Output is correct