This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=3e5+5;
ll n, d, c[nx], q, l[nx], r[nx], t[nx], qs[nx], res[nx], sm;
stack<pair<ll, ll>> st;
vector<ll> qrs[nx];
struct segtree
{
ll d[4*nx], lz[4*nx];
void pushlz(int l, int r, int i)
{
d[i]+=lz[i]*(r-l+1);
if (l==r) return lz[i]=0, void();
lz[2*i]+=lz[i];
lz[2*i+1]+=lz[i];
lz[i]=0;
}
void update(int l, int r, int i, int ql, int qr, ll vl)
{
pushlz(l, r, i);
if (qr<l||r<ql) return;
if (ql<=l&&r<=qr) return lz[i]+=vl, pushlz(l, r, i), void();
int md=(l+r)/2;
update(l, md ,2*i, ql, qr, vl);
update(md+1, r ,2*i+1, ql, qr, vl);
d[i]=d[2*i]+d[2*i+1];
}
ll query(int l, int r, int i, int ql, int qr)
{
pushlz(l, r, i);
if (qr<l||r<ql) return 0;
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return query(l, md, 2*i, ql, qr)+query(md+1, r, 2*i+1, ql, qr);
}
void show(int l, int r, int i)
{
pushlz(l, r, i);
if (l==r) cout<<l<<' '<<r<<' '<<d[i]<<'\n';
if (l==r) return;
int md=(l+r)/2;
show(l, md ,2*i);
show(md+1, r, 2*i+1);
}
} s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
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]) t[i]=(c[i]-c[i+1])/d+((c[i]-c[i+1])%d!=0);
//for (int i=1; i<n; i++) cout<<i<<' '<<t[i]<<'\n';
cin>>q;
for (int i=1; i<=q; i++) cin>>l[i]>>r[i], qrs[r[i]].push_back(i);
for (int i=1; i<=n; i++)
{
//cout<<"debug "<<i<<'\n';
//s.show(1, n, 1);
//cout<<'\n';
for (auto x:qrs[i])
{
if (s.query(1, n, 1, l[x], l[x])*d>c[l[x]]) res[x]=-1;
else res[x]=s.query(1, n, 1, l[x], r[x]-1);
}
s.update(1, n, 1, 1, i, t[i]);
}
for (int i=1; i<=q; i++) cout<<res[i]<<'\n';
}
/*
5 1
5 4 3 2 1
3
1 2
2 4
1 5
5 1
3 1 4 1 5
3
1 5
2 4
3 5
5 5
1 2 3 4 5
3
1 2
2 3
1 5
3 1
1 2 1
1
1 3
*/
# | 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... |