Submission #878930

# Submission time Handle Problem Language Result Execution time Memory
878930 2023-11-25T15:07:07 Z hafo Pinball (JOI14_pinball) C++14
100 / 100
168 ms 13656 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e18 + 69;

int m, n;
struct item {
    int l, r, c, cost;
} a[maxn];
vector<int> val;

struct sub2 {
    static const int maxn = 200 + 7;
    ll dp[maxn][407][407];

    void solve() {
        for(int i = 0; i <= n; i++) {
            for(int l = 0; l < Size(val); l++) {
                for(int r = 0; r < Size(val); r++) dp[i][l][r] = oo;
            }
        }

        for(int i = 0; i < n; i++) {
            mini(dp[i + 1][a[i].l][a[i].r], a[i].cost);
            for(int l = 0; l < Size(val); l++) {
                for(int r = l; r < Size(val); r++) {
                    if(dp[i][l][r] == oo) continue;
                    if(val[l] <= a[i].c && a[i].c <= val[r]) {
                        mini(dp[i + 1][min(l, a[i].l)][max(r, a[i].r)], dp[i][l][r] + a[i].cost);
                    }
                    mini(dp[i + 1][l][r], dp[i][l][r]);
                }
            }
        }
        ll res = dp[n][0][Size(val) - 1];
        cout<<(res == oo ? -1:res);
    }

} sub2;

struct sub3 {
    static const int maxn = 1e3 + 7;
    pair<int, ll> dp[maxn][2 * maxn];

    void solve() {
        for(int i = 0; i <= n; i++) {
            for(int r = 0; r < Size(val); r++) dp[i][r] = {Size(val), oo};
        }

        for(int i = 0; i < n; i++) {
            mini(dp[i + 1][a[i].r], make_pair(a[i].l, (ll) a[i].cost));
            for(int r = 0; r < Size(val); r++) {
                if(dp[i][r].fi == Size(val)) continue;
                int l = dp[i][r].fi;
                if(val[l] <= a[i].c && a[i].c <= val[r]) {
                    auto cur = dp[i][r];
                    mini(cur.fi, a[i].l);
                    cur.se += a[i].cost;
                    mini(dp[i + 1][max(r, a[i].r)], cur);
                }
                mini(dp[i + 1][r], dp[i][r]);
            }
        }

        auto res = dp[n][Size(val) - 1];
        cout<<(res.fi != 0 ? -1:res.se);
    }   

} sub3;

struct ST {
    struct node {
        ll mn;
        friend node operator + (node a, const node &b) {
            mini(a.mn, b.mn);
            return a;
        }
    };

    node st[4 * maxn];

    void init() {
        for(int i = 0; i <= 4 * n; i++) st[i].mn = oo;
    }

    void update(int id, int l, int r, int pos, ll val) {
        if(pos < l || pos > r) return;
        if(l == r) {
            mini(st[id].mn, val);
            return;
        }
        int mid = l + r >> 1;
        update(id << 1, l, mid, pos, val);
        update(id << 1 | 1, mid + 1, r, pos, val);
        st[id] = st[id << 1] + st[id << 1 | 1];
    }

    node get(int id, int l, int r, int u, int v) {
        if(r < u || l > v) return {oo};
        if(u <= l && r <= v) return st[id];
        int mid = l + r >> 1;
        return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
    }

} st;

struct sub4 {
    ll dpl[maxn], dpr[maxn];
        
    int id(int x) {
        return lower_bound(all(val), x) - val.begin() + 1;
    }

    void solve() {
        for(int i = 0; i < n; i++) {
            val.pb(a[i].c);
        }

        sort(all(val));
        val.erase(unique(all(val)), val.end());

        st.init();
        for(int i = 0; i < n; i++) {
            dpl[i] = oo;
            if(a[i].l == 1) dpl[i] = a[i].cost;
            else {
                int l = id(a[i].l);
                int r = id(a[i].r + 1) - 1;
                if(l <= r) mini(dpl[i], st.get(1, 1, n, l, r).mn + a[i].cost);
            }
            st.update(1, 1, n, id(a[i].c), dpl[i]);
        }

        st.init();
        ll ans = oo;
        for(int i = 0; i < n; i++) {
            dpr[i] = oo;
            if(a[i].r == m) dpr[i] = a[i].cost;
            else {
                int l = id(a[i].l);
                int r = id(a[i].r + 1) - 1;
                if(l <= r) mini(dpr[i], st.get(1, 1, n, l, r).mn + a[i].cost);
            }
            st.update(1, 1, n, id(a[i].c), dpr[i]);
            if(dpl[i] != oo && dpr[i] != oo) {
                mini(ans, dpl[i] + dpr[i] - a[i].cost);
            }
        }

        cout<<(ans == oo ? -1:ans);
    }

} sub4;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>m;
    for(int i = 0; i < n; i++) cin>>a[i].l>>a[i].r>>a[i].c>>a[i].cost;
    // reverse(a, a + n);
    // for(int i = 0; i < n; i++) {
    //     val.pb(a[i].l);
    //     val.pb(a[i].r);
    // }

    // val.pb(1);
    // val.pb(m);
    // sort(all(val));
    // val.erase(unique(all(val)), val.end());

    // for(int i = 0; i < n; i++) {
    //     a[i].l = lower_bound(all(val), a[i].l) - val.begin();
    //     a[i].r = lower_bound(all(val), a[i].r) - val.begin();
    // }

    // if(n <= 200) {
    //     sub2.solve();
    //     return 0;
    // }

    // if(n <= 1000) {
    //     sub3.solve();
    //     return 0;
    // }

    sub4.solve();
    return 0;
}

Compilation message

pinball.cpp: In member function 'void ST::update(int, int, int, int, long long int)':
pinball.cpp:108:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  108 |         int mid = l + r >> 1;
      |                   ~~^~~
pinball.cpp: In member function 'ST::node ST::get(int, int, int, int, int)':
pinball.cpp:117:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  117 |         int mid = l + r >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4556 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4556 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4568 KB Output is correct
17 Correct 2 ms 4604 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4556 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4568 KB Output is correct
17 Correct 2 ms 4604 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 14 ms 4956 KB Output is correct
26 Correct 37 ms 7768 KB Output is correct
27 Correct 111 ms 9680 KB Output is correct
28 Correct 111 ms 13420 KB Output is correct
29 Correct 79 ms 9012 KB Output is correct
30 Correct 140 ms 13500 KB Output is correct
31 Correct 167 ms 13524 KB Output is correct
32 Correct 164 ms 13656 KB Output is correct
33 Correct 21 ms 5468 KB Output is correct
34 Correct 74 ms 8896 KB Output is correct
35 Correct 108 ms 13524 KB Output is correct
36 Correct 168 ms 13560 KB Output is correct
37 Correct 145 ms 13596 KB Output is correct
38 Correct 146 ms 13536 KB Output is correct