Submission #1091222

# Submission time Handle Problem Language Result Execution time Memory
1091222 2024-09-20T07:40:23 Z tibinyte Safety (NOI18_safety) C++17
100 / 100
776 ms 28752 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

const int inf = 1e9;

struct treap // implicit key treap
{
    int prior, val, cnt;
    int maxi, lazy;
    treap *l, *r;
    treap(int val) : val(val), lazy(0), prior(random(1, 1e9)), cnt(1), l(nullptr), r(nullptr), 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:90:15: warning: 'treap::lazy' will be initialized after [-Wreorder]
   90 |     int maxi, lazy;
      |               ^~~~
safety.cpp:89:9: warning:   'long long int treap::prior' [-Wreorder]
   89 |     int prior, val, cnt;
      |         ^~~~~
safety.cpp:92:5: warning:   when initialized here [-Wreorder]
   92 |     treap(int val) : val(val), lazy(0), prior(random(1, 1e9)), cnt(1), l(nullptr), r(nullptr), maxi(val) {}
      |     ^~~~~
safety.cpp:91:16: warning: 'treap::r' will be initialized after [-Wreorder]
   91 |     treap *l, *r;
      |                ^
safety.cpp:90:9: warning:   'long long int treap::maxi' [-Wreorder]
   90 |     int maxi, lazy;
      |         ^~~~
safety.cpp:92:5: warning:   when initialized here [-Wreorder]
   92 |     treap(int val) : val(val), lazy(0), prior(random(1, 1e9)), cnt(1), l(nullptr), r(nullptr), maxi(val) {}
      |     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 1 ms 344 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 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 19080 KB Output is correct
2 Correct 719 ms 26964 KB Output is correct
3 Correct 700 ms 28048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 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 1 ms 348 KB Output is correct
19 Correct 1 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 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 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 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 600 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 604 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 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 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 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 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 600 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 604 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 12 ms 1368 KB Output is correct
29 Correct 9 ms 860 KB Output is correct
30 Correct 10 ms 1140 KB Output is correct
31 Correct 8 ms 980 KB Output is correct
32 Correct 9 ms 856 KB Output is correct
33 Correct 11 ms 1144 KB Output is correct
34 Correct 10 ms 1092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 1 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 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 1 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 2 ms 460 KB Output is correct
27 Correct 1 ms 600 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 604 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 12 ms 1368 KB Output is correct
35 Correct 9 ms 860 KB Output is correct
36 Correct 10 ms 1140 KB Output is correct
37 Correct 8 ms 980 KB Output is correct
38 Correct 9 ms 856 KB Output is correct
39 Correct 11 ms 1144 KB Output is correct
40 Correct 10 ms 1092 KB Output is correct
41 Correct 11 ms 980 KB Output is correct
42 Correct 8 ms 840 KB Output is correct
43 Correct 10 ms 1116 KB Output is correct
44 Correct 13 ms 1092 KB Output is correct
45 Correct 12 ms 860 KB Output is correct
46 Correct 9 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 0 ms 348 KB Output is correct
12 Correct 1 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 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 422 ms 19080 KB Output is correct
20 Correct 719 ms 26964 KB Output is correct
21 Correct 700 ms 28048 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 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 1 ms 348 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 1 ms 600 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 604 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 12 ms 1368 KB Output is correct
38 Correct 9 ms 860 KB Output is correct
39 Correct 10 ms 1140 KB Output is correct
40 Correct 8 ms 980 KB Output is correct
41 Correct 9 ms 856 KB Output is correct
42 Correct 11 ms 1144 KB Output is correct
43 Correct 10 ms 1092 KB Output is correct
44 Correct 11 ms 980 KB Output is correct
45 Correct 8 ms 840 KB Output is correct
46 Correct 10 ms 1116 KB Output is correct
47 Correct 13 ms 1092 KB Output is correct
48 Correct 12 ms 860 KB Output is correct
49 Correct 9 ms 860 KB Output is correct
50 Correct 776 ms 28752 KB Output is correct
51 Correct 571 ms 23892 KB Output is correct
52 Correct 491 ms 23124 KB Output is correct
53 Correct 549 ms 26884 KB Output is correct
54 Correct 618 ms 27356 KB Output is correct
55 Correct 479 ms 23380 KB Output is correct
56 Correct 547 ms 25424 KB Output is correct
57 Correct 507 ms 23636 KB Output is correct
58 Correct 426 ms 19992 KB Output is correct
59 Correct 612 ms 25940 KB Output is correct