Submission #1080778

# Submission time Handle Problem Language Result Execution time Memory
1080778 2024-08-29T14:04:44 Z _callmelucian Magic Tree (CEOI19_magictree) C++14
100 / 100
627 ms 43212 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
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())

struct node {
    ll tr, lazy;
    node *lpt, *rpt;

    node() : tr(0), lazy(0), lpt(nullptr), rpt(nullptr) {}

    ll get() { return tr + lazy; }

    void pushDown () {
        if (lpt == nullptr) lpt = new node();
        if (rpt == nullptr) rpt = new node();
        lpt->lazy += lazy, rpt->lazy += lazy;
        tr += lazy, lazy = 0;
    }

    void update (int a, int b, ll incr, int l, int r) {
        if (b < l || r < a) return;
        if (a <= l && r <= b) {
            lazy += incr;
            return;
        }
        pushDown();
        int mid = (l + r) >> 1;
        lpt->update(a, b, incr, l, mid);
        rpt->update(a, b, incr, mid + 1, r);
        tr = max(lpt->get(), rpt->get());
    }

    bool intr (int a, int b, int l, int r) {
        if (b < l || r < a) return 0;
        return 1;
    }

    ll query (int a, int b, int l, int r) {
        if (b < l || r < a) return 0;
        if (a <= l && r <= b) return get();
        pushDown();
        int mid = (l + r) >> 1;
        return max(lpt->query(a, b, l, mid), rpt->query(a, b, mid + 1, r));
    }

    void destruct() {
        if (lpt != nullptr) {
            lpt->destruct();
            delete lpt; lpt = nullptr;
        }
        if (rpt != nullptr) {
            rpt->destruct();
            delete rpt; rpt = nullptr;
        }
    }
};

const int mn = 1e5 + 5;
ll dp[mn], weight[mn];
vector<int> adj[mn];
int ripe[mn], n, k;

struct item {
    node tree;
    vector<int> vec;

    item() : tree(), vec(0) {}

    void add (int id, ll val) {
        vec.push_back(id);
        ll incr = max(0LL, val - tree.query(id, id, 1, k));
        if (incr) tree.update(id, id, incr, 1, k);
    }

    void unite (item &o) {
        o.vec.push_back(k + 1);
        sort(all(o.vec)); filter(o.vec);

        vector<ll> save(o.vec.size() - 1);
        for (int i = 0; i + 1 < o.vec.size(); i++)
            save[i] = tree.query(1, o.vec[i], 1, k);

        ll best = 0;
        for (int i = 0; i + 1 < o.vec.size(); i++) {
            ll cur = o.tree.query(o.vec[i], o.vec[i], 1, k);
            best = max(best, cur);
            tree.update(o.vec[i], o.vec[i + 1] - 1, best, 1, k);
            add(o.vec[i], cur + save[i]);
        }

        o.vec.clear(); o.tree.destruct();
    }

    ll query (int l, int r) { return tree.query(l, r, 1, k); }

    void printCand() {
        sort(all(vec)); filter(vec);
        for (int u : vec)
            cout << "(" << u << ", " << tree.query(u, u, 1, k) << ") ";
        cout << "\n";
    }

    int sz() { return vec.size(); }
} node[mn];

void dfs (int u, int p) {
    for (int v : adj[u]) dfs(v, u);

    dp[u] = node[u].query(1, ripe[u]) + weight[u];
    node[u].add(ripe[u], dp[u]);

    if (node[u].sz() > node[p].sz()) swap(node[u], node[p]);
    if (u != p)
        node[p].unite(node[u]);
}

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

    int m; cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) ripe[i] = 1;
    for (int i = 2; i <= n; i++) {
        int p; cin >> p;
        adj[p].push_back(i);
    }

    while (m--) {
        int u; cin >> u;
        cin >> ripe[u] >> weight[u];
    }

    dfs(1, 1);
    cout << node[1].query(1, k);

    return 0;
}

Compilation message

magictree.cpp: In member function 'void item::unite(item&)':
magictree.cpp:87:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         for (int i = 0; i + 1 < o.vec.size(); i++)
      |                         ~~~~~~^~~~~~~~~~~~~~
