#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
struct plf { //piecewise linear function
ll m; //slope at 0
ll b; //value at 0
ll sz; //size
multiset<ll> flip; //add a ReLU, vertext at that poitn
void fuse(plf x0) {
sz += x0.sz;
m += x0.m;
b += x0.b;
for (ll x: x0.flip) {
flip.insert(x);
}
}
void backerase(ll n) {
for (ll i=0;i<n;i++) {
flip.erase(--flip.end());
}
}
void wipe() {
flip.clear();
}
ll gmin() {
ll mv = m; ll bv = b;
ll ans = b;
for (ll x: flip) {
mv += 1; bv -= x;
ans = min(ans,mv*x+bv);
}
return ans;
}
void ext(ll L) { //add segment of length L up front
sz++; b += L;
auto A = --flip.end(); auto B = A; B--;
ll x,y; x = *A; y = *B;
flip.erase(A); flip.erase(B);
flip.insert(x+L); flip.insert(y+L);
}
};
plf junct(ll L) {
plf x0;
x0.m=-1;
x0.b=L;
x0.sz=1;
x0.flip.insert(L); x0.flip.insert(L);
return x0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
ll N,M; cin >> N >> M;
ll radj[N+M]; ll c[N+M];
vector<ll> fadj[N];
plf x0[M]; //element i corresponds to vertex N+i
radj[0]=-1; c[0]=0;
ll madd[M+N];
for (ll i=1;i<(N+M);i++) {
cin >> radj[i]; radj[i]--;
fadj[radj[i]].push_back(i);
cin >> c[i];
}
for (ll i=0;i<M;i++) {
x0[i]=junct(c[i+N]);
madd[N+i]=i;
}
for (ll i=(N-1);i>=0;i--) {
ll maxv = -1; ll xc = -1;
for (ll x: fadj[i]) {
if (x0[madd[x]].sz>maxv) {
maxv = x0[madd[x]].sz;
xc = x;
}
}
madd[i]=madd[xc];
for (ll x: fadj[i]) {
if (x != xc) {
x0[madd[i]].fuse(x0[madd[x]]);
x0[madd[x]].wipe();
}
}
x0[madd[i]].backerase(fadj[i].size()-1);
if (i != 0) {
x0[madd[i]].ext(c[i]);
} else {
cout << x0[madd[i]].gmin();
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
424 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
424 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
620 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
4 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
1060 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
1116 KB |
Output is correct |
44 |
Correct |
2 ms |
1116 KB |
Output is correct |
45 |
Correct |
2 ms |
1116 KB |
Output is correct |
46 |
Correct |
3 ms |
1372 KB |
Output is correct |
47 |
Correct |
3 ms |
1444 KB |
Output is correct |
48 |
Correct |
3 ms |
1112 KB |
Output is correct |
49 |
Correct |
3 ms |
1368 KB |
Output is correct |
50 |
Correct |
2 ms |
1116 KB |
Output is correct |
51 |
Correct |
2 ms |
1116 KB |
Output is correct |
52 |
Correct |
2 ms |
1320 KB |
Output is correct |
53 |
Correct |
3 ms |
1116 KB |
Output is correct |
54 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
424 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
620 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
4 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
1060 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
1116 KB |
Output is correct |
44 |
Correct |
2 ms |
1116 KB |
Output is correct |
45 |
Correct |
2 ms |
1116 KB |
Output is correct |
46 |
Correct |
3 ms |
1372 KB |
Output is correct |
47 |
Correct |
3 ms |
1444 KB |
Output is correct |
48 |
Correct |
3 ms |
1112 KB |
Output is correct |
49 |
Correct |
3 ms |
1368 KB |
Output is correct |
50 |
Correct |
2 ms |
1116 KB |
Output is correct |
51 |
Correct |
2 ms |
1116 KB |
Output is correct |
52 |
Correct |
2 ms |
1320 KB |
Output is correct |
53 |
Correct |
3 ms |
1116 KB |
Output is correct |
54 |
Correct |
3 ms |
1372 KB |
Output is correct |
55 |
Correct |
6 ms |
1884 KB |
Output is correct |
56 |
Correct |
24 ms |
6144 KB |
Output is correct |
57 |
Correct |
47 ms |
10388 KB |
Output is correct |
58 |
Correct |
58 ms |
13148 KB |
Output is correct |
59 |
Correct |
81 ms |
17604 KB |
Output is correct |
60 |
Correct |
105 ms |
21588 KB |
Output is correct |
61 |
Correct |
121 ms |
24436 KB |
Output is correct |
62 |
Correct |
135 ms |
27252 KB |
Output is correct |
63 |
Correct |
183 ms |
32340 KB |
Output is correct |
64 |
Correct |
235 ms |
34012 KB |
Output is correct |
65 |
Correct |
77 ms |
23892 KB |
Output is correct |
66 |
Correct |
64 ms |
23884 KB |
Output is correct |
67 |
Correct |
62 ms |
23888 KB |
Output is correct |
68 |
Correct |
131 ms |
40272 KB |
Output is correct |
69 |
Correct |
153 ms |
41604 KB |
Output is correct |
70 |
Correct |
152 ms |
41240 KB |
Output is correct |
71 |
Correct |
298 ms |
59728 KB |
Output is correct |
72 |
Correct |
287 ms |
59728 KB |
Output is correct |
73 |
Correct |
275 ms |
59728 KB |
Output is correct |
74 |
Correct |
259 ms |
59732 KB |
Output is correct |
75 |
Correct |
279 ms |
58960 KB |
Output is correct |
76 |
Correct |
243 ms |
59332 KB |
Output is correct |
77 |
Correct |
247 ms |
57664 KB |
Output is correct |
78 |
Correct |
251 ms |
57712 KB |
Output is correct |
79 |
Correct |
257 ms |
60608 KB |
Output is correct |