#ifdef __AVX2__
#pragma GCC target "avx2"
#endif
#pragma GCC optimize "O3"
#pragma GCC optimize "unroll-loops"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define elif else if
#define all(l) begin(l),end(l)
#define rall(l) rbegin(l),rend(l)
#define append push_back
#define print(l) for(auto i:l) cout<<i<<' '; cout<<endl;
#define pprint(a,b) cout<<a<<' '<<b<<endl;
#define inp(l) for(auto &i:l) cin>>i;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define pai make_pair
#define endl "\n"
#define pii pair<int,int>
#define fi first
#define se second
#define vec vector
// const int mod=998244353;
const int mod1=998244353;
const int mod=1e9+7;
const int N=1e3+5;
int c[N];
int iter=1,itera=1;
void solve(){
int n,d;
cin>>n>>d;
for(int i=1;i<=n;i++) cin>>c[i];
int q;
cin>>q;
for(int i=1;i<=q;i++){
int l,r;
cin>>l>>r;
vec<int>op(n+1,0);
op[l]=c[l]%d;
bool f=1;
for(int j=l+1;j<=r;j++){
int x=((c[j]-op[j-1])%d+d)%d;
op[j]=op[j-1]+x;
}
for(int j=l;j<=r;j++) if(op[j]>c[j]) f=0;
if(!f){
cout<<"-1\n";
continue;
}
vec<int>sm(n+2,1e12);
for(int j=r;j>=l;j--) sm[j]=min(sm[j+1],c[j]-op[j]);
int ans=0;
for(int j=l;j<=r;j++) ans+=(c[j]-op[j]-sm[j])/d;
cout<<ans<<endl;
}
}
signed main(){
// freopen("","r",stdin);
// freopen("","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// cin>>itera;
for(iter=1;iter<=itera;iter++) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |