답안 #81949

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
81949 2018-10-28T08:49:57 Z memikakizaki Fireworks (APIO16_fireworks) C++14
26 / 100
3 ms 776 KB
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author memikakizaki
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;
#define long long long
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};


class Fireworks {
	int n, m;
	vector<vector<pair<int,int> > > g;
	vector<priority_queue<int> > pq;
	vector<long> val;

	void print_pq_increasing(priority_queue<int>& pq) {
		vector<int> res;
		while(!pq.empty()) {
			res.push_back(pq.top());
			pq.pop();
		}
		reverse(res.begin(), res.end());
		for(int x: res) pq.push(x), cout << x << ' ';
		cout << endl;
	}

	void dfs(int u, int wpar) {
		int cnt = 0;
		for(auto pr: g[u]) {
			int v, w;
			tie(v, w) = pr;
			dfs(v, w);
			if(pq[u].empty()) {
				val[u] += val[v];
			} else if(pq[v].top() > pq[u].top()) {
				val[u] += val[v] + 1ll * cnt * abs(pq[v].top() - pq[u].top());
			} else {
				val[u] += val[v] + abs(pq[u].top() - pq[v].top());
			}
			if(pq[v].size() > pq[u].size()) {
				swap(pq[u], pq[v]);
			}
			while(!pq[v].empty()) pq[u].push(pq[v].top()), pq[v].pop();
			++cnt;
		}
		int r;
		while(cnt--) {
			r = pq[u].top();
			pq[u].pop();
			val[u] -= 1ll * cnt * (r - pq[u].top()); // already --
		}
		if(!g[u].empty()) {
			int l = pq[u].top();
			pq[u].pop();
			pq[u].push(l + wpar);
			pq[u].push(r + wpar);
//			cout << "l " << l << ' ' << "r " << r << endl;
		} else {
			pq[u].push(wpar);
			pq[u].push(wpar);
		}
//		cout << "node " << u << endl;
//		cout << "val " << val[u] << endl;
//		for(auto pr:g[u]) {
//			int v; tie(v, ignore) = pr;
////			cout << v << ' ' << val[v] << endl;
//		}
//		print_pq_increasing(pq[u]);
//		cout << endl;
	}

public:
	void solve(istream& cin, ostream& cout) {
		cin >> n >> m;
		n += m;
		g = vector<vector<pair<int,int> > >(n);
		pq = vector<priority_queue<int> >(n);
		val = vector<long>(n);
		for(int i = 1, p, c; i < n; i++) {
			cin >> p >> c;
			g[p-1].emplace_back(i, c);
		}
		dfs(0, 0);
//		for(int i = 0; i < n; i++) cout << i << ' ' << val[i] << endl;
		cout << val[0] << endl;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Fireworks solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 456 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 2 ms 560 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 2 ms 672 KB Output is correct
10 Correct 3 ms 672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 672 KB Output is correct
2 Correct 2 ms 776 KB Output is correct
3 Correct 2 ms 776 KB Output is correct
4 Correct 2 ms 776 KB Output is correct
5 Correct 3 ms 776 KB Output is correct
6 Correct 2 ms 776 KB Output is correct
7 Correct 2 ms 776 KB Output is correct
8 Correct 2 ms 776 KB Output is correct
9 Correct 2 ms 776 KB Output is correct
10 Correct 2 ms 776 KB Output is correct
11 Correct 2 ms 776 KB Output is correct
12 Correct 2 ms 776 KB Output is correct
13 Correct 3 ms 776 KB Output is correct
14 Correct 2 ms 776 KB Output is correct
15 Correct 2 ms 776 KB Output is correct
16 Correct 3 ms 776 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 776 KB Output is correct
19 Correct 2 ms 776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 456 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 2 ms 560 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 2 ms 672 KB Output is correct
10 Correct 3 ms 672 KB Output is correct
11 Correct 2 ms 672 KB Output is correct
12 Correct 2 ms 776 KB Output is correct
13 Correct 2 ms 776 KB Output is correct
14 Correct 2 ms 776 KB Output is correct
15 Correct 3 ms 776 KB Output is correct
16 Correct 2 ms 776 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 776 KB Output is correct
19 Correct 2 ms 776 KB Output is correct
20 Correct 2 ms 776 KB Output is correct
21 Correct 2 ms 776 KB Output is correct
22 Correct 2 ms 776 KB Output is correct
23 Correct 3 ms 776 KB Output is correct
24 Correct 2 ms 776 KB Output is correct
25 Correct 2 ms 776 KB Output is correct
26 Correct 3 ms 776 KB Output is correct
27 Correct 2 ms 776 KB Output is correct
28 Correct 2 ms 776 KB Output is correct
29 Correct 2 ms 776 KB Output is correct
30 Incorrect 2 ms 776 KB Output isn't correct
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 456 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 540 KB Output is correct
6 Correct 2 ms 560 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 2 ms 672 KB Output is correct
10 Correct 3 ms 672 KB Output is correct
11 Correct 2 ms 672 KB Output is correct
12 Correct 2 ms 776 KB Output is correct
13 Correct 2 ms 776 KB Output is correct
14 Correct 2 ms 776 KB Output is correct
15 Correct 3 ms 776 KB Output is correct
16 Correct 2 ms 776 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 776 KB Output is correct
19 Correct 2 ms 776 KB Output is correct
20 Correct 2 ms 776 KB Output is correct
21 Correct 2 ms 776 KB Output is correct
22 Correct 2 ms 776 KB Output is correct
23 Correct 3 ms 776 KB Output is correct
24 Correct 2 ms 776 KB Output is correct
25 Correct 2 ms 776 KB Output is correct
26 Correct 3 ms 776 KB Output is correct
27 Correct 2 ms 776 KB Output is correct
28 Correct 2 ms 776 KB Output is correct
29 Correct 2 ms 776 KB Output is correct
30 Incorrect 2 ms 776 KB Output isn't correct
31 Halted 0 ms 0 KB -