Submission #726062

# Submission time Handle Problem Language Result Execution time Memory
726062 2023-04-18T12:06:53 Z piOOE Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
1660 ms 478360 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int N_ = 8e5 * 20 * 2 + 1;
int top = 1;

struct Node {
    ll sum = 0, mxp = 0;
    int left = 0, right = 0;
} t[N_];

int create() {
    return top++;
}

void pull(int x) {
    int l = t[x].left, r = t[x].right;
    t[x].sum = t[l].sum + t[r].sum;
    t[x].mxp = max(t[l].mxp, t[l].sum + t[r].mxp);
}

void modify(int x, int lx, int rx, int i, ll d) {
    if (lx == i && rx == i + 1) {
        t[x].sum += d;
        t[x].mxp = max(t[x].sum, 0LL);
        return;
    }

    int mid = (lx + rx) >> 1;

    if (i < mid) {
        if (t[x].left == 0) {
            t[x].left = create();
        }
        modify(t[x].left, lx, mid, i, d);
    } else {
        if (t[x].right == 0) {
            t[x].right = create();
        }
        modify(t[x].right, mid, rx, i, d);
    }

    pull(x);
}

constexpr int N = 2e5 + 1;

vector<int> adj[N], inside[N];

int fa[N], n;

void initFa() {
    iota(fa, fa + n + 1, 0);
    for (int i = 0; i < n; ++i) {
        inside[i] = {i};
    }
}

int leader(int x) {
    while (x != fa[x]) {
        x = fa[x] = fa[fa[x]];
    }
    return x;
}

void unite(int x, int y) {
    x = leader(x), y = leader(y);
    if (x == y) {
        return;
    }

    if (inside[x].size() < inside[y].size()) {
        swap(inside[x], inside[y]);
    }
    inside[x].insert(inside[x].end(), inside[y].begin(), inside[y].end());
    inside[y].clear();

    fa[y] = x;
}

int yy[N], h[N], H[N], C[N], siz[N], root[N];
int m = 0;

pair<ll, ll> getBest(int x, int i) {
    int lx = 0, rx = m;
    ll ans = 0, sum = 0;
    while (lx + 1 < rx) {
        int mid = lx + rx >> 1;
        if (i < mid) {
            ans = max(ans, t[t[x].right].mxp + t[t[x].left].sum + sum);
            rx = mid;
            x = t[x].left;
        } else {
            sum += t[t[x].left].sum;
            lx = mid;
            x = t[x].right;
        }
    }
    sum += t[x].sum;
    ans = max(ans, sum);
    return {ans, sum};
}

void modify(int x, int l, int r, ll d) {
    if (l < m) {
        modify(x, 0, m, l, d);
    }
    if (r < m) {
        modify(x, 0, m, r, -d);
    }
}

void addDP(int x, int i, ll dp) {
    auto [ans, sum] = getBest(x, i);
    ans += dp;
    assert(ans >= sum);
    modify(x, i, i + 1, ans - sum);
}


void upd(int x, int i) {
    addDP(x, i, 0);
}

