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
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
struct Req { int lft, iReq; };
struct Itv {
int l, r, val;
int sumVal(int lim_l = -1e18) {
return (r - max(lim_l, l) + 1) * val;
}
};
bool operator<(Itv a, Itv b) { return a.l < b.l; };
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbFish, delta;
cin >> nbFish >> delta;
vector<int> ideal(nbFish);
vector<int> sumQt {0}, sumDecal {0};
rep(i, 0, nbFish) {
cin >> ideal[i];
sumQt.push_back(sumQt.back() + ideal[i]/delta);
int decal = i && (ideal[i-1] % delta > ideal[i] % delta);
sumDecal.push_back(sumDecal.back() + decal);
}
vector<int> quadDecal {0};
for (int sigma : sumDecal) {
quadDecal.push_back(quadDecal.back() + sigma);
}
int nbReq; cin >> nbReq;
vector<vector<Req>> reqs(nbFish);
rep(iReq, 0, nbReq) {
int lft, rgt;
cin >> lft >> rgt;
--lft, --rgt;
int targetQt = ideal[rgt] / delta;
int answer = sumQt[rgt+1] - sumQt[lft] - (rgt-lft+1)*targetQt;
answer += (rgt - lft + 1) * sumDecal[rgt+1] - quadDecal[rgt+2] + quadDecal[lft+1];
int decalLftRgt = sumDecal[rgt+1] - sumDecal[lft+1];
if (decalLftRgt > targetQt) {
answer = -1;
}
cout << answer << '\n';
}
}
# | 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... |