제출 #1335601

#제출 시각아이디문제언어결과실행 시간메모리
1335601anteknneFish 3 (JOI24_fish3)C++20
100 / 100
178 ms32836 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false

const int MAXN = 300 * 1000 + 17;
ll a[MAXN];
vector<pii> zap[MAXN];
ll twyn[MAXN];
ll ile[MAXN];
ll sprefile[MAXN];
ll spref[MAXN];
ll aktspref[MAXN];
vector<pii> s;

int main () {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;
    ll d;

    cin >> n >> d;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }

    int q, l, r;
    cin >> q;
    for (int i = 0; i < q; i ++) {
        cin >> l >> r;
        zap[r].pb({l, i});
    }

    for (int i = 1; i <= n; i ++) {
        spref[i] = spref[i - 1] + a[i];
        ile[i] = ile[i - 1] + ((((a[i] - a[i - 1]) % d) + d) % d);
        sprefile[i] = sprefile[i - 1] + ile[i];
    }

    for (int i = 1; i <= n; i ++) {
        int ind = i;
        while (!s.empty() && a[s.back().nd] - ile[s.back().nd] >= a[i] - ile[i]) {
            ind = s.back().st;
            s.pop_back();
        }
        s.pb({ind, i});
        ll dl = i - ind + 1;
        aktspref[i] = aktspref[ind - 1] + dl * a[i] - (ile[i] * dl - (sprefile[i] - sprefile[ind - 1]));
        for (auto x : zap[i]) {
            int p = 0, k = int(s.size()), w;
            while (p <= k) {
                int sr = (p + k)/ 2;
                if (s[sr].nd >= x.st) {
                    k = sr - 1;
                    w = sr;
                } else {
                    p = sr + 1;
                }
            }

            ll w2 = spref[i] - spref[x.st - 1];
            w2 -= aktspref[i] - aktspref[s[w].nd];
            ll dl = s[w].nd - x.st + 1;
            w2 -= dl * a[s[w].nd] - (ile[s[w].nd] * dl - (sprefile[s[w].nd] - sprefile[x.st - 1]));
            w2 /= d;
            if (a[s[w].nd] - (ile[s[w].nd] - ile[x.st]) < 0) {
                w2 = -1;
            }
            twyn[x.nd] = w2;
        }
    }

    for (int i = 0; i < q; i ++) {
        cout << twyn[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...