void dfs(int v) {
    sort(inside[v].begin(), inside[v].end(), [&](int i, int j) {
        return H[i] < H[j];
    });

    vector<int> ours = inside[v];

    siz[v] = inside[v].size();

    for (int to: adj[v]) {
        dfs(to);
        siz[v] += siz[to];
    }

    sort(adj[v].begin(), adj[v].end(), [&](int x, int y) {
        return siz[x] > siz[y];
    });

    if (adj[v].empty()) {
        root[v] = create();
    } else {
        root[v] = root[adj[v][0]];
        unite(v, adj[v][0]);
    }

    auto fuck = [&](int l, int r, ll dp) {
        upd(root[v], r - 1);
        modify(root[v], l, r, dp);
    };

    for (int i = 1; i < adj[v].size(); ++i) {
        int to = adj[v][i];

        sort(inside[to].begin(), inside[to].end(), [&](int x, int y) {
            return H[x] < H[y];
        });

        for (int x = 0; x < inside[to].size(); ++x) {
            int l = x == 0 ? 0 : h[inside[to][x - 1]] + 1;
            int r = h[inside[to][x]] + 1;
            ll dp = getBest(root[to], h[inside[to][x]]).first;
            fuck(l, r, dp);
        }

        unite(v, to);
    }

    for (int i = 0, j = 0; i < ours.size(); i = j) {
        ll sum = 0;
        while (j < ours.size() && H[ours[i]] == H[ours[j]]) {
            sum += C[ours[j]];
            j += 1;
        }
        addDP(root[v], h[ours[i]], sum);
    }
}

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

    cin >> n;

    vector<int> p(n);

    for (int i = 0; i < n; ++i) {
        cin >> p[i] >> H[i] >> C[i];
        p[i] -= 1;
    }

    copy(H, H + n, yy);

    sort(yy, yy + n);
    m = unique(yy, yy + n) - yy;

    for (int i = 0; i < n; ++i) {
        h[i] = lower_bound(yy, yy + m, H[i]) - yy;
    }

    vector<int> used(n);
    initFa();
    int T = 0;

    for (int i = 0; i < n; ++i) {
        if (used[i]) {
            continue;
        }

        int x = i;
        ++T;
        vector<int> now;

        while (!used[x]) {
            now.push_back(x);
            used[x] = T;
            x = p[x];
        }

        if (used[x] == T) {
            int y = p[x];
            while (y != x) {
                unite(x, y);
                y = p[y];
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        if (leader(i) == i) {
            int x = leader(p[i]);
            if (x == i) {
                adj[n].push_back(i);
//                cout << n << "->" << i << endl;
            } else {
                adj[x].push_back(i);
//                cout << x << "->" << i << endl;
            }
        }
    }

    dfs(n);

    ll ans = getBest(root[n], 0).first;

    ans = accumulate(C, C + n, 0LL) - ans;

    cout << ans << '\n';


    return 0;
}

Compilation message

worst_reporter2.cpp: In function 'std::pair<long long int, long long int> getBest(int, int)':
worst_reporter2.cpp:90:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   90 |         int mid = lx + rx >> 1;
      |                   ~~~^~~~
worst_reporter2.cpp: In function 'void dfs(int)':
worst_reporter2.cpp:157:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |     for (int i = 1; i < adj[v].size(); ++i) {
      |                     ~~^~~~~~~~~~~~~~~
worst_reporter2.cpp:164:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |         for (int x = 0; x < inside[to].size(); ++x) {
      |                         ~~^~~~~~~~~~~~~~~~~~~
worst_reporter2.cpp:174:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |     for (int i = 0, j = 0; i < ours.size(); i = j) {
      |                            ~~^~~~~~~~~~~~~
worst_reporter2.cpp:176:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  176 |         while (j < ours.size() && H[ours[i]] == H[ours[j]]) {
      |                ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 20 ms 13964 KB Output is correct
6 Correct 15 ms 11604 KB Output is correct
7 Correct 11 ms 10708 KB Output is correct
8 Correct 26 ms 14076 KB Output is correct
9 Correct 15 ms 11616 KB Output is correct
10 Correct 11 ms 10828 KB Output is correct
11 Correct 9 ms 10324 KB Output is correct
12 Correct 10 ms 11348 KB Output is correct
13 Correct 9 ms 11092 KB Output is correct
14 Correct 13 ms 11060 KB Output is correct
15 Correct 10 ms 10708 KB Output is correct
16 Correct 28 ms 15948 KB Output is correct
17 Correct 19 ms 12092 KB Output is correct
18 Correct 8 ms 10324 KB Output is correct
19 Correct 12 ms 11732 KB Output is correct
20 Correct 13 ms 11196 KB Output is correct
21 Correct 9 ms 10836 KB Output is correct
22 Correct 17 ms 12104 KB Output is correct
23 Correct 11 ms 11220 KB Output is correct
24 Correct 12 ms 11740 KB Output is correct
25 Correct 10 ms 11248 KB Output is correct
26 Correct 10 ms 11348 KB Output is correct
27 Correct 12 ms 11796 KB Output is correct
28 Correct 11 ms 11680 KB Output is correct
29 Correct 12 ms 11604 KB Output is correct
30 Correct 12 ms 10992 KB Output is correct
31 Correct 11 ms 11092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 20 ms 13964 KB Output is correct
6 Correct 15 ms 11604 KB Output is correct
7 Correct 11 ms 10708 KB Output is correct
8 Correct 26 ms 14076 KB Output is correct
9 Correct 15 ms 11616 KB Output is correct
10 Correct 11 ms 10828 KB Output is correct
11 Correct 9 ms 10324 KB Output is correct
12 Correct 10 ms 11348 KB Output is correct
13 Correct 9 ms 11092 KB Output is correct
14 Correct 13 ms 11060 KB Output is correct
15 Correct 10 ms 10708 KB Output is correct
16 Correct 28 ms 15948 KB Output is correct
17 Correct 19 ms 12092 KB Output is correct
18 Correct 8 ms 10324 KB Output is correct
19 Correct 12 ms 11732 KB Output is correct
20 Correct 13 ms 11196 KB Output is correct
21 Correct 9 ms 10836 KB Output is correct
22 Correct 17 ms 12104 KB Output is correct
23 Correct 11 ms 11220 KB Output is correct
24 Correct 12 ms 11740 KB Output is correct
25 Correct 10 ms 11248 KB Output is correct
26 Correct 10 ms 11348 KB Output is correct
27 Correct 12 ms 11796 KB Output is correct
28 Correct 11 ms 11680 KB Output is correct
29 Correct 12 ms 11604 KB Output is correct
30 Correct 12 ms 10992 KB Output is correct
31 Correct 11 ms 11092 KB Output is correct
32 Correct 23 ms 13940 KB Output is correct
33 Correct 1273 ms 305544 KB Output is correct
34 Correct 725 ms 134796 KB Output is correct
35 Correct 1234 ms 299212 KB Output is correct
36 Correct 759 ms 135276 KB Output is correct
37 Correct 315 ms 45396 KB Output is correct
38 Correct 251 ms 33336 KB Output is correct
39 Correct 308 ms 76124 KB Output is correct
40 Correct 186 ms 66896 KB Output is correct
41 Correct 146 ms 66752 KB Output is correct
42 Correct 366 ms 65784 KB Output is correct
43 Correct 222 ms 48548 KB Output is correct
44 Correct 1660 ms 478360 KB Output is correct
45 Correct 940 ms 193944 KB Output is correct
46 Correct 150 ms 41088 KB Output is correct
47 Correct 420 ms 108184 KB Output is correct
48 Correct 250 ms 80568 KB Output is correct
49 Correct 139 ms 60908 KB Output is correct
50 Correct 461 ms 135612 KB Output is correct
51 Correct 275 ms 90496 KB Output is correct
52 Correct 449 ms 109228 KB Output is correct
53 Correct 259 ms 81832 KB Output is correct
54 Correct 215 ms 81588 KB Output is correct
55 Correct 410 ms 107288 KB Output is correct
56 Correct 313 ms 93648 KB Output is correct
57 Correct 287 ms 88380 KB Output is correct
58 Correct 303 ms 67228 KB Output is correct
59 Correct 293 ms 67060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 20 ms 13964 KB Output is correct
6 Correct 15 ms 11604 KB Output is correct
7 Correct 11 ms 10708 KB Output is correct
8 Correct 26 ms 14076 KB Output is correct
9 Correct 15 ms 11616 KB Output is correct
10 Correct 11 ms 10828 KB Output is correct
11 Correct 9 ms 10324 KB Output is correct
12 Correct 10 ms 11348 KB Output is correct
13 Correct 9 ms 11092 KB Output is correct
14 Correct 13 ms 11060 KB Output is correct
15 Correct 10 ms 10708 KB Output is correct
16 Correct 28 ms 15948 KB Output is correct
17 Correct 19 ms 12092 KB Output is correct
18 Correct 8 ms 10324 KB Output is correct
19 Correct 12 ms 11732 KB Output is correct
20 Correct 13 ms 11196 KB Output is correct
21 Correct 9 ms 10836 KB Output is correct
22 Correct 17 ms 12104 KB Output is correct
23 Correct 11 ms 11220 KB Output is correct
24 Correct 12 ms 11740 KB Output is correct
25 Correct 10 ms 11248 KB Output is correct
26 Correct 10 ms 11348 KB Output is correct
27 Correct 12 ms 11796 KB Output is correct
28 Correct 11 ms 11680 KB Output is correct
29 Correct 12 ms 11604 KB Output is correct
30 Correct 12 ms 10992 KB Output is correct
31 Correct 11 ms 11092 KB Output is correct
32 Correct 23 ms 13940 KB Output is correct
33 Correct 1273 ms 305544 KB Output is correct
34 Correct 725 ms 134796 KB Output is correct
35 Correct 1234 ms 299212 KB Output is correct
36 Correct 759 ms 135276 KB Output is correct
37 Correct 315 ms 45396 KB Output is correct
38 Correct 251 ms 33336 KB Output is correct
39 Correct 308 ms 76124 KB Output is correct
40 Correct 186 ms 66896 KB Output is correct
41 Correct 146 ms 66752 KB Output is correct
42 Correct 366 ms 65784 KB Output is correct
43 Correct 222 ms 48548 KB Output is correct
44 Correct 1660 ms 478360 KB Output is correct
45 Correct 940 ms 193944 KB Output is correct
46 Correct 150 ms 41088 KB Output is correct
47 Correct 420 ms 108184 KB Output is correct
48 Correct 250 ms 80568 KB Output is correct
49 Correct 139 ms 60908 KB Output is correct
50 Correct 461 ms 135612 KB Output is correct
51 Correct 275 ms 90496 KB Output is correct
52 Correct 449 ms 109228 KB Output is correct
53 Correct 259 ms 81832 KB Output is correct
54 Correct 215 ms 81588 KB Output is correct
55 Correct 410 ms 107288 KB Output is correct
56 Correct 313 ms 93648 KB Output is correct
57 Correct 287 ms 88380 KB Output is correct
58 Correct 303 ms 67228 KB Output is correct
59 Correct 293 ms 67060 KB Output is correct
60 Correct 6 ms 9732 KB Output is correct
61 Correct 5 ms 9684 KB Output is correct
62 Correct 6 ms 9724 KB Output is correct
63 Correct 5 ms 9684 KB Output is correct
64 Correct 1048 ms 252644 KB Output is correct
65 Correct 641 ms 116880 KB Output is correct
66 Correct 1059 ms 254216 KB Output is correct
67 Correct 631 ms 116092 KB Output is correct
68 Correct 302 ms 46876 KB Output is correct
69 Correct 251 ms 38068 KB Output is correct
70 Correct 307 ms 46420 KB Output is correct
71 Correct 169 ms 29000 KB Output is correct
72 Correct 257 ms 37708 KB Output is correct
73 Correct 142 ms 28928 KB Output is correct
74 Correct 386 ms 87768 KB Output is correct
75 Correct 224 ms 60536 KB Output is correct
76 Correct 136 ms 40852 KB Output is correct
77 Correct 369 ms 87804 KB Output is correct
78 Correct 227 ms 60524 KB Output is correct
79 Correct 690 ms 171428 KB Output is correct
80 Correct 428 ms 85432 KB Output is correct
81 Correct 222 ms 40248 KB Output is correct
82 Correct 265 ms 37736 KB Output is correct
83 Correct 461 ms 136316 KB Output is correct
84 Correct 437 ms 74084 KB Output is correct
85 Correct 395 ms 74196 KB Output is correct
86 Correct 379 ms 71108 KB Output is correct
87 Correct 409 ms 74088 KB Output is correct
88 Correct 374 ms 73772 KB Output is correct