#include <bits/stdc++.h>
using namespace std;
#define POPCOUNT(n) (__builtin_popcountll((n)))
#define CLZ(n) (__builtin_clzll((n)))
#define CTZ(n) (__builtin_ctzll((n)))
#define LOG(n) (63 - __builtin_clzll((n)))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define MASK(i) (1ll << (i))
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
#define Int __int128
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
template <class T1, class T2> bool maximize(T1 &x, T2 y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T1, class T2> bool minimize(T1 &x, T2 y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <class T> void remove_duplicate(vector<T> &ve) {
sort (ve.begin(), ve.end());
ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
long long random(long long l, long long r) {
return uniform_int_distribution<long long>(l, r)(rng);
}
unsigned long long random(unsigned long long l, unsigned long long r) {
return uniform_int_distribution<unsigned long long>(l, r)(rng);
}
template <class T> T random(T r) {
return rng() % r;
}
const int N = 3e5 + 5, LG = 19;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const long long INF = 1e18;
int n, q;
ll D;
int jump[N][LG];
ll c[N], f[N], suf[N], pref[N], pref_cost[N], cost[N][LG];
ll get_cost(int l, int r) {
// c(i) - (suf[i] - suf[r + 1]) * D >= 0
// c(i) - suf[i] * D + suf[r + 1] * D >= 0
// c(i) - suf[i] * D >= -suf[r + 1] * D
return (pref_cost[r] - pref_cost[l - 1]) - (pref[r] - pref[l - 1]) * (l - 1);
}
ll solve(int l, int r) {
int p = r - 1; ll ans = 0;
for (int i = LG - 1; i >= 0; --i)
if (jump[p][i] >= l) {
ans += cost[p][i];
p = jump[p][i];
}
ans += get_cost(l, p);
return ans;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> D;
for (int i = 1; i <= n; ++i) cin >> c[i];
for (int i = 1; i < n; ++i) {
if (c[i] > c[i + 1]) f[i] = (c[i] - c[i + 1] + D - 1) / D;
else f[i] = -((c[i + 1] - c[i]) / D);
}
suf[n] = 0;
for (int i = n - 1; i > 0; --i) suf[i] = suf[i + 1] + f[i];
for (int i = 1; i < n; ++i) {
pref[i] = pref[i - 1] + f[i];
pref_cost[i] = pref_cost[i - 1] + f[i] * i;
}
stack<int> st; st.emplace(1);
for (int i = 1; i < n; ++i) {
while (!st.empty() && suf[st.top()] > suf[i + 1]) st.pop();
if (st.empty()) jump[i][0] = -1;
else jump[i][0] = st.top() - 1;
st.emplace(i + 1);
}
// for (int i = 1; i < n; ++i) {
// cerr << suf[i] << ' ';
// }
// cerr << '\n';
for (int i = 1; i < n; ++i) {
cerr << jump[i][0] << ' ';
}
cerr << '\n';
for (int i = 1; i < n; ++i) {
cost[i][0] = get_cost(jump[i][0] + 2, i);
if (jump[i][0] == -1) jump[i][0] = 0;
}
for (int i = 1; i < LG; ++i) for (int j = 1; j < n; ++j) {
jump[j][i] = jump[jump[j][i - 1]][i - 1];
cost[j][i] = cost[j][i - 1] + cost[jump[j][i - 1]][i - 1];
}
cin >> q;
while (q--) {
int l, r; cin >> l >> r;
ll k = 0, ans = 0;
for (int i = r - 1; i >= l; --i) {
// c(i) - k * D <= pre
// k >= ceil((c(i) - pre) / D)
if (c[i] > c[i + 1]) k += f[i];
else k = max(0ll, k + f[i]);
if (c[i] - k * D < 0) {
ans = -1;
break;
}
ans += k;
}
cout << ans << '\n';
// if (ans == -1) cout << "-1\n";
// else cout << solve(l, r) << '\n';
}
return 0;
}
| # | 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... |