#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp(a,b) make_pair(a, b)
#define gcd(a,b) (__gcd(a,b))
#define sz(x) (int)(x.size())
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define db(val) cerr << "["#val" = "<<(val)<<"] "
#define all(x) (x).begin(), (x).end()
#define file(name) if(ifstream(name".inp")){freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
typedef pair<int,int> pii;
const int mod = 1e9 + 7;
const int N = 3e5+5;
mt19937_64 rng(time(0));
template<class T>
bool maximize(T &res, T val) {
return res < val ? res = val,true : false;
}
template <class T>
bool minimize(T &res, T val) {
return res > val ? res = val,true : false;
}
long long power(long long a, long long b) {
long long res = 1;
a %= mod;
while(b) {
if(b&1) res = res*a%mod;
a = a*a%mod;
b /= 2;
}
return res;
}
template<class Info, class Tag>
struct LazySegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
vector<int> siz;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
siz.assign(3 << __lg(n), 0);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
siz[p] = r - l;
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
Tag temp = v;
temp.add *= siz[p];
info[p].apply(temp);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
long long add = 0;
void apply(Tag t) {
add += t.add;
}
};
struct Info {
long long val = 0;
void apply(Tag t) {
val += t.add;
}
};
Info operator+(Info a, Info b) {
return {a.val + b.val};
}
int n,d;
int q;
vector<pii> queries[N];
int main() {
fast_cin();
cin >> n >> d;
vector<long long> c(n);
vector<long long> f(n);
for(int i = 0; i < n; ++i) {
cin >> c[i];
f[i] = c[i];
if(i) f[i] += f[i - 1];
}
LazySegmentTree<Info, Tag> T(n + 10);
for(int i = 0; i < n; ++i) {
Info x;
x.val = c[i];
T.modify(i, x);
}
cin >> q;
for(int i = 0; i < q; ++i) {
int l,r;
cin >> l >> r;
--l;
--r;
queries[r].push_back({l, i});
}
vector<int> res(q, -1);
deque<int> lst({-1});
for(int i = 0; i < n; ++i) {
while(lst.size() > 1) {
int pos = lst.back();
auto val0 = T.rangeQuery(pos, pos + 1).val;
auto val1 = T.rangeQuery(pos + 1, pos + 2).val;
if(val0 > val1) {
if(val0 % d <= val1) {
int k = (val0 - val1 + d - 1)/d;
lst.pop_back();
int u = lst.back() + 1;
Tag E;
E.add = -k*d;
T.rangeApply(u, pos + 1, E);
if(T.rangeQuery(u, u + 1).val < 0) {
lst.clear();
while(T.rangeQuery(u + 1, u + 2).val < 0) {
u++;
}
lst.push_back(u);
}
}
else {
lst.clear();
lst.push_back(pos);
}
}
else break;
}
lst.push_back(i);
for(auto [l, id] : queries[i]) {
if(l <= lst[0]) continue;
res[id] = (f[i] - (l ? f[l - 1] : 0) - T.rangeQuery(l, i + 1).val);
assert(res[id] % d == 0);
res[id] /= d;
}
}
for(int i = 0; i < q; ++i) {
cout << res[i] << "\n";
}
#ifndef LOCAL
cerr << "\nTime elapsed: " << 1.0 * (double)clock() / CLOCKS_PER_SEC << " s.\n ";
#endif
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... |