// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 3e5 + 7;
int n, d, q, a[N], ans[N];
vector<pair<int, int>> ev[N];
struct SEGTREE {
vector<int> st, lazy;
int n;
SEGTREE() {}
SEGTREE(int n) : n(n) {
st.resize(n * 4 + 4, 0);
lazy.resize(n * 4 + 4, 0);
}
void down(int i, int l, int r) {
if(lazy[i]) {
int x = lazy[i];
lazy[i] = 0;
int m = (l + r) >> 1;
lazy[i * 2] += x;
lazy[i * 2 + 1] += x;
st[i * 2] += (m - l + 1) * x;
st[i * 2 + 1] += (r - m) * x;
}
}
void update(int i, int l, int r, int u, int v, int x) {
if(l > v or r < u) return;
if(u <= l and r <= v) {
st[i] += (r - l + 1) * x;
lazy[i] += x;
return;
}
int m = (l + r) >> 1;
down(i, l, r);
update(i * 2, l, m, u, v, x);
update(i * 2 + 1, m + 1, r, u, v, x);
st[i] = st[i * 2] + st[i * 2 + 1];
}
int get(int i, int l, int r, int u, int v) {
if(l > v or r < u) return 0;
if(u <= l and r <= v) return st[i];
down(i, l, r);
int m = (l + r) >> 1;
return get(i * 2, l, m, u, v) + get(i * 2 + 1, m + 1, r, u, v);
}
void update(int l, int r, int x) {
update(1, 1, n, l, r, x);
}
int get(int l, int r) {
return get(1, 1, n, l, r);
}
} st;
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> d;
for(int i = 1; i <= n; i++) cin >> a[i];
cin >> q;
for(int i = 1; i <= q; i++) {
int l, r; cin >> l >> r;
ev[r].pb({l, i});
}
vector<int> s;
st = SEGTREE(n);
for(int r = 1; r <= n; r++) {
int cur = r;
while(sz(s)) {
int lst = s.back();
int pre = a[cur - 1] - st.get(cur - 1, cur - 1) * d;
int tar = a[cur] - st.get(cur, cur) * d;
if(pre <= tar) break;
int diff = (pre - tar - 1) / d + 1;
st.update(lst, cur - 1, diff);
cur = lst;
s.pop_back();
}
s.pb(cur);
for(auto [l, id] : ev[r]) {
if(a[l] - st.get(l, l) * d < 0) ans[id] = -1;
else ans[id] = st.get(l, r);
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:71:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:72:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |