#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////
void usaco_problem(){
freopen("milkvisits.in", "r", stdin);
freopen("milkvisits.out", "w", stdout);
}
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 3005;
const int LOG = 25;
const int inf = 1e9;
const ll mod = 998244353;
#define int ll
ll arr[mx];
ll tmp[mx], cop[mx];
int pf[mx];
int32_t main(){
speed;
ll d;
int n;cin >> n >> d;
for(int i = 0; i < n;i++){
cin >> arr[i];
}
int q;cin >> q;
while(q--){
int l, r;cin >> l >> r;l--;r--;
ll done = 0;
bool ok = 1;
for(int i = l;i <= r;i++){
cop[i] = arr[i];
tmp[i] = arr[i] % d;
}
for(int i = l;i <= r;i++){
tmp[i] -= (done % d);
tmp[i] %= d;
while(tmp[i] < 0)tmp[i] += d;
done += tmp[i];
cop[i] -= done;
if(done > arr[i]){ok = 0;break;}
}
if(!ok){
cout << -1 << endl;
continue;
}
for(int i = l;i <= r;i++){
cop[i] = cop[i]/d;
}
pf[r] = cop[r];
for(int i = r-1; i >= l;i--){
pf[i] = min(pf[i+1], cop[i]);
}
int st = l, en = r;
while(st <= en){
int k1 = st, k2 = en;
int k = l;
while(k1 <= k2){
int md = (k1+k2)/2;
if(pf[md] == pf[st]){
k1 = md+1;
k = md;
}
else{
k2 = md-1;
}
}
for(int i = st; i <= k;i++){
cop[i] -= cop[k];
}
st = k+1;
}
ll ans = 0;
for(int i = l;i <= r;i++){
ans += cop[i];
}
cout << ans << endl;
}
}
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
Your Guide when stuck:
- Continue keyword only after reading the whole input
- Don't use memset with testcases
- Check for corner cases(n=1, n=0)
- Check where you declare n(Be careful of declaring it globally and in main)
*/
Compilation message
Main.cpp: In function 'void usaco_problem()':
Main.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("milkvisits.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen("milkvisits.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void init()':
Main.cpp:40:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:42:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
43 ms |
604 KB |
Output is correct |
5 |
Correct |
9 ms |
604 KB |
Output is correct |
6 |
Correct |
29 ms |
608 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
8 ms |
576 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
46 ms |
612 KB |
Output is correct |
11 |
Correct |
9 ms |
600 KB |
Output is correct |
12 |
Correct |
87 ms |
604 KB |
Output is correct |
13 |
Correct |
43 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2025 ms |
4748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
43 ms |
604 KB |
Output is correct |
5 |
Correct |
9 ms |
604 KB |
Output is correct |
6 |
Correct |
29 ms |
608 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
8 ms |
576 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
46 ms |
612 KB |
Output is correct |
11 |
Correct |
9 ms |
600 KB |
Output is correct |
12 |
Correct |
87 ms |
604 KB |
Output is correct |
13 |
Correct |
43 ms |
600 KB |
Output is correct |
14 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |