# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1128218 | jj_master | Fish 3 (JOI24_fish3) | C++20 | 0 ms | 0 KiB |
// JOI 2023 - 2024
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,d;
cin >> n >> d;
vector<int> arr(n+1);
for(int i=1;i<=n;i++)cin>>arr[i];
int q;
cin >> q;
for(int i=1;i<=q;i++){
int l,r;cin>>l>>r;
vector<int> curr(n+1);
for(int x=l;x<=r;x++){
curr[x] = curr[x-1]+((d+(arr[x]%d)-(curr[x-1]%d))%d);
}
int uses = 1e15;
int ans = 0;
bool works = true;
for(int x=r;x>=l;x--){
int req = (arr[x]-curr[x])/d;
if(req<0)works=false;
uses = min(uses,req);
ans+=req-uses;
}
if(works)cout<<ans<<'\n';
else cout << "-1" '\n';
}
}