magictree.cpp:91:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for (int i = 0; i + 1 < o.vec.size(); i++) {
      |                         ~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8284 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 4 ms 8280 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8284 KB Output is correct
6 Correct 5 ms 8284 KB Output is correct
7 Correct 4 ms 8068 KB Output is correct
8 Correct 3 ms 8280 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 25848 KB Output is correct
2 Correct 99 ms 31184 KB Output is correct
3 Correct 567 ms 34004 KB Output is correct
4 Correct 198 ms 24788 KB Output is correct
5 Correct 221 ms 26828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8536 KB Output is correct
2 Correct 4 ms 8560 KB Output is correct
3 Correct 4 ms 8540 KB Output is correct
4 Correct 87 ms 43212 KB Output is correct
5 Correct 78 ms 43208 KB Output is correct
6 Correct 97 ms 43048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 17492 KB Output is correct
2 Correct 53 ms 17492 KB Output is correct
3 Correct 56 ms 24620 KB Output is correct
4 Correct 39 ms 15824 KB Output is correct
5 Correct 43 ms 34532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8284 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 4 ms 8280 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8284 KB Output is correct
6 Correct 5 ms 8284 KB Output is correct
7 Correct 4 ms 8068 KB Output is correct
8 Correct 3 ms 8280 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Correct 101 ms 17236 KB Output is correct
11 Correct 80 ms 16980 KB Output is correct
12 Correct 54 ms 23960 KB Output is correct
13 Correct 61 ms 15056 KB Output is correct
14 Correct 47 ms 33996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 10328 KB Output is correct
2 Correct 102 ms 16468 KB Output is correct
3 Correct 101 ms 16268 KB Output is correct
4 Correct 93 ms 16468 KB Output is correct
5 Correct 103 ms 14800 KB Output is correct
6 Correct 92 ms 19664 KB Output is correct
7 Correct 59 ms 24016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8284 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 4 ms 8280 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8284 KB Output is correct
6 Correct 5 ms 8284 KB Output is correct
7 Correct 4 ms 8068 KB Output is correct
8 Correct 3 ms 8280 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Correct 4 ms 8536 KB Output is correct
11 Correct 4 ms 8560 KB Output is correct
12 Correct 4 ms 8540 KB Output is correct
13 Correct 87 ms 43212 KB Output is correct
14 Correct 78 ms 43208 KB Output is correct
15 Correct 97 ms 43048 KB Output is correct
16 Correct 101 ms 17236 KB Output is correct
17 Correct 80 ms 16980 KB Output is correct
18 Correct 54 ms 23960 KB Output is correct
19 Correct 61 ms 15056 KB Output is correct
20 Correct 47 ms 33996 KB Output is correct
21 Correct 64 ms 10836 KB Output is correct
22 Correct 215 ms 18008 KB Output is correct
23 Correct 381 ms 30292 KB Output is correct
24 Correct 616 ms 32196 KB Output is correct
25 Correct 190 ms 24020 KB Output is correct
26 Correct 206 ms 36044 KB Output is correct
27 Correct 158 ms 40420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8284 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 4 ms 8280 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8284 KB Output is correct
6 Correct 5 ms 8284 KB Output is correct
7 Correct 4 ms 8068 KB Output is correct
8 Correct 3 ms 8280 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Correct 310 ms 25848 KB Output is correct
11 Correct 99 ms 31184 KB Output is correct
12 Correct 567 ms 34004 KB Output is correct
13 Correct 198 ms 24788 KB Output is correct
14 Correct 221 ms 26828 KB Output is correct
15 Correct 4 ms 8536 KB Output is correct
16 Correct 4 ms 8560 KB Output is correct
17 Correct 4 ms 8540 KB Output is correct
18 Correct 87 ms 43212 KB Output is correct
19 Correct 78 ms 43208 KB Output is correct
20 Correct 97 ms 43048 KB Output is correct
21 Correct 53 ms 17492 KB Output is correct
22 Correct 53 ms 17492 KB Output is correct
23 Correct 56 ms 24620 KB Output is correct
24 Correct 39 ms 15824 KB Output is correct
25 Correct 43 ms 34532 KB Output is correct
26 Correct 101 ms 17236 KB Output is correct
27 Correct 80 ms 16980 KB Output is correct
28 Correct 54 ms 23960 KB Output is correct
29 Correct 61 ms 15056 KB Output is correct
30 Correct 47 ms 33996 KB Output is correct
31 Correct 31 ms 10328 KB Output is correct
32 Correct 102 ms 16468 KB Output is correct
33 Correct 101 ms 16268 KB Output is correct
34 Correct 93 ms 16468 KB Output is correct
35 Correct 103 ms 14800 KB Output is correct
36 Correct 92 ms 19664 KB Output is correct
37 Correct 59 ms 24016 KB Output is correct
38 Correct 64 ms 10836 KB Output is correct
39 Correct 215 ms 18008 KB Output is correct
40 Correct 381 ms 30292 KB Output is correct
41 Correct 616 ms 32196 KB Output is correct
42 Correct 190 ms 24020 KB Output is correct
43 Correct 206 ms 36044 KB Output is correct
44 Correct 158 ms 40420 KB Output is correct
45 Correct 60 ms 10840 KB Output is correct
46 Correct 220 ms 18300 KB Output is correct
47 Correct 388 ms 27684 KB Output is correct
48 Correct 627 ms 35132 KB Output is correct
49 Correct 214 ms 24784 KB Output is correct
50 Correct 234 ms 36756 KB Output is correct
51 Correct 166 ms 41164 KB Output is correct