Submission #1239314

#TimeUsernameProblemLanguageResultExecution timeMemory
1239314minhpkFish 3 (JOI24_fish3)C++20
100 / 100
173 ms49540 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 5001001001001001001;
vector<pair<int,int>> q[1000005];
int z[1000005], t[1000005], prefix[1000005], res[1000005];

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

    int a, d;
    cin >> a >> d;

    for (int i = 0; i < a; i++) cin >> z[i];

    int c;
    cin >> c;
    for (int i = 0; i < c; i++) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        q[y].emplace_back(x, i);
    }

    for (int i = a - 2; i >= 0; i--) {
        if (z[i] >= z[i + 1])
            t[i] = t[i + 1] + (z[i] - z[i + 1] + d - 1) / d;
        else
            t[i] = t[i + 1] - (z[i + 1] - z[i]) / d;
    }

    for (int i = 0; i < a; i++) {
        prefix[i + 1] = prefix[i] + t[i];
    }

    vector<pair<int,int>> st;
    st.emplace_back(-1, 0);

    for (int i = 0; i < a; i++) {
        while (st.size() > 1 && t[st.back().first] >= t[i]) {
            st.pop_back();
        }

        st.emplace_back(i, st.back().second + (i - st.back().first) * t[i]);

        for (auto p : q[i]) {
            int x = p.first, idx = p.second;
            int pos = lower_bound(st.begin(), st.end(), make_pair(x, -INF)) - st.begin();
            int pre = st.back().second - st[pos - 1].second - t[st[pos].first] * (x - 1 - st[pos - 1].first);
            res[idx] = prefix[i + 1] - prefix[x] - pre;
            if (z[x] < d * (t[x] - t[st[pos].first])) {
                res[idx] = -1;
            }
        }
    }

    for (int i = 0; i < c; i++) {
        cout << res[i] << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...