#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);
split(u, ii[w]);
u = merge(merge(l_, w), r_);
v = remove_first(v);
}
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;
ans += (long long) xx[i] * yy[ii[w]];
xx[i] += xx[ii[w]], yy[i] += yy[ii[w]];
uu[i] = remove_first(uu[i]);
}
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]);
ans += (long long) x * yy[ii[w]], x += xx[ii[w]];
uu[0] = remove_first(uu[0]);
}
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 |
2 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 |
1 ms |
6760 KB |
Output is correct |
5 |
Correct |
20 ms |
16108 KB |
Output is correct |
6 |
Correct |
37 ms |
25780 KB |
Output is correct |
7 |
Correct |
57 ms |
35412 KB |
Output is correct |
8 |
Correct |
74 ms |
44884 KB |
Output is correct |
9 |
Correct |
76 ms |
44888 KB |
Output is correct |
10 |
Correct |
72 ms |
45088 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
75 ms |
45008 KB |
Output is correct |
13 |
Correct |
69 ms |
44880 KB |
Output is correct |
14 |
Correct |
84 ms |
45004 KB |
Output is correct |
15 |
Correct |
66 ms |
44884 KB |
Output is correct |
16 |
Correct |
87 ms |
44876 KB |
Output is correct |
17 |
Correct |
75 ms |
44880 KB |
Output is correct |
18 |
Correct |
74 ms |
44872 KB |
Output is correct |
19 |
Correct |
71 ms |
44864 KB |
Output is correct |
20 |
Correct |
92 ms |
44880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
70 ms |
34124 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Execution timed out |
3082 ms |
6492 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
1 ms |
6760 KB |
Output is correct |
5 |
Correct |
20 ms |
16108 KB |
Output is correct |
6 |
Correct |
37 ms |
25780 KB |
Output is correct |
7 |
Correct |
57 ms |
35412 KB |
Output is correct |
8 |
Correct |
74 ms |
44884 KB |
Output is correct |
9 |
Correct |
76 ms |
44888 KB |
Output is correct |
10 |
Correct |
72 ms |
45088 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
75 ms |
45008 KB |
Output is correct |
13 |
Correct |
69 ms |
44880 KB |
Output is correct |
14 |
Correct |
84 ms |
45004 KB |
Output is correct |
15 |
Correct |
66 ms |
44884 KB |
Output is correct |
16 |
Correct |
87 ms |
44876 KB |
Output is correct |
17 |
Correct |
75 ms |
44880 KB |
Output is correct |
18 |
Correct |
74 ms |
44872 KB |
Output is correct |
19 |
Correct |
71 ms |
44864 KB |
Output is correct |
20 |
Correct |
92 ms |
44880 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |