This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 3e5+100, M = 1e5+10, K = 20, MX = 30;
int n, q, to[N][K];
ll D, C[N], B[N], suf[N], arr[N], rmq[N][K], pref[N], S[N], dp[N][K];
array<ll, 2> L[N];
ll get(int l, int r){
int k= log2(r-l+1);
return min(rmq[l][k], rmq[r-(1<<k)+1][k]);
}
void solve(){
cin >> n >> D;
for(int i = 1; i <= n; ++i) cin >> C[i];
B[1] = C[1] % D;
for(int i = 2; i <= n; ++i){
if(C[i] % D >= C[i-1] % D){
B[i] = B[i-1] + (C[i]%D)-(C[i-1]%D);
}else{
B[i] = B[i-1] + D + (C[i]%D)-(C[i-1]%D);
}
}
for(int i = 1; i <= n; ++i) arr[i] = rmq[i][0] = C[i] - B[i];
for(int j = 1; j < K ;++j){
for(int i = 1; i + (1<<j) <= n+1; ++i){
rmq[i][j] = min(rmq[i][j - 1], rmq[i+(1<<(j-1))][j-1]);
}
}
pref[0] = 0;
S[0] = 0;
C[0] = arr[0] = 0;
for(int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + B[i];
for(int i = 1; i <= n; ++i) S[i] = S[i - 1] + C[i];
deque<pair<ll, int>> Q;
L[1] = {0, arr[1]};
to[1][0] = L[1][0];
dp[1][0] = arr[1] * (1 - to[1][0]);
Q.pb({arr[1], 1});
for(int i = 2; i <= n; ++i){
while(!Q.empty() && Q.back().first > arr[i]) Q.pop_back();
if(Q.empty()) L[i] = {0, arr[i]};
else L[i] = {Q.back().second, arr[i]};
to[i][0] = L[i][0];
dp[i][0] = arr[i] * (i - to[i][0]);
Q.pb({arr[i], i});
}
for(int j = 1; j < K; ++j) for(int i = 1; i <= n; ++i){
dp[i][j] = dp[i][j - 1] + dp[to[i][j - 1]][j - 1];
to[i][j] = to[to[i][j - 1]][j - 1];
}
// for(int i = 1; i <= n; ++i) cout << B[i] << ' ';en;
// for(int i = 1; i <= n; ++i) cout << arr[i] << ' ';en;
// for(int i = 1; i <= n; ++i) cout << L[i][0] << ' ';en;
cin >> q;
for(int i = 1; i <= q; ++i){
int l, r; cin >> l >> r;
ll ans = 0;
ll mn = get(l, r);
ll R = B[l] - (C[l]%D);
if(mn + R < 0){
cout << -1 << '\n';
continue;
}
// cout << R << ' ';
ans = ( S[r] - S[l-1] - (pref[r] - pref[l-1] - (r-l+1)*R));
ans -= R*(r-l+1);
// cout << S[r] - S[l-1] << ' ' << pref[r]-pref[l-1] << '\n';
int p = r;
for(int j = K-1; j >= 0; --j){
if(to[p][j] >= l){
ans -= dp[p][j];
p = to[p][j];
}
}
ans -= (p-l+1)*arr[p];
// while(p > l-1){
// if(L[p][0] < l){
// ans -= (arr[p] + R) * (p - l + 1);
// }else{
// ans -= (arr[p] + R) * (p - L[p][0]);
// }
// p = L[p][0];
// }
if(ans % D != 0) cout << -1 << '\n';
else
cout << ans / D << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:104:15: warning: unused variable 'aa' [-Wunused-variable]
104 | int tt = 1, aa;
| ^~
# | 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... |