Submission #874818

# Submission time Handle Problem Language Result Execution time Memory
874818 2023-11-17T21:05:16 Z rainboy Job Scheduling (IOI19_job) C++17
100 / 100
303 ms 48168 KB
#include "job.h"
#include <cstdlib>
#include <vector>

using namespace std;

typedef vector<int> vi;

const int N = 200000;

unsigned int Z = 12345;

int rand_() {
	return (Z *= 3) >> 1;
}

int n;
int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;
	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

long long cross(int x1, int y1, int x2, int y2) {
	return (long long) x1 * y2 - (long long) x2 * y1;
}

int xx[N], yy[N];

int compare(int i, int j) {
	long long c = cross(xx[i], yy[i], xx[j], yy[j]);
	return c != 0 ? (c < 0 ? -1 : 1) : i - j;
}

int zz[N + 1], ll[N + 1], rr[N + 1], ii[N + 1], u_, l_, r_;

int node(int i) {
	static int _ = 1;
	zz[_] = rand_();
	ll[_] = rr[_] = 0;
	ii[_] = i;
	return _++;
}

void split(int u, int i) {
	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	int c = compare(ii[u], i);
	if (c < 0) {
		split(rr[u], i);
		rr[u] = l_, l_ = u;
	} else if (c > 0) {
		split(ll[u], i);
		ll[u] = r_, r_ = u;
	} else {
		u_ = u, l_ = ll[u], r_ = rr[u];
		ll[u] = rr[u] = 0;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

int first(int u) {
	return ll[u] == 0 ? u : first(ll[u]);
}

int remove_first(int u) {
	if (ll[u] == 0)
		return rr[u];
	ll[u] = remove_first(ll[u]);
	return u;
}

int merge_(int u, int v) {
	while (v) {
		int w = first(v); v = remove_first(v); ll[w] = rr[w] = 0;
		split(u, ii[w]);
		u = merge(merge(l_, w), r_);
	}
	return u;
}

int sz[N], uu[N]; long long ans;

void dfs(int i) {
	sz[i] = 1, uu[i] = 0;
	for (int o = eo[i]; o--; ) {
		int j = ej[i][o];
		dfs(j);
		uu[i] = sz[i] > sz[j] ? merge_(uu[i], uu[j]) : merge_(uu[j], uu[i]);
		sz[i] += sz[j];
	}
	while (uu[i]) {
		int w = first(uu[i]);
		if (compare(i, ii[w]) < 0)
			break;
		uu[i] = remove_first(uu[i]);
		ans += (long long) xx[i] * yy[ii[w]];
		xx[i] += xx[ii[w]], yy[i] += yy[ii[w]];
	}
	uu[i] = merge(node(i), uu[i]);
}

long long scheduling_cost(vi pp, vi yy_, vi xx_) {
	n = pp.size();
	for (int i = 0; i < n; i++)
		xx[i] = xx_[i], yy[i] = yy_[i];
	for (int i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
	for (int i = 1; i < n; i++)
		append(pp[i], i);
	ans = 0;
	for (int i = 0; i < n; i++)
		ans += (long long) xx[i] * yy[i];
	dfs(0);
	int x = 0;
	while (uu[0]) {
		int w = first(uu[0]); uu[0] = remove_first(uu[0]);
		ans += (long long) x * yy[ii[w]], x += xx[ii[w]];
	}
	for (int i = 0; i < n; i++)
		free(ej[i]);
	return ans;
}

Compilation message

job.cpp: In function 'void append(int, int)':
job.cpp:22:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   22 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 20 ms 16988 KB Output is correct
6 Correct 54 ms 27228 KB Output is correct
7 Correct 76 ms 37716 KB Output is correct
8 Correct 74 ms 47956 KB Output is correct
9 Correct 79 ms 48168 KB Output is correct
10 Correct 84 ms 47952 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 83 ms 48068 KB Output is correct
13 Correct 82 ms 47956 KB Output is correct
14 Correct 89 ms 47956 KB Output is correct
15 Correct 69 ms 48092 KB Output is correct
16 Correct 90 ms 48140 KB Output is correct
17 Correct 74 ms 48068 KB Output is correct
18 Correct 76 ms 48104 KB Output is correct
19 Correct 74 ms 48068 KB Output is correct
20 Correct 98 ms 48084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 189 ms 22548 KB Output is correct
5 Correct 178 ms 22488 KB Output is correct
6 Correct 180 ms 22608 KB Output is correct
7 Correct 189 ms 22480 KB Output is correct
8 Correct 190 ms 22604 KB Output is correct
9 Correct 179 ms 22456 KB Output is correct
10 Correct 175 ms 22616 KB Output is correct
11 Correct 175 ms 22612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 9 ms 7260 KB Output is correct
6 Correct 182 ms 22992 KB Output is correct
7 Correct 180 ms 23440 KB Output is correct
8 Correct 177 ms 23120 KB Output is correct
9 Correct 177 ms 23084 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 2 ms 6492 KB Output is correct
12 Correct 6 ms 7028 KB Output is correct
13 Correct 6 ms 7256 KB Output is correct
14 Correct 187 ms 23040 KB Output is correct
15 Correct 177 ms 23120 KB Output is correct
16 Correct 175 ms 23120 KB Output is correct
17 Correct 179 ms 23004 KB Output is correct
18 Correct 186 ms 23016 KB Output is correct
19 Correct 175 ms 23120 KB Output is correct
20 Correct 175 ms 23124 KB Output is correct
21 Correct 186 ms 23120 KB Output is correct
22 Correct 177 ms 23104 KB Output is correct
23 Correct 175 ms 22992 KB Output is correct
24 Correct 178 ms 23128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 70 ms 34016 KB Output is correct
3 Correct 71 ms 37196 KB Output is correct
4 Correct 68 ms 37076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6576 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6576 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6488 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 20 ms 16988 KB Output is correct
6 Correct 54 ms 27228 KB Output is correct
7 Correct 76 ms 37716 KB Output is correct
8 Correct 74 ms 47956 KB Output is correct
9 Correct 79 ms 48168 KB Output is correct
10 Correct 84 ms 47952 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 83 ms 48068 KB Output is correct
13 Correct 82 ms 47956 KB Output is correct
14 Correct 89 ms 47956 KB Output is correct
15 Correct 69 ms 48092 KB Output is correct
16 Correct 90 ms 48140 KB Output is correct
17 Correct 74 ms 48068 KB Output is correct
18 Correct 76 ms 48104 KB Output is correct
19 Correct 74 ms 48068 KB Output is correct
20 Correct 98 ms 48084 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 189 ms 22548 KB Output is correct
25 Correct 178 ms 22488 KB Output is correct
26 Correct 180 ms 22608 KB Output is correct
27 Correct 189 ms 22480 KB Output is correct
28 Correct 190 ms 22604 KB Output is correct
29 Correct 179 ms 22456 KB Output is correct
30 Correct 175 ms 22616 KB Output is correct
31 Correct 175 ms 22612 KB Output is correct
32 Correct 1 ms 6488 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 2 ms 6492 KB Output is correct
36 Correct 9 ms 7260 KB Output is correct
37 Correct 182 ms 22992 KB Output is correct
38 Correct 180 ms 23440 KB Output is correct
39 Correct 177 ms 23120 KB Output is correct
40 Correct 177 ms 23084 KB Output is correct
41 Correct 1 ms 6488 KB Output is correct
42 Correct 2 ms 6492 KB Output is correct
43 Correct 6 ms 7028 KB Output is correct
44 Correct 6 ms 7256 KB Output is correct
45 Correct 187 ms 23040 KB Output is correct
46 Correct 177 ms 23120 KB Output is correct
47 Correct 175 ms 23120 KB Output is correct
48 Correct 179 ms 23004 KB Output is correct
49 Correct 186 ms 23016 KB Output is correct
50 Correct 175 ms 23120 KB Output is correct
51 Correct 175 ms 23124 KB Output is correct
52 Correct 186 ms 23120 KB Output is correct
53 Correct 177 ms 23104 KB Output is correct
54 Correct 175 ms 22992 KB Output is correct
55 Correct 178 ms 23128 KB Output is correct
56 Correct 1 ms 6488 KB Output is correct
57 Correct 70 ms 34016 KB Output is correct
58 Correct 71 ms 37196 KB Output is correct
59 Correct 68 ms 37076 KB Output is correct
60 Correct 1 ms 6492 KB Output is correct
61 Correct 1 ms 6488 KB Output is correct
62 Correct 1 ms 6576 KB Output is correct
63 Correct 1 ms 6492 KB Output is correct
64 Correct 1 ms 6492 KB Output is correct
65 Correct 1 ms 6492 KB Output is correct
66 Correct 1 ms 6492 KB Output is correct
67 Correct 1 ms 6492 KB Output is correct
68 Correct 2 ms 6492 KB Output is correct
69 Correct 1 ms 6492 KB Output is correct
70 Correct 1 ms 6492 KB Output is correct
71 Correct 1 ms 6492 KB Output is correct
72 Correct 1 ms 6576 KB Output is correct
73 Correct 1 ms 6492 KB Output is correct
74 Correct 1 ms 6492 KB Output is correct
75 Correct 1 ms 6488 KB Output is correct
76 Correct 1 ms 6492 KB Output is correct
77 Correct 1 ms 6492 KB Output is correct
78 Correct 1 ms 6492 KB Output is correct
79 Correct 1 ms 6492 KB Output is correct
80 Correct 1 ms 6492 KB Output is correct
81 Correct 1 ms 6492 KB Output is correct
82 Correct 1 ms 6492 KB Output is correct
83 Correct 1 ms 6492 KB Output is correct
84 Correct 1 ms 6492 KB Output is correct
85 Correct 1 ms 6492 KB Output is correct
86 Correct 1 ms 6492 KB Output is correct
87 Correct 1 ms 6492 KB Output is correct
88 Correct 4 ms 6748 KB Output is correct
89 Correct 6 ms 7004 KB Output is correct
90 Correct 8 ms 7768 KB Output is correct
91 Correct 41 ms 10668 KB Output is correct
92 Correct 86 ms 14940 KB Output is correct
93 Correct 202 ms 23124 KB Output is correct
94 Correct 206 ms 23332 KB Output is correct
95 Correct 200 ms 23124 KB Output is correct
96 Correct 213 ms 23120 KB Output is correct
97 Correct 225 ms 23364 KB Output is correct
98 Correct 189 ms 23108 KB Output is correct
99 Correct 245 ms 23636 KB Output is correct
100 Correct 187 ms 23328 KB Output is correct
101 Correct 137 ms 23120 KB Output is correct
102 Correct 303 ms 23120 KB Output is correct
103 Correct 137 ms 23372 KB Output is correct
104 Correct 105 ms 22988 KB Output is correct
105 Correct 83 ms 23872 KB Output is correct
106 Correct 208 ms 23632 KB Output is correct