Submission #939264

# Submission time Handle Problem Language Result Execution time Memory
939264 2024-03-06T07:48:14 Z Alcabel Fireworks (APIO16_fireworks) C++17
7 / 100
11 ms 32288 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 3e5;
priority_queue<long long> down[maxn];
priority_queue<long long, vector<long long>, greater<long long>> up[maxn];
long long val[maxn], modUp[maxn];
int slope[maxn];
vector<int> g[maxn];
int par[maxn], c[maxn];

/*void print(int v) {
    vector<long long> res;
    {
        auto tmp = down[v];
        while (!tmp.empty()) {
            res.emplace_back(tmp.top());
            tmp.pop();
        }
        reverse(res.begin(), res.end());
    }
    {
        auto tmp = up[v];
        while (!tmp.empty()) {
            res.emplace_back(tmp.top() + modUp[v]);
            tmp.pop();
        }
        for (const int& x : res) {
            cerr << x << ' ';
        }
        cerr << '\n';
    }
}*/

void dfs(int v) {
    if (g[v].empty()) {
        val[v] = c[v];
        slope[v] = -1;
        up[v].emplace(c[v]);
        up[v].emplace(c[v]);
        return;
    }
    for (const int& u : g[v]) {
        dfs(u);
        if (down[v].size() + up[v].size() < down[u].size() + up[u].size()) {
            swap(down[v], down[u]);
            swap(up[v], up[u]);
            swap(val[v], val[u]);
            swap(modUp[v], modUp[u]);
            swap(slope[v], slope[u]);
        }
        slope[v] += slope[u];
        val[v] += val[u];
        while (!down[u].empty()) {
            down[v].emplace(down[u].top());
            down[u].pop();
        }
        while (!up[u].empty()) {
            up[v].emplace(up[u].top() + modUp[u] - modUp[v]);
            up[u].pop();
        }
    }
    if (!up[v].empty()) {
        while (!down[v].empty() && down[v].top() > up[v].top() + modUp[v]) {
            up[v].emplace(down[v].top() - modUp[v]);
            down[v].pop();
        }
    }
    while (slope[v] + (int)down[v].size() > -1) {
        up[v].emplace(down[v].top() - modUp[v]);
        down[v].pop();
    }
    while (slope[v] + (int)down[v].size() < -1) {
        down[v].emplace(up[v].top() + modUp[v]);
        up[v].pop();
    }
    // cerr << "v: " << v + 1 << " before:\n";
    // print(v);
    if (par[v] != -1) {
        val[v] += c[v];
        modUp[v] += c[v];
        down[v].emplace(up[v].top() + modUp[v]);
        up[v].pop();
        down[v].emplace(up[v].top() + modUp[v]);
        up[v].pop();
        modUp[v] += c[v];
    }
    // cerr << "v: " << v + 1 << ", val: " << val[v] << ", slope: " << slope[v] << '\n';
    // print(v);
}

void solve() {
    int n, m;
    cin >> n >> m;
    par[0] = -1, c[0] = 0;
    for (int i = 1; i < n + m; ++i) {
        cin >> par[i] >> c[i];
        --par[i];
        g[par[i]].emplace_back(i);
    }
    dfs(0);
    assert(slope[0] + (int)down[0].size() == -1);
    down[0].emplace(up[0].top() + modUp[0]);
    up[0].pop();
    long long ans = val[0], last = down[0].top();
    // cerr << "val: " << val[0] << '\n';
    while (!down[0].empty()) {
        ans += (slope[0] + (int)down[0].size()) * (last - down[0].top());
        last = down[0].top();
        down[0].pop();
    }
    ans += slope[0] * last;
    cout << ans << '\n';
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
        cerr << "-----------\n";
        cout << "-----------\n";
    }
#else
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
#endif
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 32088 KB Output is correct
2 Correct 6 ms 32092 KB Output is correct
3 Correct 7 ms 32092 KB Output is correct
4 Correct 7 ms 32092 KB Output is correct
5 Correct 7 ms 32092 KB Output is correct
6 Correct 7 ms 32088 KB Output is correct
7 Correct 7 ms 32092 KB Output is correct
8 Correct 7 ms 32092 KB Output is correct
9 Correct 7 ms 32280 KB Output is correct
10 Correct 8 ms 32224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 32088 KB Output is correct
2 Correct 7 ms 32092 KB Output is correct
3 Correct 7 ms 32092 KB Output is correct
4 Correct 7 ms 32092 KB Output is correct
5 Correct 7 ms 32288 KB Output is correct
6 Incorrect 7 ms 32092 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 32088 KB Output is correct
2 Correct 6 ms 32092 KB Output is correct
3 Correct 7 ms 32092 KB Output is correct
4 Correct 7 ms 32092 KB Output is correct
5 Correct 7 ms 32092 KB Output is correct
6 Correct 7 ms 32088 KB Output is correct
7 Correct 7 ms 32092 KB Output is correct
8 Correct 7 ms 32092 KB Output is correct
9 Correct 7 ms 32280 KB Output is correct
10 Correct 8 ms 32224 KB Output is correct
11 Correct 7 ms 32088 KB Output is correct
12 Correct 7 ms 32092 KB Output is correct
13 Correct 7 ms 32092 KB Output is correct
14 Correct 7 ms 32092 KB Output is correct
15 Correct 7 ms 32288 KB Output is correct
16 Incorrect 7 ms 32092 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 32088 KB Output is correct
2 Correct 6 ms 32092 KB Output is correct
3 Correct 7 ms 32092 KB Output is correct
4 Correct 7 ms 32092 KB Output is correct
5 Correct 7 ms 32092 KB Output is correct
6 Correct 7 ms 32088 KB Output is correct
7 Correct 7 ms 32092 KB Output is correct
8 Correct 7 ms 32092 KB Output is correct
9 Correct 7 ms 32280 KB Output is correct
10 Correct 8 ms 32224 KB Output is correct
11 Correct 7 ms 32088 KB Output is correct
12 Correct 7 ms 32092 KB Output is correct
13 Correct 7 ms 32092 KB Output is correct
14 Correct 7 ms 32092 KB Output is correct
15 Correct 7 ms 32288 KB Output is correct
16 Incorrect 7 ms 32092 KB Output isn't correct
17 Halted 0 ms 0 KB -