Submission #205952

# Submission time Handle Problem Language Result Execution time Memory
205952 2020-03-01T11:08:43 Z himyu Fireworks (APIO16_fireworks) C++17
55 / 100
2000 ms 23640 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

using i64 = long long;
using d64 = long double;
using pi = pair<int, int>;
using pli = pair<i64, i64>;
using ti = tuple<int, int, int>;
using tli = tuple<i64, i64, i64>;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define iterall(cont) cont.begin(), cont.end()
#define prec(n) setprecision(n) << fixed

vector<pli> adj[300001];
const i64 inf = numeric_limits<i64>::max() / 3;

inline i64 ab(i64 x) { return x > 0 ? x : -x; }

class slope {
   public:
    vector<i64> bp;
    i64 sac, pen;

    slope() {
        bp.clear();
        sac = 0, pen = 0;
    }

    void push(i64 x) { 
        if (bp.empty()) {
            bp.emplace_back(x);
            bp.emplace_back(x);
            ++sac;
            return;
        }

        while (bp.size() > sac + 1) bp.pop_back();
        bp[sac] += x;
        bp[sac - 1] += x;
    }

    i64 get(i64 x) const {
        if (bp.empty()) return 0;

        i64 ret = 0;
        if (x > bp[sac])
            for (int i = sac; i < bp.size(); i++) ret += max(x - bp[i], 0LL);
        if (x < bp[sac - 1])
            for (int i = sac - 1; i >= 0; i--) ret += max(bp[i] - x, 0LL);
        return ret;
    }

    void print() {
        cout << "sac: " << sac << endl;
        cout << "pen: " << pen << endl;
        cout << "bp: ";
        for (auto el : bp) cout << el << " ";
        cout << endl;
    }
};

slope mer(const vector<slope> &sl) {
    auto ret = slope();

    for (auto &sp : sl) {
        ret.sac += sp.sac;
        ret.pen += sp.pen;
    }

    for (auto &sp : sl) {
        for (auto el : sp.bp) {
            ret.bp.emplace_back(el);
        }
    }
    sort(iterall(ret.bp));

    for (auto &sp : sl) ret.pen += sp.get(ret.bp[ret.sac]);
    return ret;
}

slope solve(int h, int p = -1) {
    vector<slope> tomer;
    for (auto [t, wf] : adj[h]) {
        if (t == p) continue;
        auto chslp = solve(t, h);
        chslp.push(wf);
        tomer.emplace_back(chslp);
    }

    return mer(tomer);
}

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

    int N, M;
    cin >> N >> M;

    for (int i = 2; i <= N + M; i++) {
        i64 j, w;
        cin >> j >> w;

        adj[i].emplace_back(j, w);
        adj[j].emplace_back(i, w);
    }

    cout << solve(1).pen << endl;

    return 0;
}

Compilation message

fireworks.cpp: In member function 'void slope::push(i64)':
fireworks.cpp:45:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (bp.size() > sac + 1) bp.pop_back();
                ~~~~~~~~~~^~~~~~~~~
fireworks.cpp: In member function 'i64 slope::get(i64) const':
fireworks.cpp:55:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int i = sac; i < bp.size(); i++) ret += max(x - bp[i], 0LL);
                               ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 7416 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 10 ms 7416 KB Output is correct
