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>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=3e5+3;
int n,q,f[N],l[N],r[N];
ll d,c[N],ans[N];
vector<pair<int,int>> qr[N];
int find_set(int u) {
return (f[u]<0?u:f[u]=find_set(f[u]));
}
void unite(int u,int v) {
int a=find_set(u),b=find_set(v);
if (a==b) return;
if (f[a]>f[b]) swap(a,b);
f[a]+=f[b];
l[a]=min(l[a],l[b]);
r[a]=max(r[a],r[b]);
f[b]=a;
}
struct SegTree {
ll lazy[N*4],tr[N*4],len[N*4];
void push(int id) {
tr[id*2]+=len[id*2]*lazy[id];
lazy[id*2]+=lazy[id];
tr[id*2+1]+=len[id*2+1]*lazy[id];
lazy[id*2+1]+=lazy[id];
lazy[id]=0;
}
void preprocess(int id,int l,int r) {
len[id]=r-l+1;
if (l==r) return;
int mid=l+r>>1;
preprocess(id*2,l,mid);
preprocess(id*2+1,mid+1,r);
}
void update(int id,int l,int r,int u,int v,ll val) {
if (l>v || r<u) return;
if (l>=u && r<=v) {
tr[id]+=1LL*(r-l+1)*val;
lazy[id]+=val;
return;
}
push(id);
int mid=l+r>>1;
update(id*2,l,mid,u,v,val);
update(id*2+1,mid+1,r,u,v,val);
tr[id]=tr[id*2]+tr[id*2+1];
}
ll query(int id,int l,int r,int u,int v) {
if (l>v || r<u) return 0;
if (l>=u && r<=v) return tr[id];
push(id);
int mid=l+r>>1;
return query(id*2,l,mid,u,v)+query(id*2+1,mid+1,r,u,v);
}
};
SegTree st;
inline ll get(int idx) {
return c[idx]-st.query(1,1,n-1,idx,idx)*d;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> d;
For(i,1,n) cin >> c[i];
For(i,1,n) f[i]=-1,l[i]=r[i]=i;
cin >> q;
For(i,1,q) {
int l,r;
cin >> l >> r;
qr[r].pb({l,i});
}
st.preprocess(1,1,n-1);
For(i,1,n) {
int cur=i-1;
while(cur>0) {
if (get(cur)<=get(cur+1)) break;
ll sus=(get(cur)-get(cur+1))/d;
if (get(cur)-sus*d>get(cur+1)) sus++;
st.update(1,1,n-1,l[find_set(cur)],r[find_set(cur)],sus);
unite(cur,cur+1);
cur=l[find_set(cur)]-1;
}
for(auto el: qr[i])
if (get(el.ff)<0) ans[el.ss]=-1;
else ans[el.ss]=st.query(1,1,n-1,el.ff,i);
}
For(i,1,q) cout << ans[i] << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In member function 'void SegTree::preprocess(int, int, int)':
Main.cpp:58:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int mid=l+r>>1;
| ~^~
Main.cpp: In member function 'void SegTree::update(int, int, int, int, int, long long int)':
Main.cpp:70:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int mid=l+r>>1;
| ~^~
Main.cpp: In member function 'long long int SegTree::query(int, int, int, int, int)':
Main.cpp:79:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
79 | int mid=l+r>>1;
| ~^~
# | 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... |