Submission #958984

# Submission time Handle Problem Language Result Execution time Memory
958984 2024-04-07T10:51:27 Z typ_ik Pinball (JOI14_pinball) C++17
100 / 100
499 ms 42704 KB
#include <bits/stdc++.h>

#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define watch(x) cout << (#x) << " : " << x << '\n'
#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 300100;
int l[N], r[N], c[N], d[N];

ll pref[N], suf[N];
const ll INF = 1e16 + 128;

ll mn[2][4 * N];

ll get_min(bool where, int l, int r, int v, int tl, int tr) {
    if (tl > r || tr < l)
        return INF;
    if (l <= tl && tr <= r)
        return mn[where][v];
    int tm = (tl + tr) >> 1;
    return min(get_min(where, l, r, v << 1, tl, tm),
               get_min(where, l, r, v << 1 | 1, tm + 1, tr));
}

void update(bool where, int pos, ll val, int v, int tl, int tr) {
    if (tl > pos || tr < pos)
        return;
    if (tl == tr) {
        mn[where][v] = min(mn[where][v], val);
        return;
    }
    int tm = (tl + tr) >> 1;
    update(where, pos, val, v << 1, tl, tm);
    update(where, pos, val, v << 1 | 1, tm + 1, tr);
    mn[where][v] = min(mn[where][v << 1], mn[where][v << 1 | 1]);
}

void build(bool where, int v, int tl, int tr) {
    if (tl == tr) {
        mn[where][v] = INF;
        return;
    }
    int tm = (tl + tr) >> 1;
    build(where, v << 1, tl, tm);
    build(where, v << 1 | 1, tm + 1, tr);
    mn[where][v] = INF;
}

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

    for (int i = 0; i < n; i++)
        cin >> l[i] >> r[i] >> c[i] >> d[i];

    vector <int> als;
    als.push_back(1);
    als.push_back(m);
    for (int i = 0; i < n; i++) {
        als.push_back(l[i]);
        als.push_back(r[i]);
        als.push_back(c[i]);
    }

    sort(all(als));
    als.resize(unique(all(als)) - als.begin());

    int sz = (int)als.size();
    map <int, int> comp;
    for (int i = 0; i < sz; i++)
        comp[als[i]] = i;

    build(0, 1, 0, sz - 1);
    build(1, 1, 0, sz - 1);

    ll ans = INF;
    for (int i = 0; i < n; i++) {
        ll lx = INF, rx = INF;
        if (l[i] == 1) lx = 0;
        else lx = min(lx, get_min(0, comp[l[i]], comp[r[i]], 1, 0, sz - 1));
        if (r[i] == m) rx = 0;
        else rx = min(rx, get_min(1, comp[l[i]], comp[r[i]], 1, 0, sz - 1));

        ans = min(ans, lx + rx + d[i]);

        if (l[i] == 1)
            update(0, comp[c[i]], 1ll * d[i], 1, 0, sz - 1);
        else
            update(0, comp[c[i]], get_min(0, comp[l[i]], comp[r[i]], 1, 0, sz - 1) + d[i], 1, 0, sz - 1);

        if (r[i] == m)
            update(1, comp[c[i]], 1ll * d[i], 1, 0, sz - 1);
        else
            update(1, comp[c[i]], get_min(1, comp[l[i]], comp[r[i]], 1, 0, sz - 1) + d[i], 1, 0, sz - 1);
    }

    if (ans >= INF)
        ans = -1;

    cout << ans << '\n';
}

main() {
    boost;
    solve();
    return 0;
}

Compilation message

pinball.cpp:107:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  107 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6612 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 3 ms 6744 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 4 ms 6748 KB Output is correct
20 Correct 3 ms 6748 KB Output is correct
21 Correct 3 ms 6744 KB Output is correct
22 Correct 3 ms 6748 KB Output is correct
23 Correct 4 ms 6748 KB Output is correct
24 Correct 4 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6612 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 3 ms 6744 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 4 ms 6748 KB Output is correct
20 Correct 3 ms 6748 KB Output is correct
21 Correct 3 ms 6744 KB Output is correct
22 Correct 3 ms 6748 KB Output is correct
23 Correct 4 ms 6748 KB Output is correct
24 Correct 4 ms 6748 KB Output is correct
25 Correct 31 ms 10076 KB Output is correct
26 Correct 84 ms 12500 KB Output is correct
27 Correct 255 ms 19152 KB Output is correct
28 Correct 186 ms 12236 KB Output is correct
29 Correct 181 ms 17872 KB Output is correct
30 Correct 266 ms 15048 KB Output is correct
31 Correct 402 ms 28100 KB Output is correct
32 Correct 396 ms 21960 KB Output is correct
33 Correct 50 ms 12336 KB Output is correct
34 Correct 213 ms 24524 KB Output is correct
35 Correct 312 ms 42464 KB Output is correct
36 Correct 499 ms 42704 KB Output is correct
37 Correct 448 ms 42696 KB Output is correct
38 Correct 408 ms 42696 KB Output is correct