6 Correct 9 ms 7464 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 10 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7544 KB Output is correct
2 Correct 12 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 10 ms 7416 KB Output is correct
5 Correct 9 ms 7416 KB Output is correct
6 Correct 9 ms 7416 KB Output is correct
7 Correct 10 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 9 ms 7416 KB Output is correct
11 Correct 9 ms 7408 KB Output is correct
12 Correct 9 ms 7416 KB Output is correct
13 Correct 9 ms 7416 KB Output is correct
14 Correct 10 ms 7452 KB Output is correct
15 Correct 10 ms 7416 KB Output is correct
16 Correct 10 ms 7416 KB Output is correct
17 Correct 11 ms 7416 KB Output is correct
18 Correct 12 ms 7416 KB Output is correct
19 Correct 10 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 7416 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 10 ms 7416 KB Output is correct
6 Correct 9 ms 7464 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 10 ms 7416 KB Output is correct
11 Correct 9 ms 7544 KB Output is correct
12 Correct 12 ms 7416 KB Output is correct
13 Correct 9 ms 7416 KB Output is correct
14 Correct 10 ms 7416 KB Output is correct
15 Correct 9 ms 7416 KB Output is correct
16 Correct 9 ms 7416 KB Output is correct
17 Correct 10 ms 7416 KB Output is correct
18 Correct 10 ms 7416 KB Output is correct
19 Correct 9 ms 7416 KB Output is correct
20 Correct 9 ms 7416 KB Output is correct
21 Correct 9 ms 7408 KB Output is correct
22 Correct 9 ms 7416 KB Output is correct
23 Correct 9 ms 7416 KB Output is correct
24 Correct 10 ms 7452 KB Output is correct
25 Correct 10 ms 7416 KB Output is correct
26 Correct 10 ms 7416 KB Output is correct
27 Correct 11 ms 7416 KB Output is correct
28 Correct 12 ms 7416 KB Output is correct
29 Correct 10 ms 7416 KB Output is correct
30 Correct 10 ms 7416 KB Output is correct
31 Correct 11 ms 7416 KB Output is correct
32 Correct 11 ms 7544 KB Output is correct
33 Correct 16 ms 7544 KB Output is correct
34 Correct 13 ms 7672 KB Output is correct
35 Correct 18 ms 7672 KB Output is correct
36 Correct 18 ms 7672 KB Output is correct
37 Correct 15 ms 7800 KB Output is correct
38 Correct 20 ms 7800 KB Output is correct
39 Correct 16 ms 7800 KB Output is correct
40 Correct 14 ms 8568 KB Output is correct
41 Correct 13 ms 8568 KB Output is correct
42 Correct 17 ms 7672 KB Output is correct
43 Correct 114 ms 8284 KB Output is correct
44 Correct 98 ms 8328 KB Output is correct
45 Correct 97 ms 8272 KB Output is correct
46 Correct 14 ms 7904 KB Output is correct
47 Correct 13 ms 7800 KB Output is correct
48 Correct 13 ms 7840 KB Output is correct
49 Correct 12 ms 7928 KB Output is correct
50 Correct 14 ms 7868 KB Output is correct
51 Correct 15 ms 7972 KB Output is correct
52 Correct 15 ms 7868 KB Output is correct
53 Correct 14 ms 7896 KB Output is correct
54 Correct 14 ms 8300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 7416 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 10 ms 7416 KB Output is correct
6 Correct 9 ms 7464 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 10 ms 7416 KB Output is correct
11 Correct 9 ms 7544 KB Output is correct
12 Correct 12 ms 7416 KB Output is correct
13 Correct 9 ms 7416 KB Output is correct
14 Correct 10 ms 7416 KB Output is correct
15 Correct 9 ms 7416 KB Output is correct
16 Correct 9 ms 7416 KB Output is correct
17 Correct 10 ms 7416 KB Output is correct
18 Correct 10 ms 7416 KB Output is correct
19 Correct 9 ms 7416 KB Output is correct
20 Correct 9 ms 7416 KB Output is correct
21 Correct 9 ms 7408 KB Output is correct
22 Correct 9 ms 7416 KB Output is correct
23 Correct 9 ms 7416 KB Output is correct
24 Correct 10 ms 7452 KB Output is correct
25 Correct 10 ms 7416 KB Output is correct
26 Correct 10 ms 7416 KB Output is correct
27 Correct 11 ms 7416 KB Output is correct
28 Correct 12 ms 7416 KB Output is correct
29 Correct 10 ms 7416 KB Output is correct
30 Correct 10 ms 7416 KB Output is correct
31 Correct 11 ms 7416 KB Output is correct
32 Correct 11 ms 7544 KB Output is correct
33 Correct 16 ms 7544 KB Output is correct
34 Correct 13 ms 7672 KB Output is correct
35 Correct 18 ms 7672 KB Output is correct
36 Correct 18 ms 7672 KB Output is correct
37 Correct 15 ms 7800 KB Output is correct
38 Correct 20 ms 7800 KB Output is correct
39 Correct 16 ms 7800 KB Output is correct
40 Correct 14 ms 8568 KB Output is correct
41 Correct 13 ms 8568 KB Output is correct
42 Correct 17 ms 7672 KB Output is correct
43 Correct 114 ms 8284 KB Output is correct
44 Correct 98 ms 8328 KB Output is correct
45 Correct 97 ms 8272 KB Output is correct
46 Correct 14 ms 7904 KB Output is correct
47 Correct 13 ms 7800 KB Output is correct
48 Correct 13 ms 7840 KB Output is correct
49 Correct 12 ms 7928 KB Output is correct
50 Correct 14 ms 7868 KB Output is correct
51 Correct 15 ms 7972 KB Output is correct
52 Correct 15 ms 7868 KB Output is correct
53 Correct 14 ms 7896 KB Output is correct
54 Correct 14 ms 8300 KB Output is correct
55 Correct 38 ms 8440 KB Output is correct
56 Correct 291 ms 11344 KB Output is correct
57 Correct 599 ms 13960 KB Output is correct
58 Correct 692 ms 15908 KB Output is correct
59 Correct 1591 ms 18856 KB Output is correct
60 Correct 1449 ms 20992 KB Output is correct
61 Execution timed out 2029 ms 23640 KB Time limit exceeded
62 Halted 0 ms 0 KB -