#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
mt19937 rng(42);
struct Node {
int sz=1, pri=rng();
ll val=0, lz=0;
Node *left=nullptr, *right=nullptr;
Node(ll x) : val(x) {}
};
int sz(Node* u) {
return u?u->sz:0;
}
Node* cmb(Node* u) {
u->sz=1+sz(u->left)+sz(u->right);
return u;
}
void psh(Node* u) {
if (u&&u->lz) {
u->val+=u->lz;
if (u->left)
u->left->lz+=u->lz;
if (u->right)
u->right->lz+=u->lz;
u->lz=0;
}
}
ar<Node*, 2> split1(Node* u, int k) {
if (!u)
return {nullptr, nullptr};
psh(u);
if (sz(u->left)>=k) {
ar<Node*, 2> x=split1(u->left, k);
u->left=x[1];
return {x[0], cmb(u)};
} else {
ar<Node*, 2> x=split1(u->right, k-sz(u->left)-1);
u->right=x[0];
return {cmb(u), x[1]};
}
}
ar<Node*, 2> split2(Node* u, ll k) {
if (!u)
return {nullptr, nullptr};
psh(u);
if (u->val>k) {
ar<Node*, 2> x=split2(u->left, k);
u->left=x[1];
return {x[0], cmb(u)};
} else {
ar<Node*, 2> x=split2(u->right, k);
u->right=x[0];
return {cmb(u), x[1]};
}
}
Node* mrg(Node* u, Node* v) {
psh(u), psh(v);
if (!u||!v)
return u?u:v;
if (u->pri>v->pri) {
u->right=mrg(u->right, v);
return cmb(u);
} else {
v->left=mrg(u, v->left);
return cmb(v);
}
}
void app(Node* u, ll x) {
assert(u);
u->lz+=x;
psh(u);
}
ll walk(Node* u, int dir) {
while(1) {
psh(u);
Node* nxt=dir==0?u->left:u->right;
if (nxt)
u=nxt;
else
break;
}
return u->val;
}
const int mxN=2e5;
int n, h, a[mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> h;
for (int i=0; i<n; ++i)
cin >> a[i];
ll ans=0;
Node* rt=mrg(new Node(a[0]), new Node(a[0]));
for (int i=1; i<n; ++i) {
ar<Node*, 2> x=split1(rt, i);
app(x[0], -h);
app(x[1], h);
ll bound=walk(x[0], 1);
if (a[i]<bound)
ans+=bound-a[i];
else {
bound=walk(x[1], 0);
if (a[i]>bound)
ans+=a[i]-bound;
}
rt=mrg(x[0], x[1]);
x=split2(rt, a[i]);
rt=mrg(mrg(x[0], new Node(a[i])), mrg(new Node(a[i]), x[1]));
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
13536 KB |
Output is correct |
2 |
Correct |
361 ms |
20420 KB |
Output is correct |
3 |
Correct |
360 ms |
21196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
344 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
344 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 |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 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 |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
344 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 |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 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 |
7 ms |
728 KB |
Output is correct |
29 |
Correct |
5 ms |
764 KB |
Output is correct |
30 |
Correct |
8 ms |
736 KB |
Output is correct |
31 |
Correct |
6 ms |
724 KB |
Output is correct |
32 |
Correct |
5 ms |
728 KB |
Output is correct |
33 |
Correct |
6 ms |
732 KB |
Output is correct |
34 |
Correct |
6 ms |
732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
336 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 |
344 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 |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 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 |
7 ms |
728 KB |
Output is correct |
35 |
Correct |
5 ms |
764 KB |
Output is correct |
36 |
Correct |
8 ms |
736 KB |
Output is correct |
37 |
Correct |
6 ms |
724 KB |
Output is correct |
38 |
Correct |
5 ms |
728 KB |
Output is correct |
39 |
Correct |
6 ms |
732 KB |
Output is correct |
40 |
Correct |
6 ms |
732 KB |
Output is correct |
41 |
Correct |
6 ms |
780 KB |
Output is correct |
42 |
Correct |
4 ms |
808 KB |
Output is correct |
43 |
Correct |
5 ms |
724 KB |
Output is correct |
44 |
Correct |
5 ms |
860 KB |
Output is correct |
45 |
Correct |
5 ms |
724 KB |
Output is correct |
46 |
Correct |
5 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
276 ms |
13536 KB |
Output is correct |
20 |
Correct |
361 ms |
20420 KB |
Output is correct |
21 |
Correct |
360 ms |
21196 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 |
344 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 |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 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 |
7 ms |
728 KB |
Output is correct |
38 |
Correct |
5 ms |
764 KB |
Output is correct |
39 |
Correct |
8 ms |
736 KB |
Output is correct |
40 |
Correct |
6 ms |
724 KB |
Output is correct |
41 |
Correct |
5 ms |
728 KB |
Output is correct |
42 |
Correct |
6 ms |
732 KB |
Output is correct |
43 |
Correct |
6 ms |
732 KB |
Output is correct |
44 |
Correct |
6 ms |
780 KB |
Output is correct |
45 |
Correct |
4 ms |
808 KB |
Output is correct |
46 |
Correct |
5 ms |
724 KB |
Output is correct |
47 |
Correct |
5 ms |
860 KB |
Output is correct |
48 |
Correct |
5 ms |
724 KB |
Output is correct |
49 |
Correct |
5 ms |
716 KB |
Output is correct |
50 |
Correct |
439 ms |
21724 KB |
Output is correct |
51 |
Correct |
304 ms |
17992 KB |
Output is correct |
52 |
Correct |
260 ms |
17344 KB |
Output is correct |
53 |
Correct |
254 ms |
20172 KB |
Output is correct |
54 |
Correct |
276 ms |
20824 KB |
Output is correct |
55 |
Correct |
236 ms |
17716 KB |
Output is correct |
56 |
Correct |
239 ms |
19452 KB |
Output is correct |
57 |
Correct |
218 ms |
17844 KB |
Output is correct |
58 |
Correct |
182 ms |
14968 KB |
Output is correct |
59 |
Correct |
320 ms |
19716 KB |
Output is correct |