답안 #1091219

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091219 2024-09-20T07:38:58 Z tibinyte Safety (NOI18_safety) C++17
100 / 100
787 ms 29168 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;
}

void print(treap *&t)
{
    push(t);
    if (t->l)
    {
        print(t->l);
    }
    cout << t->val << ' ';
    if (t->r)
    {
        print(t->r);
    }
}

struct brute
{
    vector<int> a;
    void insert(int x)
    {
        a.push_back(x);
        sort(a.begin(), a.end());
    }
    int pop()
    {
        int ans = a.back();
        a.pop_back();
        return ans;
    }
    void move(int pref, int h)
    {
        pref--;
        for (int i = 0; i <= pref; ++i)
        {
            a[i] -= h;
        }
        for (int i = pref + 1; i < a.size(); ++i)
        {
            a[i] += h;
        }
    }
};

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) {}
      |     ^~~~~
safety.cpp: In member function 'void brute::move(long long int, long long int)':
safety.cpp:271:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  271 |         for (int i = pref + 1; i < a.size(); ++i)
      |                                ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 452 ms 18972 KB Output is correct
2 Correct 638 ms 27024 KB Output is correct
3 Correct 717 ms 28148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 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 432 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 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 432 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 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 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 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 432 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 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 600 KB Output is correct
28 Correct 12 ms 1116 KB Output is correct
29 Correct 10 ms 860 KB Output is correct
30 Correct 11 ms 1064 KB Output is correct
31 Correct 9 ms 856 KB Output is correct
32 Correct 8 ms 860 KB Output is correct
33 Correct 11 ms 1116 KB Output is correct
34 Correct 10 ms 984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 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 432 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 2 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 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 600 KB Output is correct
34 Correct 12 ms 1116 KB Output is correct
35 Correct 10 ms 860 KB Output is correct
36 Correct 11 ms 1064 KB Output is correct
37 Correct 9 ms 856 KB Output is correct
38 Correct 8 ms 860 KB Output is correct
39 Correct 11 ms 1116 KB Output is correct
40 Correct 10 ms 984 KB Output is correct
41 Correct 15 ms 1116 KB Output is correct
42 Correct 8 ms 996 KB Output is correct
43 Correct 11 ms 1116 KB Output is correct
44 Correct 11 ms 1024 KB Output is correct
45 Correct 10 ms 1116 KB Output is correct
46 Correct 10 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 452 ms 18972 KB Output is correct
20 Correct 638 ms 27024 KB Output is correct
21 Correct 717 ms 28148 KB Output is correct
22 Correct 1 ms 344 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 432 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 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 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 12 ms 1116 KB Output is correct
38 Correct 10 ms 860 KB Output is correct
39 Correct 11 ms 1064 KB Output is correct
40 Correct 9 ms 856 KB Output is correct
41 Correct 8 ms 860 KB Output is correct
42 Correct 11 ms 1116 KB Output is correct
43 Correct 10 ms 984 KB Output is correct
44 Correct 15 ms 1116 KB Output is correct
45 Correct 8 ms 996 KB Output is correct
46 Correct 11 ms 1116 KB Output is correct
47 Correct 11 ms 1024 KB Output is correct
48 Correct 10 ms 1116 KB Output is correct
49 Correct 10 ms 860 KB Output is correct
50 Correct 787 ms 29168 KB Output is correct
51 Correct 566 ms 23892 KB Output is correct
52 Correct 446 ms 22980 KB Output is correct
53 Correct 535 ms 26704 KB Output is correct
54 Correct 560 ms 27540 KB Output is correct
55 Correct 449 ms 23376 KB Output is correct
56 Correct 582 ms 25796 KB Output is correct
57 Correct 498 ms 23628 KB Output is correct
58 Correct 424 ms 19976 KB Output is correct
59 Correct 517 ms 26308 KB Output is correct