Submission #979894

#TimeUsernameProblemLanguageResultExecution timeMemory
979894hugo_pmFish 3 (JOI24_fish3)C++17
100 / 100
252 ms61084 KiB
#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, qt; }; 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); } auto sumItv = [&] (Itv itv, int query_lft = -1e18) { int l = max(itv.l, query_lft); int nbElem = itv.r - l + 1; int totDecal = nbElem * sumDecal[itv.r+1] -quadDecal[itv.r+2] + quadDecal[l+1]; return nbElem * itv.qt - totDecal; }; auto qtAt = [&] (Itv itv, int i) { return itv.qt - (sumDecal[itv.r+1] - sumDecal[i+1]); }; int nbReq; cin >> nbReq; vector<vector<Req>> reqs(nbFish); rep(iReq, 0, nbReq) { int lft, rgt; cin >> lft >> rgt; --lft, --rgt; reqs[rgt].push_back({lft, iReq}); } vector<int> answers(nbReq); vector<Itv> stk; vector<int> sumStk {0} ; rep(rgt, 0, nbFish) { int qtRgt = ideal[rgt] / delta; Itv toPush {rgt, rgt, qtRgt}; while (!stk.empty() && stk.back().qt >= qtAt(toPush, toPush.l)) { toPush.l = stk.back().l; stk.pop_back(); sumStk.pop_back(); } stk.push_back(toPush); sumStk.push_back(sumStk.back() + sumItv(stk.back())); for (auto [lft, iReq] : reqs[rgt]) { #ifdef DEBUG dbg(lft, rgt); for (auto itv : stk) { dbg(itv.l, itv.r, itv.qt, sumItv(itv, lft)); } #endif int idItvContainingLft = lower_bound(all(stk), Itv {lft+1, -1, -1}) - stk.begin() - 1; int sumBelowIncluded = sumStk.back() - sumStk[idItvContainingLft+1]; int belowBorder = sumItv(stk[idItvContainingLft], lft); int totalBelow = sumBelowIncluded + belowBorder; int sumAbove = sumQt[rgt+1] - sumQt[lft]; answers[iReq] = sumAbove - totalBelow; dbg(sumAbove, sumBelowIncluded, belowBorder, sumDecal[rgt+1]); if (qtAt(stk[idItvContainingLft], lft) < 0) { answers[iReq] = -1; } } } for (int x : answers) { cout << x << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...