# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
40845 | ssnsarang2023 | Fireworks (APIO16_fireworks) | C++14 | 245 ms | 132484 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
#define SZ(x) ((int)x.size())
const int N = (int)3e5+5;
struct data {
ll a, b;
priority_queue<ll> *pq;
} a[N];
data merge(const data &x, const data &y) {
data re;
re.a = x.a + y.a;
re.b = x.b + y.b;
if (x.pq->size() > y.pq->size()) {
re.pq = x.pq;
while (y.pq->size()) {
re.pq->push(y.pq->top());
y.pq->pop();
}
} else {
re.pq = y.pq;
while (x.pq->size()) {
re.pq->push(x.pq->top());
x.pq->pop();
}
}
return re;
}
int n, m, p[N];
ll c[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 2; i <= n+m; ++i) scanf("%d%lld", &p[i], &c[i]);
for (int i = 1; i <= n+m; ++i) {
a[i].pq = new priority_queue<ll>();
a[i].a = a[i].b = 0;
}
for (int i = n+m; i >= n+1; --i) {
a[i].a = 1;
a[i].b = -c[i];
a[i].pq->push(c[i]);
a[i].pq->push(c[i]);
a[p[i]] = merge(a[p[i]], a[i]);
}
for (int i = n; i >= 2; --i) {
while (a[i].a > 1) {
--a[i].a;
a[i].b += a[i].pq->top();
a[i].pq->pop();
}
ll x = a[i].pq->top();
a[i].pq->pop();
ll y = a[i].pq->top();
a[i].pq->pop();
a[i].pq->push(x + c[i]);
a[i].pq->push(y + c[i]);
a[i].b -= a[i].a*c[i];
a[p[i]] = merge(a[p[i]], a[i]);
}
while (a[1].a > 0) {
--a[1].a;
a[1].b += a[1].pq->top();
a[1].pq->pop();
}
printf("%lld", a[1].b);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |