Submission #373724

# Submission time Handle Problem Language Result Execution time Memory
373724 2021-03-05T13:57:16 Z KoD Fireworks (APIO16_fireworks) C++17
100 / 100
1128 ms 94760 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;
using ll = long long;

template <class F>
struct RecLambda: private F {
    explicit RecLambda(F &&f): F(std::forward<F>(f)) { }
    template <class... Args>
    decltype(auto) operator () (Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);        
    }
};

struct HeapNode;
using Heap = std::unique_ptr<HeapNode>;

struct HeapNode {
    ll value;
    Heap left, right;
    HeapNode(const ll value): value(value), left(), right() { }
};

Heap merge(Heap left, Heap right) {
    if (!left) return right;
    if (!right) return left;
    if (left -> value < right -> value) {
        std::swap(left, right);
    }
    left -> right = merge(std::move(left -> right), std::move(right));
    std::swap(left -> left, left -> right);
    return left;
}

void push(Heap &heap, const ll value) {
    heap = merge(std::move(heap), std::make_unique<HeapNode>(value));
}

ll top(Heap &heap) {
    return heap -> value;
}

ll pop(Heap &heap) {
    const auto ret = top(heap);
    heap = merge(std::move(heap -> left), std::move(heap -> right));
    return ret;
}

struct SlopeTrick {
    ll last_slope, last_y;
    Heap changes;

    SlopeTrick(): last_slope(), last_y(), changes() { 
        push(changes, 0);
    }
    
    void absorb(SlopeTrick &&other) {
        const auto x1 = top(changes);
        const auto x2 = top(other.changes);
        last_y += other.last_y + (x1 < x2 ? last_slope * (x2 - x1) : other.last_slope * (x1 - x2));
        last_slope += other.last_slope;
        changes = merge(std::move(changes), std::move(other.changes));
    }

    bool pop_last() {
        const auto x1 = pop(changes);
        if (!changes) return false;
        const auto x2 = top(changes);
        last_slope -= 1;
        last_y += last_slope * (x2 - x1);
        return true;
    }
};

int main() {
    int N, M;
    std::cin >> N >> M;
    Vec<Vec<std::pair<int, int>>> children(N + M);
    for (int i = 1; i < N + M; ++i) {
        int p, c;
        std::cin >> p >> c;
        p -= 1;
        children[p].emplace_back(i, c);
    }
    Vec<SlopeTrick> data(N);
    RecLambda([&](auto dfs, const int u) -> void {
        for (const auto [v, c]: children[u]) {
            if (v >= N) {
                SlopeTrick trick;
                trick.last_slope = 1;
                push(trick.changes, c);
                push(trick.changes, c);
                data[u].absorb(std::move(trick));
            }
            else {
                dfs(v);
                while (data[v].last_slope > 1) {
                    data[v].pop_last();
                }
                const auto x1 = pop(data[v].changes);
                const auto x2 = pop(data[v].changes);
                push(data[v].changes, x2 + c);
                push(data[v].changes, x1 + c);
                data[u].absorb(std::move(data[v]));
            }
        }
    })(0);
    ll ans = data[0].last_y;
    while (data[0].pop_last()) {
        ans = std::min(ans, data[0].last_y);
    }
    std::cout << ans << '\n';
    return 0;    
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 516 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 516 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 492 KB Output is correct
32 Correct 3 ms 492 KB Output is correct
33 Correct 3 ms 620 KB Output is correct
34 Correct 4 ms 620 KB Output is correct
35 Correct 6 ms 748 KB Output is correct
36 Correct 6 ms 748 KB Output is correct
37 Correct 8 ms 876 KB Output is correct
38 Correct 7 ms 876 KB Output is correct
39 Correct 7 ms 876 KB Output is correct
40 Correct 7 ms 1900 KB Output is correct
41 Correct 7 ms 1900 KB Output is correct
42 Correct 7 ms 1004 KB Output is correct
43 Correct 8 ms 1388 KB Output is correct
44 Correct 10 ms 1260 KB Output is correct
45 Correct 9 ms 1260 KB Output is correct
46 Correct 8 ms 876 KB Output is correct
47 Correct 8 ms 876 KB Output is correct
48 Correct 8 ms 876 KB Output is correct
49 Correct 9 ms 876 KB Output is correct
50 Correct 8 ms 876 KB Output is correct
51 Correct 7 ms 876 KB Output is correct
52 Correct 8 ms 876 KB Output is correct
53 Correct 8 ms 876 KB Output is correct
54 Correct 9 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 516 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 492 KB Output is correct
32 Correct 3 ms 492 KB Output is correct
33 Correct 3 ms 620 KB Output is correct
34 Correct 4 ms 620 KB Output is correct
35 Correct 6 ms 748 KB Output is correct
36 Correct 6 ms 748 KB Output is correct
37 Correct 8 ms 876 KB Output is correct
38 Correct 7 ms 876 KB Output is correct
39 Correct 7 ms 876 KB Output is correct
40 Correct 7 ms 1900 KB Output is correct
41 Correct 7 ms 1900 KB Output is correct
42 Correct 7 ms 1004 KB Output is correct
43 Correct 8 ms 1388 KB Output is correct
44 Correct 10 ms 1260 KB Output is correct
45 Correct 9 ms 1260 KB Output is correct
46 Correct 8 ms 876 KB Output is correct
47 Correct 8 ms 876 KB Output is correct
48 Correct 8 ms 876 KB Output is correct
49 Correct 9 ms 876 KB Output is correct
50 Correct 8 ms 876 KB Output is correct
51 Correct 7 ms 876 KB Output is correct
52 Correct 8 ms 876 KB Output is correct
53 Correct 8 ms 876 KB Output is correct
54 Correct 9 ms 1004 KB Output is correct
55 Correct 18 ms 1936 KB Output is correct
56 Correct 78 ms 5996 KB Output is correct
57 Correct 143 ms 9580 KB Output is correct
58 Correct 191 ms 12024 KB Output is correct
59 Correct 254 ms 15724 KB Output is correct
60 Correct 354 ms 19308 KB Output is correct
61 Correct 391 ms 21996 KB Output is correct
62 Correct 437 ms 23916 KB Output is correct
63 Correct 545 ms 28396 KB Output is correct
64 Correct 600 ms 29932 KB Output is correct
65 Correct 539 ms 94760 KB Output is correct
66 Correct 531 ms 94700 KB Output is correct
67 Correct 481 ms 34028 KB Output is correct
68 Correct 582 ms 60652 KB Output is correct
69 Correct 637 ms 55916 KB Output is correct
70 Correct 627 ms 55924 KB Output is correct
71 Correct 754 ms 30060 KB Output is correct
72 Correct 751 ms 30316 KB Output is correct
73 Correct 795 ms 29932 KB Output is correct
74 Correct 788 ms 29932 KB Output is correct
75 Correct 785 ms 29292 KB Output is correct
76 Correct 822 ms 29112 KB Output is correct
77 Correct 862 ms 28520 KB Output is correct
78 Correct 956 ms 28968 KB Output is correct
79 Correct 1128 ms 37972 KB Output is correct