#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;
}
} 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 * i);
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);
}
for(int i = 0 ; i < q ; i++)
cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Incorrect |
3 ms |
7364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
165 ms |
44396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
69 ms |
24660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
35536 KB |
Output is correct |
2 |
Correct |
142 ms |
43980 KB |
Output is correct |
3 |
Correct |
98 ms |
27952 KB |
Output is correct |
4 |
Correct |
163 ms |
47308 KB |
Output is correct |
5 |
Correct |
164 ms |
48468 KB |
Output is correct |
6 |
Correct |
179 ms |
49148 KB |
Output is correct |
7 |
Correct |
164 ms |
39524 KB |
Output is correct |
8 |
Correct |
181 ms |
49260 KB |
Output is correct |
9 |
Incorrect |
138 ms |
43980 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Incorrect |
3 ms |
7364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |