Submission #1000580

# Submission time Handle Problem Language Result Execution time Memory
1000580 2024-06-17T20:52:36 Z PurpleCrayon Fish 3 (JOI24_fish3) C++17
100 / 100
456 ms 118456 KB
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 3e5+10, MOD = 1e9+7;
const ll INF = 1e18+10;
const int L = 19;

pair<int, ll> anc[N][L];

ll ceil_div(ll x, ll y) {
    return (x + y - 1) / y;
}

void solve() {
    int n; ll k; cin >> n >> k;
    vector<ll> a(n); for (auto& x : a) cin >> x;
    vector<ll> diff(n-1);
    for (int i = 0; i < n-1; i++) {
        if (a[i] <= a[i+1])
            diff[i] = max(0LL, (a[i+1] - a[i]) / k);
        else
            diff[i] = -ceil_div(a[i] - a[i+1], k);
    }

    vector<ll> ps_diff = diff;
    for (int i = 1; i < n-1; i++) {
        ps_diff[i] += ps_diff[i-1];
    }

    vector<int> prv(n-1, -2); // previous thing less than it

    vector<int> stk;
    for (int i = 0; i < n-1; i++) {
        while (sz(stk) && ps_diff[stk.back()] >= ps_diff[i]) stk.pop_back();
        if (sz(stk)) prv[i] = stk.back();
        else prv[i] = ps_diff[i] > 0 ? -1 : -2;
        stk.push_back(i);
    }

    vector<ll> suf1 = diff; suf1.push_back(0);
    for (int i = sz(diff)-1; i >= 0; i--) {
        suf1[i] += suf1[i+1];
    }

    vector<ll> suf2 = suf1;
    for (int i = sz(diff)-1; i >= 0; i--) {
        suf2[i] += suf2[i+1];
    }

    auto qry = [&](int l, int r) {
        if (l > r) return 0LL;
        return -(suf2[l] - suf2[r+1] - suf1[r+1] * (r - l + 1));
    };

    vector<ll> store(n-1);
    for (int i = 0; i < n-1; i++) {
        store[i] = qry(prv[i] + 2, i);
    }

    for (int i = 0; i < n-1; i++) {
        anc[i][0] = {prv[i], store[i]};
    }

    for (int k = 1; k < L; k++) {
        for (int i = 0; i < n; i++) {
            int m = anc[i][k-1].first;
            if (m >= 0) {
                anc[i][k] = {anc[m][k-1].first, anc[i][k-1].second + anc[m][k-1].second};
            } else {
                anc[i][k] = {-2, -2};
            }
        }
    }

    int q; cin >> q;
    while (q--) {
        int l, r; cin >> l >> r, --l, --r;
        if (l == r) {
            ll ans = 0;
            ll p = 0;
            for (int i = r-1; i >= l; i--) {
                if (p >= diff[i]) {
                    p -= diff[i];
                } else {
                    p = 0;
                }

                ans += p;
            }

            if (a[l] - k * p >= 0)
                cout << ans << '\n';
            else
                cout << -1 << '\n';

            continue;
        }

        ll ans = 0;
        int c = r-1;
        for (int i = L-1; i >= 0; i--) {
            while (c >= 0 && anc[c][i].first + 1 >= l) {
                ans += anc[c][i].second;
                c = anc[c][i].first;
            }
        }

        /*
        while (c >= 0 && prv[c] + 1 >= l) { // slow
            ans += store[c]; 
            c = prv[c];
        }
        */

        ll p = 0;
        if (l <= c) {
            p -= (suf1[l] - suf1[c+1]);
        }
        ans += qry(l, c);

        if (a[l] - k * p >= 0)
            cout << ans << '\n';
        else
            cout << -1 << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2736 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 1 ms 2828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 108076 KB Output is correct
2 Correct 178 ms 106536 KB Output is correct
3 Correct 48 ms 3664 KB Output is correct
4 Correct 152 ms 106280 KB Output is correct
5 Correct 151 ms 107308 KB Output is correct
6 Correct 155 ms 106280 KB Output is correct
7 Correct 145 ms 105688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 7308 KB Output is correct
2 Correct 179 ms 110892 KB Output is correct
3 Correct 183 ms 110888 KB Output is correct
4 Correct 194 ms 110636 KB Output is correct
5 Correct 200 ms 110372 KB Output is correct
6 Correct 175 ms 107912 KB Output is correct
7 Correct 168 ms 108060 KB Output is correct
8 Correct 177 ms 106948 KB Output is correct
9 Correct 184 ms 106976 KB Output is correct
10 Correct 410 ms 110240 KB Output is correct
11 Correct 456 ms 115124 KB Output is correct
12 Correct 329 ms 117164 KB Output is correct
13 Correct 332 ms 117528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 89020 KB Output is correct
2 Correct 155 ms 107048 KB Output is correct
3 Correct 74 ms 26096 KB Output is correct
4 Correct 173 ms 107848 KB Output is correct
5 Correct 161 ms 100924 KB Output is correct
6 Correct 206 ms 111360 KB Output is correct
7 Correct 151 ms 90904 KB Output is correct
8 Correct 172 ms 110844 KB Output is correct
9 Correct 152 ms 107048 KB Output is correct
10 Correct 156 ms 107052 KB Output is correct
11 Correct 178 ms 107468 KB Output is correct
12 Correct 163 ms 107992 KB Output is correct
13 Correct 185 ms 111660 KB Output is correct
14 Correct 170 ms 106568 KB Output is correct
15 Correct 171 ms 109864 KB Output is correct
16 Correct 163 ms 106940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2736 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 1 ms 2828 KB Output is correct
14 Correct 155 ms 108076 KB Output is correct
15 Correct 178 ms 106536 KB Output is correct
16 Correct 48 ms 3664 KB Output is correct
17 Correct 152 ms 106280 KB Output is correct
18 Correct 151 ms 107308 KB Output is correct
19 Correct 155 ms 106280 KB Output is correct
20 Correct 145 ms 105688 KB Output is correct
21 Correct 62 ms 7308 KB Output is correct
22 Correct 179 ms 110892 KB Output is correct
23 Correct 183 ms 110888 KB Output is correct
24 Correct 194 ms 110636 KB Output is correct
25 Correct 200 ms 110372 KB Output is correct
26 Correct 175 ms 107912 KB Output is correct
27 Correct 168 ms 108060 KB Output is correct
28 Correct 177 ms 106948 KB Output is correct
29 Correct 184 ms 106976 KB Output is correct
30 Correct 410 ms 110240 KB Output is correct
31 Correct 456 ms 115124 KB Output is correct
32 Correct 329 ms 117164 KB Output is correct
33 Correct 332 ms 117528 KB Output is correct
34 Correct 140 ms 89020 KB Output is correct
35 Correct 155 ms 107048 KB Output is correct
36 Correct 74 ms 26096 KB Output is correct
37 Correct 173 ms 107848 KB Output is correct
38 Correct 161 ms 100924 KB Output is correct
39 Correct 206 ms 111360 KB Output is correct
40 Correct 151 ms 90904 KB Output is correct
41 Correct 172 ms 110844 KB Output is correct
42 Correct 152 ms 107048 KB Output is correct
43 Correct 156 ms 107052 KB Output is correct
44 Correct 178 ms 107468 KB Output is correct
45 Correct 163 ms 107992 KB Output is correct
46 Correct 185 ms 111660 KB Output is correct
47 Correct 170 ms 106568 KB Output is correct
48 Correct 171 ms 109864 KB Output is correct
49 Correct 163 ms 106940 KB Output is correct
50 Correct 189 ms 117824 KB Output is correct
51 Correct 200 ms 113196 KB Output is correct
52 Correct 178 ms 115024 KB Output is correct
53 Correct 410 ms 115052 KB Output is correct
54 Correct 333 ms 118456 KB Output is correct
55 Correct 187 ms 118316 KB Output is correct
56 Correct 183 ms 114000 KB Output is correct
57 Correct 155 ms 110880 KB Output is correct
58 Correct 174 ms 111520 KB Output is correct
59 Correct 186 ms 116520 KB Output is correct
60 Correct 179 ms 114728 KB Output is correct
61 Correct 412 ms 114500 KB Output is correct
62 Correct 177 ms 113964 KB Output is correct
63 Correct 319 ms 117416 KB Output is correct
64 Correct 175 ms 114728 KB Output is correct