Submission #854348

# Submission time Handle Problem Language Result Execution time Memory
854348 2023-09-27T01:23:03 Z fanwen Pinball (JOI14_pinball) C++17
100 / 100
171 ms 16588 KB
#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

template < class T, T (*f) (T, T), T (*e) ()>
struct segment_tree {
    vector <T> it;
    int n;

    void update(int u, T p) {
        u += n - 1;
        it[u] = f(it[u], p);
        for(; u >>= 1; ) {
            it[u] = f(it[u << 1], it[u << 1 | 1]);
        }
    }

    void set(int u, T p) {
        u--;
        for (it[u += n] = p; u >>= 1; ) {
            it[u] = f(it[u << 1], it[u << 1 | 1]);
        }
    }

    T get(int p) {
        return it[p += n - 1];
    }

    T get(int l, int r) {
        l--;
        T resl = e(), resr = e();
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) resl = f(resl, it[l++]);
            if(r & 1) resr = f(it[--r], resr);
        }
        return f(resl, resr);
    }

    segment_tree(int n = 0) : n(n), it(n << 1 | 1) {
        fill(it.begin(), it.end(), T{});
    }
};

namespace _min_ {
    template <class T> T op(T a, T b) { return a < b ? a : b; }
    template <class T> T e() { return numeric_limits<T>::max(); }
};

void you_make_it(void) {
    int n, m; cin >> n >> m;
    vector <tuple <int, int, int, int>> A(n);
    vector <int> compress;
    for (auto &[a, b, c, d] : A) {
        cin >> a >> b >> c >> d;
        compress.emplace_back(a);
        compress.emplace_back(b);
        compress.emplace_back(c);
    }
    sort(ALL(compress));
    if(*compress.begin() != 1 || compress.end()[-1] != m) {
        cout << -1;
        return;
    }
    compress.erase(unique(ALL(compress)), compress.end());
    m = (int) compress.size();
    segment_tree <long long, _min_::op, _min_::e> L(m), R(m);

    const long long INF = 1e18;
    FOR(i, 1, m) L.set(i, INF), R.set(i, INF);

    auto update = [&] (int &x) {
        x = lower_bound(ALL(compress), x) - compress.begin() + 1;
    };
    long long ans = INF;
    for (auto &[a, b, c, d] : A) {
        update(a), update(b), update(c);
        if(a == 1 && b == m) {
            minimize(ans, d);
            continue;
        }     
        if(a == 1) {
            auto f = R.get(a, b);
            minimize(ans, f + d);
            R.update(c, f + d);
            L.update(c, d);
        } else if(b == m) {
            auto f = L.get(a, b);
            minimize(ans, f + d);
            L.update(c, f + d);
            R.update(c, d);
        } else {
            auto l = L.get(a, b), r = R.get(a, b);
            minimize(ans, l + r + d);
            L.update(c, l + d);
            R.update(c, r + d);
        }
    }
    cout << (ans >= INF ? -1 : ans);
}
signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

pinball.cpp: In instantiation of 'segment_tree<T, f, e>::segment_tree(int) [with T = long long int; T (* f)(T, T) = _min_::op<long long int>; T (* e)() = _min_::e<long long int>]':
pinball.cpp:79:54:   required from here
pinball.cpp:21:9: warning: 'segment_tree<long long int, _min_::op<long long int>, _min_::e<long long int> >::n' will be initialized after [-Wreorder]
   21 |     int n;
      |         ^
pinball.cpp:20:16: warning:   'std::vector<long long int, std::allocator<long long int> > segment_tree<long long int, _min_::op<long long int>, _min_::e<long long int> >::it' [-Wreorder]
   20 |     vector <T> it;
      |                ^~
pinball.cpp:52:5: warning:   when initialized here [-Wreorder]
   52 |     segment_tree(int n = 0) : n(n), it(n << 1 | 1) {
      |     ^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 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 528 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 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 528 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 11 ms 1628 KB Output is correct
26 Correct 31 ms 3796 KB Output is correct
27 Correct 88 ms 8192 KB Output is correct
28 Correct 79 ms 7376 KB Output is correct
29 Correct 63 ms 6740 KB Output is correct
30 Correct 98 ms 7624 KB Output is correct
31 Correct 134 ms 12244 KB Output is correct
32 Correct 125 ms 10748 KB Output is correct
33 Correct 19 ms 3560 KB Output is correct
34 Correct 64 ms 8400 KB Output is correct
35 Correct 99 ms 16460 KB Output is correct
36 Correct 171 ms 16360 KB Output is correct
37 Correct 142 ms 16588 KB Output is correct
38 Correct 121 ms 16380 KB Output is correct