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>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 3e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
int n, q, d, a[N], diff[N];
pair<int,int> Q[N];
namespace Subtask1{
void process()
{
for(int cur_query = 1; cur_query <= q; cur_query++){
int pos_l = Q[cur_query].first, pos_r = Q[cur_query].second;
int answer = 0; bool can = true;
stack<pair<int,int>> save;
save.push(make_pair(a[pos_l]/d, pos_l));
for(int i = pos_l+1; i <= pos_r; i++){
if(diff[i] >= 0){
save.push(make_pair(diff[i]/d, i));
}
else{
int need = (-diff[i] + d-1) / d;
while(!save.empty() && need){
pair<int,int> e = save.top(); save.pop();
int oper = e.first, l = e.second;
int op_done = min(oper, need);
need -= op_done, oper -= op_done;
answer = answer + op_done * (i-l);
if(oper) save.push(make_pair(oper, l));
}
if(need){
can = false;
break;
}
}
}
cout << (can ? answer : -1) <<"\n";
}
}
}
void solve()
{
cin >> n >> d;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(i > 1){
diff[i] = a[i] - a[i-1];
}
}
cin >> q;
for(int i = 1; i <= q; i++){
cin >> Q[i].first >> Q[i].second;
}
Subtask1::process();
return;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |