#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
typedef long long ll;
template<class T>void minimize(T& a, T b){
if(a > b){
a = b;
}
}
const int lim = 3e5 + 5;
const ll INF = 1e18;
int n, q;
ll d, c[lim];
namespace sub1{
void solve(){
for(int _ = 0; _ < q; _++){
int l, r;
cin >> l >> r;
ll mn = INF, ans = 0;
for(int i = r; i >= l; i--){
minimize(mn, c[i]);
if((c[i] - mn) % d != 0 && (mn -= d - (c[i] - mn) % d) < 0){
ans = -1;
break;
}
ans += (c[i] - mn) / d;
}
cout << ans << "\n";
}
}
}
namespace sub2{
void solve(){
vector<int>left(n + 1), f(n + 1);
left[0] = f[0] = 0;
for(int i = 1; i <= n; i++){
left[i] = (c[i] == 0 ? i : left[i - 1]);
f[i] = f[i - 1] + c[i];
}
for(int _ = 0; _ < q; _++){
int l, r;
cin >> l >> r;
if(left[r] <= l){
cout << "0\n";
continue;
}
if(d == 1){
cout << f[left[r]] - f[l - 1] << "\n";
}
else{
cout << (f[left[r]] - f[l - 1] == 0 ? 0 : -1) << "\n";
}
}
}
}
namespace sub3{
void solve(){
vector<vector<pair<int, int>>>query(n + 1);
vector<ll>f(n + 1);
for(int i = f[0] = 0; i < q; i++){
int l, r;
cin >> l >> r;
query[r].emplace_back(l, i);
}
vector<ll>st(n << 2, 0), lazy(n << 2, -1);
auto push_down = [&] (int id, int l, int r, int m){
if(lazy[id] != -1){
lazy[id << 1] = lazy[id << 1 | 1] = lazy[id];
st[id << 1] = lazy[id] * ll(m - l + 1);
st[id << 1 | 1] = lazy[id] * ll(r - m);
lazy[id] = -1;
}
};
function<void(int, int, int, int, int, ll)>update;
update = [&] (int id, int l, int r, int u, int v, ll x){
if(l > v || r < u){
return;
}
if(u <= l && v >= r){
st[id] = (lazy[id] = x) * ll(r - l + 1);
return;
}
int m = (l + r) >> 1;
push_down(id, l, r, m);
update(id << 1, l, m, u, v, x);
update(id << 1 | 1, m + 1, r, u, v, x);
st[id] = st[id << 1] + st[id << 1 | 1];
};
function<ll(int, int, int, int, int)>get;
get = [&] (int id, int l, int r, int u, int v){
if(l > v || r < u){
return 0LL;
}
if(u <= l && v >= r){
return st[id];
}
int m = (l + r) >> 1;
push_down(id, l, r, m);
return get(id << 1, l, m, u, v) + get(id << 1 | 1, m + 1, r, u, v);
};
stack<int>stk;
vector<ll>ans(q);
for(int i = 1; i <= n; stk.push(i++)){
f[i] = f[i - 1] + c[i];
while(!stk.empty() && c[stk.top()] >= c[i]){
stk.pop();
}
update(1, 1, n, stk.empty() ? 1 : stk.top() + 1, i, c[i]);
for(auto& [l, I] : query[i]){
ans[I] = f[i] - f[l - 1] - get(1, 1, n, l, i);
}
}
for(ll& x : ans){
cout << x << "\n";
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> d;
for(int i = 1; i <= n; i++){
cin >> c[i];
}
cin >> q;
if(max(n, q) <= 3000){
sub1::solve();
}
else if(*max_element(c + 1, c + n + 1) <= 1){
sub2::solve();
}
else if(d == 1){
sub3::solve();
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:121:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |