Submission #259591

# Submission time Handle Problem Language Result Execution time Memory
259591 2020-08-08T03:30:41 Z DrumpfTheGodEmperor Fireworks (APIO16_fireworks) C++14
100 / 100
398 ms 82808 KB
#include <bits/stdc++.h>
#define int long long
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 3e5 + 5;
int n, m, c[N], ptr[N];
vector<int> g[N];
struct slope {
	int a, b;
	priority_queue<int> pq;
	void move(int c) {
		//After pq.top() is slope 1.
		int curTop = pq.top();
		pq.pop();
		int nxtTop = -1;
		if (pq.size()) {
			nxtTop = pq.top();
			pq.pop();
		}
		pq.push(curTop + c);
		if (nxtTop != -1) pq.push(nxtTop + c);
		
		b -= c;
	}
	int pqSize() {
		return pq.size();
	}
	int getTop() {
		if (pq.empty()) return -1;
		int ans = pq.top();
		pq.pop();
		return ans;
	}
	void pushIn(int val) {
		pq.push(val);
	}
} dp[N];
void print(priority_queue<int> pq) {
	while (pq.size()) {
		cout << pq.top() << " "; pq.pop();
	}
	cout << "\n";
}
void merge(slope &s1, slope &s2) {
	s1.a += s2.a;
	s1.b += s2.b;
	while (1) {
		int val = s2.getTop();
		if (val == -1) break;
		s1.pushIn(val);
	}
}
void dfs(int u) {
	if (g[u].size() == 0) {
		dp[u].pq.push(0);
		dp[u].pq.push(0);
		dp[u].a = 1, dp[u].b = 0;
		ptr[u] = u;
		return;
	}
	fa (v, g[u]) dfs(v);
//	cout << "U = " << u + 1 << "\n";
	fa (v, g[u]) dp[ptr[v]].move(c[v]);
//	cout << "all moves performed\n";
	int bigChild = -1;
	fa (v, g[u]) {
		if (bigChild == -1 || dp[ptr[v]].pqSize() > dp[ptr[bigChild]].pqSize()) bigChild = v;
	}
//	cout << "bigChild found = " << bigChild << "\n";
	ptr[u] = ptr[bigChild];
	fa (v, g[u]) if (v != bigChild) {
		merge(dp[ptr[u]], dp[ptr[v]]);
	}
	
	//Pop off the last point if the slope is still > 1
	slope &s = dp[ptr[u]];
	while (s.a > 1) {
		int x = s.pq.top();
		s.pq.pop();
		
		s.b += x;
		s.a--;
	}
}
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//	so uhh get dp(v) and move it to the right by C(v) units, merge it with |C(v) - X|
	cin >> n >> m;
	fw (i, 1, n + m) {
		int p;
		cin >> p >> c[i];
		p--;
		g[p].pb(i);
	}
//	a function actually only needs slope -1 -> 0 and 0 -> 1 moved when moving to the right
//	merging functions are just unioning their pqs and summing up a and b.
	dfs(0);
	slope s = dp[ptr[0]];
//	print(s.pq);
	int x = s.pq.top(), a = s.a, b = s.b;
//	cout << "a = " << a << " b = " << b << " x = " << x << "\n";
	cout << x * a + b;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 16 ms 21504 KB Output is correct
3 Correct 13 ms 21504 KB Output is correct
4 Correct 15 ms 21504 KB Output is correct
5 Correct 12 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 13 ms 21504 KB Output is correct
9 Correct 12 ms 21504 KB Output is correct
10 Correct 12 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21504 KB Output is correct
2 Correct 13 ms 21504 KB Output is correct
3 Correct 14 ms 21504 KB Output is correct
4 Correct 13 ms 21504 KB Output is correct
5 Correct 14 ms 21504 KB Output is correct
6 Correct 13 ms 21504 KB Output is correct
7 Correct 17 ms 21504 KB Output is correct
8 Correct 12 ms 21504 KB Output is correct
9 Correct 14 ms 21504 KB Output is correct
10 Correct 13 ms 21504 KB Output is correct
11 Correct 16 ms 21504 KB Output is correct
12 Correct 13 ms 21504 KB Output is correct
13 Correct 12 ms 21504 KB Output is correct
14 Correct 12 ms 21504 KB Output is correct
15 Correct 15 ms 21504 KB Output is correct
16 Correct 15 ms 21504 KB Output is correct
17 Correct 13 ms 21504 KB Output is correct
18 Correct 15 ms 21504 KB Output is correct
19 Correct 12 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 16 ms 21504 KB Output is correct
3 Correct 13 ms 21504 KB Output is correct
4 Correct 15 ms 21504 KB Output is correct
5 Correct 12 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 13 ms 21504 KB Output is correct
9 Correct 12 ms 21504 KB Output is correct
10 Correct 12 ms 21504 KB Output is correct
11 Correct 12 ms 21504 KB Output is correct
12 Correct 13 ms 21504 KB Output is correct
13 Correct 14 ms 21504 KB Output is correct
14 Correct 13 ms 21504 KB Output is correct
15 Correct 14 ms 21504 KB Output is correct
16 Correct 13 ms 21504 KB Output is correct
17 Correct 17 ms 21504 KB Output is correct
18 Correct 12 ms 21504 KB Output is correct
19 Correct 14 ms 21504 KB Output is correct
20 Correct 13 ms 21504 KB Output is correct
21 Correct 16 ms 21504 KB Output is correct
22 Correct 13 ms 21504 KB Output is correct
23 Correct 12 ms 21504 KB Output is correct
24 Correct 12 ms 21504 KB Output is correct
25 Correct 15 ms 21504 KB Output is correct
26 Correct 15 ms 21504 KB Output is correct
27 Correct 13 ms 21504 KB Output is correct
28 Correct 15 ms 21504 KB Output is correct
29 Correct 12 ms 21504 KB Output is correct
30 Correct 13 ms 21504 KB Output is correct
31 Correct 15 ms 21632 KB Output is correct
32 Correct 14 ms 21632 KB Output is correct
33 Correct 14 ms 21632 KB Output is correct
34 Correct 17 ms 21756 KB Output is correct
35 Correct 15 ms 21760 KB Output is correct
36 Correct 15 ms 21760 KB Output is correct
37 Correct 19 ms 21888 KB Output is correct
38 Correct 15 ms 21888 KB Output is correct
39 Correct 17 ms 21888 KB Output is correct
40 Correct 15 ms 22528 KB Output is correct
41 Correct 17 ms 22520 KB Output is correct
42 Correct 17 ms 21828 KB Output is correct
43 Correct 17 ms 22144 KB Output is correct
44 Correct 16 ms 22144 KB Output is correct
45 Correct 17 ms 22144 KB Output is correct
46 Correct 21 ms 22016 KB Output is correct
47 Correct 17 ms 22016 KB Output is correct
48 Correct 15 ms 22016 KB Output is correct
49 Correct 16 ms 22016 KB Output is correct
50 Correct 16 ms 21888 KB Output is correct
51 Correct 15 ms 21888 KB Output is correct
52 Correct 15 ms 22016 KB Output is correct
53 Correct 16 ms 21948 KB Output is correct
54 Correct 16 ms 22016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 16 ms 21504 KB Output is correct
3 Correct 13 ms 21504 KB Output is correct
4 Correct 15 ms 21504 KB Output is correct
5 Correct 12 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 13 ms 21504 KB Output is correct
9 Correct 12 ms 21504 KB Output is correct
10 Correct 12 ms 21504 KB Output is correct
11 Correct 12 ms 21504 KB Output is correct
12 Correct 13 ms 21504 KB Output is correct
13 Correct 14 ms 21504 KB Output is correct
14 Correct 13 ms 21504 KB Output is correct
15 Correct 14 ms 21504 KB Output is correct
16 Correct 13 ms 21504 KB Output is correct
17 Correct 17 ms 21504 KB Output is correct
18 Correct 12 ms 21504 KB Output is correct
19 Correct 14 ms 21504 KB Output is correct
20 Correct 13 ms 21504 KB Output is correct
21 Correct 16 ms 21504 KB Output is correct
22 Correct 13 ms 21504 KB Output is correct
23 Correct 12 ms 21504 KB Output is correct
24 Correct 12 ms 21504 KB Output is correct
25 Correct 15 ms 21504 KB Output is correct
26 Correct 15 ms 21504 KB Output is correct
27 Correct 13 ms 21504 KB Output is correct
28 Correct 15 ms 21504 KB Output is correct
29 Correct 12 ms 21504 KB Output is correct
30 Correct 13 ms 21504 KB Output is correct
31 Correct 15 ms 21632 KB Output is correct
32 Correct 14 ms 21632 KB Output is correct
33 Correct 14 ms 21632 KB Output is correct
34 Correct 17 ms 21756 KB Output is correct
35 Correct 15 ms 21760 KB Output is correct
36 Correct 15 ms 21760 KB Output is correct
37 Correct 19 ms 21888 KB Output is correct
38 Correct 15 ms 21888 KB Output is correct
39 Correct 17 ms 21888 KB Output is correct
40 Correct 15 ms 22528 KB Output is correct
41 Correct 17 ms 22520 KB Output is correct
42 Correct 17 ms 21828 KB Output is correct
43 Correct 17 ms 22144 KB Output is correct
44 Correct 16 ms 22144 KB Output is correct
45 Correct 17 ms 22144 KB Output is correct
46 Correct 21 ms 22016 KB Output is correct
47 Correct 17 ms 22016 KB Output is correct
48 Correct 15 ms 22016 KB Output is correct
49 Correct 16 ms 22016 KB Output is correct
50 Correct 16 ms 21888 KB Output is correct
51 Correct 15 ms 21888 KB Output is correct
52 Correct 15 ms 22016 KB Output is correct
53 Correct 16 ms 21948 KB Output is correct
54 Correct 16 ms 22016 KB Output is correct
55 Correct 21 ms 22528 KB Output is correct
56 Correct 49 ms 25636 KB Output is correct
57 Correct 104 ms 28408 KB Output is correct
58 Correct 115 ms 30320 KB Output is correct
59 Correct 169 ms 33240 KB Output is correct
60 Correct 205 ms 36208 KB Output is correct
61 Correct 285 ms 38252 KB Output is correct
62 Correct 247 ms 40152 KB Output is correct
63 Correct 291 ms 43856 KB Output is correct
64 Correct 398 ms 44844 KB Output is correct
65 Correct 162 ms 82680 KB Output is correct
66 Correct 167 ms 82808 KB Output is correct
67 Correct 159 ms 40568 KB Output is correct
68 Correct 209 ms 63852 KB Output is correct
69 Correct 269 ms 60668 KB Output is correct
70 Correct 256 ms 60688 KB Output is correct
71 Correct 294 ms 56904 KB Output is correct
72 Correct 241 ms 56804 KB Output is correct
73 Correct 216 ms 53600 KB Output is correct
74 Correct 210 ms 53472 KB Output is correct
75 Correct 211 ms 52068 KB Output is correct
76 Correct 214 ms 52064 KB Output is correct
77 Correct 222 ms 50904 KB Output is correct
78 Correct 228 ms 50144 KB Output is correct
79 Correct 211 ms 49756 KB Output is correct