# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1091231 |
2024-09-20T07:54:12 Z |
tibinyte |
Safety (NOI18_safety) |
C++17 |
|
781 ms |
26992 KB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int random(int st, int dr)
{
uniform_int_distribution<int> dist(st, dr);
return dist(rng);
}
template <typename t>
using ordered_set = tree<t, null_type, less<t>, rb_tree_tag, tree_order_statistics_node_update>;
const int mod = 1e9 + 7;
struct Mint
{
int val;
Mint(int x = 0)
{
val = x % mod;
}
Mint(long long x)
{
val = x % mod;
}
Mint operator+(Mint oth)
{
return val + oth.val;
}
Mint operator-(Mint oth)
{
return val - oth.val + mod;
}
Mint operator*(Mint oth)
{
return 1ll * val * oth.val;
}
void operator+=(Mint oth)
{
val = (*this + oth).val;
}
void operator-=(Mint oth)
{
val = (*this - oth).val;
}
void operator*=(Mint oth)
{
val = (*this * oth).val;
}
};
Mint powmod(int a, int b)
{
if (b == 0)
{
return 1;
}
if (b % 2 == 1)
{
return powmod(a, b - 1) * a;
}
Mint p = powmod(a, b / 2);
return p * p;
}
/*
.___ __ __ .__
____ ____ __| _/____ _______/ |______ ________/ |_ ______ | |__ ___________ ____
_/ ___\/ _ \ / __ |/ __ \ / ___/\ __\__ \\_ __ \ __\/ ___/ | | \_/ __ \_ __ \_/ __ \
\ \__( <_> ) /_/ \ ___/ \___ \ | | / __ \| | \/| | \___ \ | Y \ ___/| | \/\ ___/
\___ >____/\____ |\___ > /____ > |__| (____ /__| |__| /____ > |___| /\___ >__| \___ >
\/ \/ \/ \/ \/ \/ \/ \/ \/
*/
#define int long long
struct treap // implicit key treap
{
int prior, val, cnt;
int maxi, lazy;
treap *l = nullptr, *r = nullptr;
treap(int val = 0) : val(val), lazy(0), prior(random(1, 1e9)), cnt(1), maxi(val) {}
};
int cnt(treap *t)
{
return t ? t->cnt : 0;
}
void push(treap *&t)
{
if (t && t->lazy)
{
t->maxi += t->lazy;
t->val += t->lazy;
if (t->l)
{
t->l->lazy += t->lazy;
}
if (t->r)
{
t->r->lazy += t->lazy;
}
t->lazy = 0;
}
}
void pull(treap *&t)
{
if (t)
{
assert(t->lazy == 0);
push(t->l);
push(t->r);
t->cnt = cnt(t->l) + cnt(t->r) + 1;
t->maxi = t->val;
if (t->l)
{
t->maxi = max(t->maxi, t->l->maxi);
}
if (t->r)
{
t->maxi = max(t->maxi, t->r->maxi);
}
}
}
void merge(treap *&t, treap *l, treap *r)
{
push(l), push(r);
if (!l || !r)
{
t = (l ? l : r);
return;
}
if (r->prior > l->prior)
{
merge(r->l, l, r->l);
t = r;
}
else
{
merge(l->r, l->r, r);
t = l;
}
pull(t);
}
void split(treap *t, treap *&l, treap *&r, int key, int add = 0)
{
push(t);
if (!t)
{
l = r = nullptr;
return;
}
int curr_key = add + cnt(t->l) + 1;
if (curr_key <= key)
{
split(t->r, t->r, r, key, add + cnt(t->l) + 1);
l = t;
}
else
{
split(t->l, l, t->l, key, add);
r = t;
}
pull(t);
}
int find_rank(treap *&t, int x)
{
push(t);
pull(t);
if (!t || t->maxi < x)
{
return cnt(t);
}
if (t->l)
{
if (t->l->maxi > x)
{
return find_rank(t->l, x);
}
}
if (t->val > x)
{
return cnt(t->l);
}
return cnt(t->l) + 1 + find_rank(t->r, x);
}
void insert(treap *&t, int x)
{
treap *a, *b;
a = b = nullptr;
int my_rank = find_rank(t, x);
split(t, a, b, my_rank);
merge(a, a, new treap(x));
merge(t, a, b);
}
void move(treap *&t, int pref, int h)
{
treap *a, *b;
a = b = nullptr;
split(t, a, b, pref);
if (b)
b->lazy += h;
if (a)
a->lazy -= h;
merge(t, a, b);
}
int pop(treap *&t)
{
assert(cnt(t) >= 1);
int ans = t->maxi;
treap *lying = nullptr;
split(t, t, lying, cnt(t) - 1);
return ans;
}
struct line
{
int a, b;
line(int a = 0, int b = 0) : a(a), b(b) {}
int eval(int x)
{
return a * x + b;
}
line operator+(line oth)
{
return line(a + oth.a, b + oth.b);
}
};
struct slope_trick
{
treap *t = nullptr;
line rightmost;
void shift(int dist)
{
move(t, cnt(t) - rightmost.a, dist);
rightmost.b = rightmost.b - rightmost.a * dist;
}
void clean()
{
while (rightmost.a > 0)
{
int nxt = pop(t);
rightmost = line(rightmost.a - 1, nxt + rightmost.b);
}
}
void add(int x)
{
insert(t, x);
insert(t, x);
rightmost = rightmost + line(1, -x);
}
int getOpt()
{
clean();
return rightmost.b;
}
};
int32_t main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n, h;
cin >> n >> h;
vector<int> a(n + 1);
slope_trick adam;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
adam.shift(h);
adam.add(a[i]);
}
cout << adam.getOpt() << '\n';
}
/*
I cannot take this anymore
Saying everything I've said before
All these words, they make no sense
I find bliss in ignorance
Less I hear, the less you say
You'll find that out anyway
Just like before
Everything you say to me
(Takes me one step closer to the edge)
(And I'm about to break)
I need a little room to breathe
(Cause I'm one step closer to the edge)
(I'm about to break)
*/
Compilation message
safety.cpp: In constructor 'treap::treap(long long int)':
safety.cpp:88:15: warning: 'treap::lazy' will be initialized after [-Wreorder]
88 | int maxi, lazy;
| ^~~~
safety.cpp:87:9: warning: 'long long int treap::prior' [-Wreorder]
87 | int prior, val, cnt;
| ^~~~~
safety.cpp:90:5: warning: when initialized here [-Wreorder]
90 | treap(int val = 0) : val(val), lazy(0), prior(random(1, 1e9)), cnt(1), maxi(val) {}
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
488 ms |
17664 KB |
Output is correct |
2 |
Correct |
699 ms |
25172 KB |
Output is correct |
3 |
Correct |
700 ms |
26216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
352 KB |
Output is correct |
15 |
Correct |
2 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
352 KB |
Output is correct |
15 |
Correct |
2 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
420 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
352 KB |
Output is correct |
15 |
Correct |
2 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
420 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
14 ms |
960 KB |
Output is correct |
29 |
Correct |
10 ms |
860 KB |
Output is correct |
30 |
Correct |
13 ms |
920 KB |
Output is correct |
31 |
Correct |
8 ms |
856 KB |
Output is correct |
32 |
Correct |
12 ms |
916 KB |
Output is correct |
33 |
Correct |
12 ms |
1064 KB |
Output is correct |
34 |
Correct |
9 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
348 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
352 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
420 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
14 ms |
960 KB |
Output is correct |
35 |
Correct |
10 ms |
860 KB |
Output is correct |
36 |
Correct |
13 ms |
920 KB |
Output is correct |
37 |
Correct |
8 ms |
856 KB |
Output is correct |
38 |
Correct |
12 ms |
916 KB |
Output is correct |
39 |
Correct |
12 ms |
1064 KB |
Output is correct |
40 |
Correct |
9 ms |
1116 KB |
Output is correct |
41 |
Correct |
13 ms |
1092 KB |
Output is correct |
42 |
Correct |
9 ms |
892 KB |
Output is correct |
43 |
Correct |
11 ms |
992 KB |
Output is correct |
44 |
Correct |
11 ms |
856 KB |
Output is correct |
45 |
Correct |
11 ms |
860 KB |
Output is correct |
46 |
Correct |
10 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
348 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
488 ms |
17664 KB |
Output is correct |
20 |
Correct |
699 ms |
25172 KB |
Output is correct |
21 |
Correct |
700 ms |
26216 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
352 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
420 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
14 ms |
960 KB |
Output is correct |
38 |
Correct |
10 ms |
860 KB |
Output is correct |
39 |
Correct |
13 ms |
920 KB |
Output is correct |
40 |
Correct |
8 ms |
856 KB |
Output is correct |
41 |
Correct |
12 ms |
916 KB |
Output is correct |
42 |
Correct |
12 ms |
1064 KB |
Output is correct |
43 |
Correct |
9 ms |
1116 KB |
Output is correct |
44 |
Correct |
13 ms |
1092 KB |
Output is correct |
45 |
Correct |
9 ms |
892 KB |
Output is correct |
46 |
Correct |
11 ms |
992 KB |
Output is correct |
47 |
Correct |
11 ms |
856 KB |
Output is correct |
48 |
Correct |
11 ms |
860 KB |
Output is correct |
49 |
Correct |
10 ms |
844 KB |
Output is correct |
50 |
Correct |
781 ms |
26992 KB |
Output is correct |
51 |
Correct |
605 ms |
22240 KB |
Output is correct |
52 |
Correct |
474 ms |
21420 KB |
Output is correct |
53 |
Correct |
590 ms |
25072 KB |
Output is correct |
54 |
Correct |
570 ms |
25728 KB |
Output is correct |
55 |
Correct |
474 ms |
21844 KB |
Output is correct |
56 |
Correct |
606 ms |
24088 KB |
Output is correct |
57 |
Correct |
547 ms |
21992 KB |
Output is correct |
58 |
Correct |
422 ms |
19256 KB |
Output is correct |
59 |
Correct |
518 ms |
24412 KB |
Output is correct |