Submission #771033

# Submission time Handle Problem Language Result Execution time Memory
771033 2023-07-02T11:16:33 Z dxz05 Rabbit Carrot (LMIO19_triusis) C++17
100 / 100
185 ms 20384 KB
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx,avx2")

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 2e5 + 30;

template <typename T>
struct SegmentTree{
    struct node{
        T Min, Max;
        T add_lazy, assign_lazy;
        bool isAdded, isAssigned;
        node(){
            add_lazy = assign_lazy = 0;
            isAdded = isAssigned = false;
            Min = Max = 0;
        };
        node(T x){
            add_lazy = assign_lazy = 0;
            isAdded = isAssigned = false;
            Min = Max = x;
        }
    };

    vector<node> tree;

    node neutral_element = node(1e6); // e.g. 0 for sum, INF for min ...;

    inline void combine(node &par, node lf, node rg){
        par.Min = min(lf.Min, rg.Min);
        par.Max = max(lf.Max, rg.Max);
    }

    int _begin, _end;

    inline void init(int n){
        _begin = 0;
        _end = n - 1;
        tree.assign(n * 4 + 5, neutral_element);
    }

    inline void upd(int v, char op, T lazy){
        if (op == '='){
            tree[v].Min = lazy;
            tree[v].Max = lazy;
            tree[v].assign_lazy = lazy;
            tree[v].isAssigned = true;

            tree[v].add_lazy = 0;
            tree[v].isAdded = false;
        }
        if (op == '+'){
            tree[v].Min += lazy;
            tree[v].Max += lazy;
            tree[v].add_lazy += lazy;
            tree[v].isAdded = true;
        }
    }

    inline void push(int v, int tl, int tr){
        if (tl == tr) return;

        if (tree[v].isAssigned){
            upd(v << 1, '=', tree[v].assign_lazy);
            upd(v << 1 | 1, '=', tree[v].assign_lazy);
            tree[v].isAssigned = false;
            tree[v].assign_lazy = 0;
        }
        if (tree[v].isAdded){
            upd(v << 1, '+', tree[v].add_lazy);
            upd(v << 1 | 1, '+', tree[v].add_lazy);
            tree[v].isAdded = false;
            tree[v].add_lazy = 0;
        }

    }

    inline void update(int v, int tl, int tr, int l, int r, char op, T val){
        push(v, tl, tr);
        if (l <= tl && tr <= r){
            upd(v, op, val);
            return;
        }
        if (tl > r || tr < l) return;

        int tm = (tl + tr) >> 1;
        update(v << 1, tl, tm, l, r, op, val);
        update(v << 1 | 1, tm + 1, tr, l, r, op, val);

        combine(tree[v], tree[v << 1], tree[v << 1 | 1]);
    }

    inline void update(int l, int r, char op, T val){
        update(1, _begin, _end, l, r, op, val);
    }

    inline void update(int pos, char op, T val){
        update(1, _begin, _end, pos, pos, op, val);
    }

    inline node get(int v, int tl, int tr, int l, int r){
        push(v, tl, tr);
        if (l <= tl && tr <= r) return tree[v];
        if (tl > r || tr < l) return neutral_element;
        int tm = (tl + tr) >> 1;
        node res;
        combine(res, get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
        return res;
    }

    inline node get(int l, int r){
        return get(1, _begin, _end, l, r);
    }

    int find(int v, int tl, int tr, int k){
        push(v, tl, tr);
        if (tl == tr) return tl;
        int tm = (tl + tr) >> 1;
        if (tree[v << 1].Max > k){
            return find(v << 1, tl, tm, k);
        } else {
            return find(v << 1 | 1, tm + 1, tr, k);
        }
    }

    int find(int k){
        if (tree[1].Max <= k) return -1;
        return find(1, _begin, _end, k);
    }

};

int a[N];
int ord[N];

void solve(){
    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= n; i++) cin >> a[i];

    vector<int> p(n + 1);
    iota(all(p), 0);

    sort(all(p), [&](int i, int j){
        return MP(a[i] - 1LL * i * m, -i) < MP(a[j] - 1LL * j * m, -j);
    });

    for (int i = 0; i <= n; i++) ord[p[i]] = i;

    SegmentTree<int> st;
    st.init(n + 1);

    st.update(ord[0], '=', 0);

    for (int i = 1; i <= n; i++){
        int j = ord[i];

        int new_dp = st.get(j, n).Min;

        st.update(0, n, '+', 1);

        if (new_dp <= n){
            int pos = st.find(new_dp);
            if (pos != -1 && pos <= j) st.update(pos, j, '=', new_dp);
        }

//        for (int t = 0; t <= n; t++){cout << st.get(t, t).Min << ' ';} cout << endl;

    }

    cout << st.get(0, n).Min << endl;

}

int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test_cases = 1;
//    cin >> test_cases;

    for (int test = 1; test <= test_cases; test++){
        //cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }

#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif

    return 0;
}

Compilation message

triusis.cpp: In function 'int main()':
triusis.cpp:191:13: warning: unused variable 'startTime' [-Wunused-variable]
  191 |     clock_t startTime = clock();
      |             ^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 328 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 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 1 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 328 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3 ms 596 KB Output is correct
21 Correct 3 ms 724 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 3 ms 728 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
25 Correct 3 ms 724 KB Output is correct
26 Correct 3 ms 752 KB Output is correct
27 Correct 3 ms 724 KB Output is correct
28 Correct 3 ms 728 KB Output is correct
29 Correct 3 ms 724 KB Output is correct
30 Correct 4 ms 724 KB Output is correct
31 Correct 5 ms 724 KB Output is correct
32 Correct 3 ms 724 KB Output is correct
33 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 3 ms 728 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 3 ms 752 KB Output is correct
11 Correct 3 ms 724 KB Output is correct
12 Correct 3 ms 728 KB Output is correct
13 Correct 3 ms 724 KB Output is correct
14 Correct 4 ms 724 KB Output is correct
15 Correct 5 ms 724 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 328 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 2 ms 852 KB Output is correct
36 Correct 3 ms 852 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 4 ms 728 KB Output is correct
39 Correct 3 ms 848 KB Output is correct
40 Correct 4 ms 724 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 3 ms 724 KB Output is correct
43 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 852 KB Output is correct
2 Correct 2 ms 852 KB Output is correct
3 Correct 3 ms 852 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 4 ms 728 KB Output is correct
6 Correct 3 ms 848 KB Output is correct
7 Correct 4 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 3 ms 724 KB Output is correct
16 Correct 2 ms 724 KB Output is correct
17 Correct 3 ms 728 KB Output is correct
18 Correct 3 ms 724 KB Output is correct
19 Correct 3 ms 724 KB Output is correct
20 Correct 3 ms 752 KB Output is correct
21 Correct 3 ms 724 KB Output is correct
22 Correct 3 ms 728 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 4 ms 724 KB Output is correct
25 Correct 5 ms 724 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 4 ms 724 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 328 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 135 ms 18448 KB Output is correct
45 Correct 123 ms 20268 KB Output is correct
46 Correct 176 ms 20204 KB Output is correct
47 Correct 185 ms 20384 KB Output is correct
48 Correct 141 ms 19148 KB Output is correct
49 Correct 160 ms 19472 KB Output is correct
50 Correct 162 ms 20260 KB Output is correct
51 Correct 152 ms 20280 KB Output is correct
52 Correct 177 ms 19176 KB Output is correct
53 Correct 80 ms 19540 KB Output is correct
54 Correct 139 ms 19368 KB Output is correct
55 Correct 143 ms 19380 KB Output is correct