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> sumIdeal {0};
rep(i, 0, nbFish) {
cin >> ideal[i];
sumIdeal.push_back(sumIdeal.back() + ideal[i]);
}
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 valRgt = ideal[rgt];
int pushLft = rgt;
while (!stk.empty() && stk.back().val > valRgt) {
pushLft = stk.back().l;
stk.pop_back();
sumStk.pop_back();
}
stk.push_back({pushLft, rgt, valRgt});
sumStk.push_back(sumStk.back() + stk.back().sumVal());
for (auto [lft, iReq] : reqs[rgt]) {
int idItvContainingLft = lower_bound(all(stk), Itv {lft+1, -1, -1}) - stk.begin() - 1;
int sumBelowIncluded = sumStk.back() - sumStk[idItvContainingLft+1];
int belowBorder = stk[idItvContainingLft].sumVal(lft);
int sumAbove = sumIdeal[rgt+1] - sumIdeal[lft];
answers[iReq] = sumAbove - (sumBelowIncluded + belowBorder);
}
}
for (int x : answers) {
cout << x << '\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... |