#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<int,pii> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = 3e5+50;
const int SQRT = 610;
const int MAXA = 50;
const int MOD = 998244353;
const int LOG = 21;
const int INF = 1e16+100;
const int INF2 = 1e18;
const ld EPS = 1e-12;
int n, d, c[MAXN], ans[MAXN], val[MAXN], su[MAXN];
vector <pii> que[MAXN];
int QUE(int l, int r){
int tot = su[l]-su[r+1] - (r-l+1)*val[r+1];
return tot;
}
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>d;
for(int i=1; i<=n; i++){
cin >> c[i];
}
for(int i=n-1; i>=1; i--){
val[i] = val[i+1];
if(c[i] >= c[i+1]) val[i] += (c[i]-c[i+1]+d-1)/d;
else val[i] -= (c[i+1]-c[i])/d;
su[i] = su[i+1] + val[i];
// cout << i << ' ' << val[i] << ' ' << su[i] << " pp\n";
}
int q; cin>>q;
for(int i=1; i<=q; i++){
int l, r; cin>>l>>r;
if(l==r){ ans[i] = 0; continue; }
que[r].pb({l, i});
}
int las = 0;
for(int i=1; i<=n; i++){
if(i!=1 && c[i-1] > c[i]) las = i-1;
for(auto [x, id] : que[i]){
if(las<x){
ans[id] = 0; continue;
}
// cout << las << "las\n";
bool can = 1;
for(int j=las; j>=x; j--){
if(c[j] < d*(val[j]-val[las+1])) can = 0;
}
if(!can){
ans[id] = -1; continue;
}
ans[id] = QUE(x, las);
}
// int nw = 0; bool can = 1;
// for(int j=r; j>=l; j--){
// if(c[j] >= c[j+1]) nw += (c[j]-c[j+1]+d-1)/d;
// else nw -= (c[j+1]-c[j])/d;
// if(nw*d > c[j]) can = 0;
// nw = max(0ll, nw);
// ans[i] += nw;
// }
// if(!can) ans[i] = -1;
// if((val[l]-val[r+1])*d > c[l]){ ans[i] = -1; continue; }
// ans[i] = su[l]-su[r+1] - (r-l+1) * val[r+1];
// cout << su[l]-su[r+1] << " su\n";
}
for(int i=1; i<=q;i++) cout << ans[i] << " \n";
}
# | 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... |