답안 #563132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563132 2022-05-16T11:00:00 Z hollwo_pelw Fireworks (APIO16_fireworks) C++17
100 / 100
186 ms 46156 KB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;

void Hollwo_Pelw();

signed main(){
#ifndef hollwo_pelw_local
	if (fopen(".inp", "r"))
		assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
	using namespace chrono;
	auto start = steady_clock::now();
#endif
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	cout << "\nExecution time : " << duration_cast<milliseconds> (steady_clock::now() - start).count() << "[ms]" << endl;
#endif
}

#define int long long

const int N = 3e5 + 5;
// wtf slope trick

int n, m, p[N], c[N];

struct slope_t {
	priority_queue<int> pq;
	int slp = 0, res = 0;

	inline void swap(slope_t &oth) {
		pq.swap(oth.pq);
		std::swap(slp, oth.slp);
		std::swap(res, oth.res);
	}

	inline void merge(slope_t &oth) {
		if (pq.size() < oth.pq.size()) swap(oth);

		if (!oth.pq.empty()) {
			if (pq.top() > oth.pq.top())
				res += oth.slp * (pq.top() - oth.pq.top());
			if (pq.top() < oth.pq.top())
				res +=     slp * (oth.pq.top() - pq.top());
		}

		res += oth.res;
		slp += oth.slp;

		for (; !oth.pq.empty(); oth.pq.pop())
			pq.push(oth.pq.top());

	}

	inline void __init__(int v) {
		slp = 1, res = 0;
		pq.push(v), pq.push(v);
	}

	inline void __norm__(int v) {
		for (; slp > 1; slp --) {
			int last = pq.top(); pq.pop();
			res -= (last - pq.top()) * (slp - 1);
		}
		int slp1 = pq.top(); pq.pop();
		int slp0 = pq.top(); pq.pop();
		pq.push(slp0 + v);
		pq.push(slp1 + v);
	}

	inline void print() {
		priority_queue<int> f = pq;
		cout << "pq = { ";
		while (!f.empty())
			cout << f.top() << ' ', f.pop();
		cout << "}\n";
		cout << "slp = " << slp << '\n';
		cout << "ans = " << res << '\n';
	}

} slope_trick[N];

