This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
const int MAXN = 300005;
const llint INF = 1000000000000000LL;
int n, m;
llint p[MAXN], c[MAXN];
llint a[MAXN], b[MAXN];
vector <int> v[MAXN];
priority_queue <llint> hull[MAXN];
void add (int x, int len) {
int slope = a[x] + hull[x].size();
llint x1, x2;
while (slope >= 0) {
if (slope == 1) x2 = hull[x].top();
if (slope == 0) x1 = hull[x].top();
hull[x].pop(); slope--;
}
b[x] += len;
hull[x].push(x1 + len);
hull[x].push(x2 + len);
}
void spoji (int x, int y) {
if (hull[x].size() > hull[y].size()) swap(hull[x], hull[y]);
a[y] += a[x];
b[y] += b[x];
while (!hull[x].empty()) {
hull[y].push(hull[x].top());
hull[x].pop();
}
}
void dfs (int x) {
if (x >= n + 1) {
a[x] = -1; b[x] = 0;
hull[x].push(0);
hull[x].push(0);
return;
}
for (int i=0; i<v[x].size(); i++) {
int sus = v[x][i];
dfs(sus);
add(sus, c[sus]);
spoji(sus, x);
}
}
llint upit (int x) {
vector <llint> r;
while (!hull[x].empty()) {
r.push_back(hull[x].top());
hull[x].pop();
}
llint val = b[x], slope = a[x], pos = 0;
llint sol = val;
while (!r.empty()) {
val += slope * (r.back() - pos);
sol = min(sol, val);
pos = r.back();
r.pop_back();
slope++;
}
return sol;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 2; i <= n+m; i++) {
cin >> p[i] >> c[i];
v[p[i]].push_back(i);
}
dfs(1);
cout << upit(1);
return 0;
}
Compilation message (stderr)
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++) {
~^~~~~~~~~~~~
fireworks.cpp: In function 'void add(int, int)':
fireworks.cpp:26:21: warning: 'x2' may be used uninitialized in this function [-Wmaybe-uninitialized]
hull[x].push(x2 + len);
~~~^~~~~
fireworks.cpp:25:21: warning: 'x1' may be used uninitialized in this function [-Wmaybe-uninitialized]
hull[x].push(x1 + len);
~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |