#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 2e5;
const int inf = 2e9;
int n, h;
int heights[MAXN];
mt19937 gen(0);
pii operator+(pii a, pii b) {
return pii(a.first+b.first, a.second+b.second);
}
class Node;
class Node {
public:
Node *l = nullptr;
Node *r = nullptr;
unsigned int key;
pii val; //
pii sum;
Node(pii v) : val(v) {
key = gen();
sum = val;
}
static pii get_val(Node *rt) {
return (rt == nullptr) ? pii(0, 0) : rt->val;
}
static pii get_sum(Node *rt) {
return (rt == nullptr) ? pii(0, 0) : rt->sum;
}
void make_sum() {
sum = val+get_sum(l)+get_sum(r);
}
void make_inorder(vector<pii> &res) {
if (l) l->make_inorder(res);
res.push_back(val);
if (r) r->make_inorder(res);
}
};
void merge(Node *l, Node *r, Node *&rt) {
if (l == nullptr) {
rt = r;
return;
}
if (r == nullptr) {
rt = l;
return;
}
if (l->key < r->key) {
rt = l;
merge(l->r, r, l->r);
return rt->make_sum();
}
rt = r;
merge(l, r->l, r->l);
rt->make_sum();
}
void split(Node *rt, Node *&l, Node *&r, int v) { // split by x-value, for adding a new line
int amt = Node::get_sum(rt->l).second;
if (amt > v) {
r = rt;
split(rt->l, l, r->l, v);
return r->make_sum();
}
if (amt+rt->val.second < v) {
l = rt;
split(rt->r, l->r, r, v-amt-rt->val.second);
return l->make_sum();
}
r = rt->r;
l = rt;
l->r = nullptr;
return l->make_sum();
}
void split2(Node *rt, Node *&l, Node *&r, int v) { // split by slope, for expanding
int amt = Node::get_sum(rt->l).first;
if (amt > v) {
r = rt;
split2(rt->l, l, r->l, v);
return r->make_sum();
}
if (amt+rt->val.first < v) {
l = rt;
split2(rt->r, l->r, r, v-amt-rt->val.first);
return l->make_sum();
}
l = rt->l;
r = rt;
r->l = nullptr;
return r->make_sum();
}
int get_r_dist(Node *rt) {
if (rt->r != nullptr) return get_r_dist(rt->r);
return rt->val.second;
}
void set_r_dist(Node *rt, int v) {
if (rt->r != nullptr) set_r_dist(rt->r, v);
else rt->val.second = v;
rt->make_sum();
}
void sub_l_slope(Node *rt, int v) {
if (rt->l != nullptr) sub_l_slope(rt->l, v);
else rt->val.first -= v;
rt->make_sum();
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> h;
for (int i = 0; i < n; i++) cin >> heights[i];
Node *tree = new Node(pii(0, inf));
int lzx = 0; // + amount by which tree is supposed to be left shifted
int init_slope = 0; // + initial slope
int y = 0;
for (int i = n-1; i >= 0; i--) {
Node *l;
Node *r;
split(tree, l, r, heights[i]+lzx);
y += heights[i]+lzx;
int rdist = Node::get_sum(l).second-heights[i]-lzx;
int ldist = get_r_dist(l)-rdist;
set_r_dist(l, ldist);
Node *mid = new Node(pii(2, rdist));
init_slope++;
merge(l, mid, l);
merge(l, r, tree);
split2(tree, l, r, init_slope);
int ex_slope = init_slope-Node::get_sum(l).first;
sub_l_slope(r, ex_slope);
mid = new Node(pii(ex_slope, 2*h));
lzx += h;
merge(l, mid, l);
merge(l, r, tree);
}
Node *l;
Node *r;
split2(tree, l, r, init_slope);
vector<pii> inorder;
l->make_inorder(inorder);
for (pii p: inorder) {
init_slope -= p.first;
y -= init_slope*p.second;
}
cout << y << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
19980 KB |
Output is correct |
2 |
Correct |
351 ms |
31256 KB |
Output is correct |
3 |
Correct |
390 ms |
36560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 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 |
340 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 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 |
340 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 |
6 ms |
1108 KB |
Output is correct |
29 |
Correct |
4 ms |
980 KB |
Output is correct |
30 |
Correct |
4 ms |
1128 KB |
Output is correct |
31 |
Correct |
4 ms |
980 KB |
Output is correct |
32 |
Correct |
4 ms |
980 KB |
Output is correct |
33 |
Correct |
5 ms |
1108 KB |
Output is correct |
34 |
Correct |
4 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
292 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
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 |
6 ms |
1108 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
4 ms |
1128 KB |
Output is correct |
37 |
Correct |
4 ms |
980 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
5 ms |
1108 KB |
Output is correct |
40 |
Correct |
4 ms |
1108 KB |
Output is correct |
41 |
Correct |
5 ms |
1160 KB |
Output is correct |
42 |
Correct |
4 ms |
852 KB |
Output is correct |
43 |
Correct |
5 ms |
1108 KB |
Output is correct |
44 |
Correct |
5 ms |
1108 KB |
Output is correct |
45 |
Correct |
4 ms |
1108 KB |
Output is correct |
46 |
Correct |
4 ms |
988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
292 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
251 ms |
19980 KB |
Output is correct |
20 |
Correct |
351 ms |
31256 KB |
Output is correct |
21 |
Correct |
390 ms |
36560 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 |
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 |
6 ms |
1108 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
4 ms |
1128 KB |
Output is correct |
40 |
Correct |
4 ms |
980 KB |
Output is correct |
41 |
Correct |
4 ms |
980 KB |
Output is correct |
42 |
Correct |
5 ms |
1108 KB |
Output is correct |
43 |
Correct |
4 ms |
1108 KB |
Output is correct |
44 |
Correct |
5 ms |
1160 KB |
Output is correct |
45 |
Correct |
4 ms |
852 KB |
Output is correct |
46 |
Correct |
5 ms |
1108 KB |
Output is correct |
47 |
Correct |
5 ms |
1108 KB |
Output is correct |
48 |
Correct |
4 ms |
1108 KB |
Output is correct |
49 |
Correct |
4 ms |
988 KB |
Output is correct |
50 |
Correct |
401 ms |
33096 KB |
Output is correct |
51 |
Correct |
298 ms |
28072 KB |
Output is correct |
52 |
Correct |
188 ms |
27236 KB |
Output is correct |
53 |
Correct |
215 ms |
31064 KB |
Output is correct |
54 |
Correct |
207 ms |
31792 KB |
Output is correct |
55 |
Correct |
191 ms |
27576 KB |
Output is correct |
56 |
Correct |
202 ms |
29960 KB |
Output is correct |
57 |
Correct |
188 ms |
27864 KB |
Output is correct |
58 |
Correct |
148 ms |
24232 KB |
Output is correct |
59 |
Correct |
209 ms |
30384 KB |
Output is correct |