#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cstdio>
#include <algorithm>
using namespace std;
#define int long long
struct func {
int k;
int b;
func(int k, int b): k(k), b(b) {}
int get(int x) {
return k * x + b;
}
};
struct node {
int x, y, add, sz;
node* l = nullptr;
node* r = nullptr;
node(int x): x(x) {
y = rand();
add = 0;
sz = 1;
}
};
void push(node* v) {
if (!v) return;
if (v->l) {
v->l->add += v->add;
}
if (v->r) {
v->r->add += v->add;
}
v->x += v->add;
v->add = 0;
}
int s(node* v) {
if (!v) return 0;
return v->sz;
}
void upd(node* v) {
if (!v) return;
v->sz = 1 + s(v->l) + s(v->r);
}
node* root = nullptr;
node* merge(node* l, node* r) {
if (!l) return r;
if (!r) return l;
if (l->y > r->y) {
push(l);
l->r = merge(l->r, r);
upd(l);
return l;
}
push(r);
r->l = merge(l, r->l);
upd(r);
return r;
}
pair<node*, node*> split(node* v, int k) {
if (!v) return { nullptr, nullptr };
push(v);
if (s(v->l) + 1 <= k) {
pair<node*, node*> q = split(v->r, k - s(v->l) - 1);
v->r = q.first;
upd(v);
return { v, q.second };
}
pair<node*, node*> q = split(v->l, k);
v->l = q.second;
upd(v);
return { q.first, v };
}
pair<node*, node*> split1(node* v, int k) {
if (!v) return { nullptr, nullptr };
push(v);
if (v->x <= k) {
pair<node*, node*> q = split1(v->r, k);
v->r = q.first;
upd(v);
return { v, q.second };
}
pair<node*, node*> q = split1(v->l, k);
v->l = q.second;
upd(v);
return { q.first, v };
}
void add(int x) {
pair<node*, node*> q = split1(root, x);
node* t = new node(x);
root = merge(q.first, merge(t, q.second));
}
int back(node* v) {
push(v);
if (v->r) {
return back(v->r);
}
return v->x;
}
void walk(node* v, vector<int> &x) {
if (!v) return;
push(v);
walk(v->l, x);
x.push_back(v->x);
walk(v->r, x);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, h;
cin >> n >> h;
vector<int> a(n);
for (int& i : a) {
cin >> i;
}
add(a[0]);
add(a[0]);
func f(1, -a[0]);
for (int i = 1; i < n; ++i) {
// expand f->g
int val = f.k * back(root) + f.b;
pair<node*, node*> q = split(root, s(root) - f.k);
if (q.first) q.first->add -= h;
if (q.second) q.second->add += h;
root = merge(q.first, q.second);
f.b = val - f.k * back(root);
// add new function
add(a[i]);
add(a[i]);
f.k += 1;
f.b -= a[i];
}
int curr = f.k;
vector<int> x;
walk(root, x);
auto pt = x.rbegin();
while (curr--) {
int x = *pt;
int val = f.k * x + f.b;
--f.k;
f.b = val - f.k * x;
++pt;
}
cout << f.b;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
23348 KB |
Output is correct |
2 |
Correct |
518 ms |
31412 KB |
Output is correct |
3 |
Correct |
601 ms |
32384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
9 ms |
1176 KB |
Output is correct |
29 |
Correct |
7 ms |
1036 KB |
Output is correct |
30 |
Correct |
9 ms |
1100 KB |
Output is correct |
31 |
Correct |
10 ms |
1004 KB |
Output is correct |
32 |
Correct |
6 ms |
980 KB |
Output is correct |
33 |
Correct |
7 ms |
1184 KB |
Output is correct |
34 |
Correct |
8 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
9 ms |
1176 KB |
Output is correct |
35 |
Correct |
7 ms |
1036 KB |
Output is correct |
36 |
Correct |
9 ms |
1100 KB |
Output is correct |
37 |
Correct |
10 ms |
1004 KB |
Output is correct |
38 |
Correct |
6 ms |
980 KB |
Output is correct |
39 |
Correct |
7 ms |
1184 KB |
Output is correct |
40 |
Correct |
8 ms |
1364 KB |
Output is correct |
41 |
Correct |
8 ms |
1236 KB |
Output is correct |
42 |
Correct |
7 ms |
980 KB |
Output is correct |
43 |
Correct |
8 ms |
1160 KB |
Output is correct |
44 |
Correct |
7 ms |
1108 KB |
Output is correct |
45 |
Correct |
7 ms |
1108 KB |
Output is correct |
46 |
Correct |
6 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
316 KB |
Output is correct |
19 |
Correct |
342 ms |
23348 KB |
Output is correct |
20 |
Correct |
518 ms |
31412 KB |
Output is correct |
21 |
Correct |
601 ms |
32384 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
9 ms |
1176 KB |
Output is correct |
38 |
Correct |
7 ms |
1036 KB |
Output is correct |
39 |
Correct |
9 ms |
1100 KB |
Output is correct |
40 |
Correct |
10 ms |
1004 KB |
Output is correct |
41 |
Correct |
6 ms |
980 KB |
Output is correct |
42 |
Correct |
7 ms |
1184 KB |
Output is correct |
43 |
Correct |
8 ms |
1364 KB |
Output is correct |
44 |
Correct |
8 ms |
1236 KB |
Output is correct |
45 |
Correct |
7 ms |
980 KB |
Output is correct |
46 |
Correct |
8 ms |
1160 KB |
Output is correct |
47 |
Correct |
7 ms |
1108 KB |
Output is correct |
48 |
Correct |
7 ms |
1108 KB |
Output is correct |
49 |
Correct |
6 ms |
1108 KB |
Output is correct |
50 |
Correct |
585 ms |
33132 KB |
Output is correct |
51 |
Correct |
417 ms |
28172 KB |
Output is correct |
52 |
Correct |
315 ms |
27140 KB |
Output is correct |
53 |
Correct |
374 ms |
31104 KB |
Output is correct |
54 |
Correct |
367 ms |
31812 KB |
Output is correct |
55 |
Correct |
319 ms |
27696 KB |
Output is correct |
56 |
Correct |
358 ms |
30168 KB |
Output is correct |
57 |
Correct |
322 ms |
27828 KB |
Output is correct |
58 |
Correct |
261 ms |
24256 KB |
Output is correct |
59 |
Correct |
372 ms |
30468 KB |
Output is correct |