제출 #140568

#제출 시각아이디문제언어결과실행 시간메모리
140568eriksuenderhaufFireworks (APIO16_fireworks)C++11
100 / 100
263 ms67172 KiB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
priority_queue<ll> pq[MAXN];
int par[MAXN];
ll a[MAXN], b[MAXN], w[MAXN];

void merge(int p, int u) {
	if (pq[p].size() < pq[u].size()) swap(pq[p], pq[u]);
	while (!pq[u].empty()) {
		pq[p].push(pq[u].top());
		pq[u].pop();
	}
	a[p] += a[u]; b[p] += b[u];
}

int main() {
	int n, m; scanf("%d %d", &n, &m);
	for (int i = 2; i <= n+m; i++)
		scanf("%d %lld", par+i, w+i);
	for (int i = n+m; i > n; i--) {
		pq[i].push(w[i]);
		pq[i].push(w[i]);
		a[i] = 1;
		b[i] = -w[i];
		merge(par[i], i);
	}
	for (int i = n; i > 1; i--) {
		while (a[i] > 1) {
			a[i]--;
			b[i] += pq[i].top();
			pq[i].pop();
		}
		ll x = pq[i].top(); pq[i].pop();
		ll y = pq[i].top(); pq[i].pop();
		x += w[i], y += w[i];
		pq[i].push(x); pq[i].push(y);
		b[i] -= w[i];
		merge(par[i], i);
	}
	while (a[1] > 0) {
		a[1]--;
		b[1] += pq[1].top();
		pq[1].pop();
	}
	prl(b[1]);
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

fireworks.cpp: In function 'int main()':
fireworks.cpp:51:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, m; scanf("%d %d", &n, &m);
            ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:53:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %lld", par+i, w+i);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...