void Hollwo_Pelw(){
	cin >> n >> m;
	for (int i = 2; i <= n + m; i++)
		cin >> p[i] >> c[i];
	for (int i = n + m; i > 1; i--) {
		// cout << "\nsolve " << i << "\n";
		// slope_trick[i].print();
		if (i > n) {
			slope_trick[i].__init__(c[i]);
		} else {
			slope_trick[i].__norm__(c[i]);
		}
		// slope_trick[i].print();
		slope_trick[p[i]].merge(slope_trick[i]);
	}
	slope_trick[1].__norm__(0);
	cout << slope_trick[1].res << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14428 KB Output is correct
4 Correct 7 ms 14364 KB Output is correct
5 Correct 7 ms 14428 KB Output is correct
6 Correct 8 ms 14424 KB Output is correct
7 Correct 7 ms 14424 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14428 KB Output is correct
5 Correct 9 ms 14340 KB Output is correct
6 Correct 9 ms 14440 KB Output is correct
7 Correct 8 ms 14328 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14424 KB Output is correct
11 Correct 8 ms 14400 KB Output is correct
12 Correct 10 ms 14424 KB Output is correct
13 Correct 7 ms 14364 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 8 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
19 Correct 8 ms 14416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14428 KB Output is correct
4 Correct 7 ms 14364 KB Output is correct
5 Correct 7 ms 14428 KB Output is correct
6 Correct 8 ms 14424 KB Output is correct
7 Correct 7 ms 14424 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14428 KB Output is correct
15 Correct 9 ms 14340 KB Output is correct
16 Correct 9 ms 14440 KB Output is correct
17 Correct 8 ms 14328 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14424 KB Output is correct
21 Correct 8 ms 14400 KB Output is correct
22 Correct 10 ms 14424 KB Output is correct
23 Correct 7 ms 14364 KB Output is correct
24 Correct 7 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14420 KB Output is correct
27 Correct 7 ms 14420 KB Output is correct
28 Correct 7 ms 14420 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 9 ms 14420 KB Output is correct
31 Correct 8 ms 14420 KB Output is correct
32 Correct 8 ms 14524 KB Output is correct
33 Correct 9 ms 14532 KB Output is correct
34 Correct 9 ms 14548 KB Output is correct
35 Correct 9 ms 14616 KB Output is correct
36 Correct 10 ms 14696 KB Output is correct
37 Correct 13 ms 14676 KB Output is correct
38 Correct 10 ms 14640 KB Output is correct
39 Correct 9 ms 14676 KB Output is correct
40 Correct 9 ms 14548 KB Output is correct
41 Correct 10 ms 14568 KB Output is correct
42 Correct 8 ms 14496 KB Output is correct
43 Correct 9 ms 14644 KB Output is correct
44 Correct 9 ms 14708 KB Output is correct
45 Correct 10 ms 14700 KB Output is correct
46 Correct 10 ms 14892 KB Output is correct
47 Correct 9 ms 14888 KB Output is correct
48 Correct 9 ms 14764 KB Output is correct
49 Correct 10 ms 14888 KB Output is correct
50 Correct 10 ms 14768 KB Output is correct
51 Correct 12 ms 14724 KB Output is correct
52 Correct 13 ms 14828 KB Output is correct
53 Correct 11 ms 14824 KB Output is correct
54 Correct 9 ms 14804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14428 KB Output is correct
4 Correct 7 ms 14364 KB Output is correct
5 Correct 7 ms 14428 KB Output is correct
6 Correct 8 ms 14424 KB Output is correct
7 Correct 7 ms 14424 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14428 KB Output is correct
15 Correct 9 ms 14340 KB Output is correct
16 Correct 9 ms 14440 KB Output is correct
17 Correct 8 ms 14328 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14424 KB Output is correct
21 Correct 8 ms 14400 KB Output is correct
22 Correct 10 ms 14424 KB Output is correct
23 Correct 7 ms 14364 KB Output is correct
24 Correct 7 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14420 KB Output is correct
27 Correct 7 ms 14420 KB Output is correct
28 Correct 7 ms 14420 KB Output is correct
29 Correct 8 ms 14416 KB Output is correct
30 Correct 9 ms 14420 KB Output is correct
31 Correct 8 ms 14420 KB Output is correct
32 Correct 8 ms 14524 KB Output is correct
33 Correct 9 ms 14532 KB Output is correct
34 Correct 9 ms 14548 KB Output is correct
35 Correct 9 ms 14616 KB Output is correct
36 Correct 10 ms 14696 KB Output is correct
37 Correct 13 ms 14676 KB Output is correct
38 Correct 10 ms 14640 KB Output is correct
39 Correct 9 ms 14676 KB Output is correct
40 Correct 9 ms 14548 KB Output is correct
41 Correct 10 ms 14568 KB Output is correct
42 Correct 8 ms 14496 KB Output is correct
43 Correct 9 ms 14644 KB Output is correct
44 Correct 9 ms 14708 KB Output is correct
45 Correct 10 ms 14700 KB Output is correct
46 Correct 10 ms 14892 KB Output is correct
47 Correct 9 ms 14888 KB Output is correct
48 Correct 9 ms 14764 KB Output is correct
49 Correct 10 ms 14888 KB Output is correct
50 Correct 10 ms 14768 KB Output is correct
51 Correct 12 ms 14724 KB Output is correct
52 Correct 13 ms 14828 KB Output is correct
53 Correct 11 ms 14824 KB Output is correct
54 Correct 9 ms 14804 KB Output is correct
55 Correct 14 ms 15188 KB Output is correct
56 Correct 27 ms 17776 KB Output is correct
57 Correct 40 ms 20044 KB Output is correct
58 Correct 57 ms 21828 KB Output is correct
59 Correct 91 ms 24012 KB Output is correct
60 Correct 96 ms 26320 KB Output is correct
61 Correct 97 ms 28660 KB Output is correct
62 Correct 110 ms 29744 KB Output is correct
63 Correct 129 ms 32808 KB Output is correct
64 Correct 132 ms 33460 KB Output is correct
65 Correct 81 ms 23976 KB Output is correct
66 Correct 80 ms 24092 KB Output is correct
67 Correct 69 ms 23992 KB Output is correct
68 Correct 121 ms 30656 KB Output is correct
69 Correct 132 ms 31356 KB Output is correct
70 Correct 151 ms 31236 KB Output is correct
71 Correct 170 ms 46036 KB Output is correct
72 Correct 171 ms 46156 KB Output is correct
73 Correct 153 ms 42704 KB Output is correct
74 Correct 155 ms 43176 KB Output is correct
75 Correct 186 ms 41696 KB Output is correct
76 Correct 186 ms 41680 KB Output is correct
77 Correct 165 ms 41020 KB Output is correct
78 Correct 165 ms 40760 KB Output is correct
79 Correct 155 ms 38968 KB Output is correct