#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,d;
cin >> n >> d;
vector<int> arr(n+1);
for(int i=1;i<=n;i++)cin>>arr[i];
int q;
cin >> q;
for(int i=1;i<=q;i++){
int l,r;cin>>l>>r;
vector<int> curr(n+1);
for(int x=l;x<=r;x++){
curr[x] = curr[x-1]+((d+(arr[x]%d)-(curr[x-1]%d))%d);
}
int uses = 1e15;
int ans = 0;
bool works = true;
for(int x=r;x>=l;x--){
int req = (arr[x]-curr[x])/d;
if(req<0)works=false;
uses = min(uses,req);
ans+=req-uses;
}
if(works)cout<<ans<<'\n';
else cout << "-1\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
33 ms |
608 KB |
Output is correct |
5 |
Correct |
35 ms |
344 KB |
Output is correct |
6 |
Correct |
23 ms |
348 KB |
Output is correct |
7 |
Correct |
19 ms |
348 KB |
Output is correct |
8 |
Correct |
35 ms |
348 KB |
Output is correct |
9 |
Correct |
34 ms |
604 KB |
Output is correct |
10 |
Correct |
34 ms |
344 KB |
Output is correct |
11 |
Correct |
35 ms |
348 KB |
Output is correct |
12 |
Correct |
35 ms |
604 KB |
Output is correct |
13 |
Correct |
35 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2071 ms |
5892 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2048 ms |
7440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2025 ms |
5056 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
33 ms |
608 KB |
Output is correct |
5 |
Correct |
35 ms |
344 KB |
Output is correct |
6 |
Correct |
23 ms |
348 KB |
Output is correct |
7 |
Correct |
19 ms |
348 KB |
Output is correct |
8 |
Correct |
35 ms |
348 KB |
Output is correct |
9 |
Correct |
34 ms |
604 KB |
Output is correct |
10 |
Correct |
34 ms |
344 KB |
Output is correct |
11 |
Correct |
35 ms |
348 KB |
Output is correct |
12 |
Correct |
35 ms |
604 KB |
Output is correct |
13 |
Correct |
35 ms |
348 KB |
Output is correct |
14 |
Execution timed out |
2071 ms |
5892 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |