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 int long long
#define F first
#define S second
using namespace std;
const int N = 3e5 + 10;
int n , a[N] , D , ans[N] , q , s[N];
vector <pair<int , int>> qu[N];
struct SEG{
int a[N << 2] , b[N << 2];
void Add(int l , int r , int A , int B , int node = 1 , int nl = 1 , int nr = n + 1)
{
if(r <= nl || nr <= l)
return;
if(l <= nl && nr <= r)
{
a[node] += A;
b[node] += B;
return;
}
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
Add(l , r , A , B , lc , nl , mid);
Add(l , r , A , B , rc , mid , nr);
}
int Get(int id , int node = 1 , int nl = 1 , int nr = n + 1)
{
int res = a[node] * id + b[node];
if(nr - nl == 1)
return res;
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
if(id < mid)
res += Get(id , lc , nl , mid);
else
res += Get(id , rc , mid , nr);
return res;
}
int Geta(int id , int node = 1 , int nl = 1 , int nr = n + 1)
{
int res = a[node];
if(nr - nl == 1)
return res;
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
if(id < mid)
res += Geta(id , lc , nl , mid);
else
res += Geta(id , rc , mid , nr);
return res;
}
} seg;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> D;
for(int i = 1 ; i <= n ; i++)
cin >> a[i];
cin >> q;
for(int i = 0 ; i < q ; i++)
{
int l , r; cin >> l >> r;
qu[r].push_back(make_pair(l , i));
}
vector <int> st;
for(int i = 1 ; i <= n ; i++)
{
if(a[i] >= a[i - 1])
{
s[i] = (a[i] - a[i - 1]) / D;
st.push_back(i);
}
else
{
int k = (a[i - 1] - a[i] + D - 1) / D;
seg.Add(1 , i , -k , k * i);
while(!st.empty() && k > 0)
{
int tmp = min(k , s[st.back()]);
seg.Add(1 , st.back() , tmp , -tmp * st.back());
s[st.back()] -= tmp;
k -= tmp;
if(s[st.back()] == 0)
st.pop_back();
}
}
for(auto u : qu[i])
{
ans[u.S] = seg.Get(u.F);
int tmp = abs(seg.Geta(u.F));
//cout << tmp << endl;
if(tmp * D > a[u.F])
ans[u.S] = -1;
}
}
for(int i = 0 ; i < q ; i++)
cout << ans[i] << '\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... |