답안 #251710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
251710 2020-07-22T08:36:08 Z atoiz Fireworks (APIO16_fireworks) C++14
0 / 100
0 ms 384 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

struct Curve : priority_queue<int>
{
	int end_slope;
	long long offset;

	void swap(Curve &rhs)
	{
		priority_queue::swap(rhs);
		std::swap(end_slope, rhs.end_slope);
		std::swap(offset, rhs.offset);
	}

	void init(int x)
	{
		while (!empty()) pop();
		push(x), push(x);
		end_slope = 1;
		offset = 0;
	}

	void merge(Curve &rhs)
	{
		if (size() < rhs.size()) swap(rhs);
		if (rhs.empty()) return;
		end_slope += rhs.end_slope;
		if (top() < rhs.top()) offset = rhs.offset + end_slope * (rhs.top() - top());
		else offset = offset + rhs.end_slope * (top() - rhs.top());
		for (; !rhs.empty(); rhs.pop()) push(rhs.top());
	}

	void normalize(int x)
	{
		for (; end_slope > 1; --end_slope) {
			int t = top();
			pop();
			offset -= (long long) (t - top()) * (end_slope - 1);
		}

		int first = top();
		pop();
		int second = top();
		pop();
		push(second + x);
		push(first + x);
	}
};

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int N, M;
	cin >> N >> M;
	vector<int> P(N + M + 1), C(N + M + 1);
	for (int i = 2; i <= N + M; ++i) cin >> P[i] >> C[i];
	vector<Curve> curves(N + M + 1);
	for (int i = N + M; i > N; --i) {
		curves[i].init(C[i]);
		curves[P[i]].merge(curves[i]);
	}
	for (int i = N; i > 1; --i) {
		curves[i].normalize(C[i]);
		curves[P[i]].merge(curves[i]);
	}
	curves[1].normalize(0);
	cout << curves[1].offset << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -