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;
/* sorry, this is the bare minimum :'( */
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second
const int N = 2e5 + 5;
int n, q;
int a[N];
int prv[N];
int sub[N];
int intersect(int l1, int r1, int l2, int r2) {
return max(0, min(r1, r2) - max(l1, l2) + 1);
}
int main(int argc, char const *argv[])
{
cin.sync_with_stdio(0); cin.tie(0);
cin.exceptions(cin.failbit);
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
a[0] = INT_MAX;
vector<int> stk = {0};
for(int i = 1; i <= n; ++i) {
while(a[stk.back()] <= a[i]) {
stk.pop_back();
}
prv[i] = stk.back();
stk.push_back(i);
}
fill(sub+1, sub+1+n, 1);
for(int i = n; i >= 1; --i) {
sub[prv[i]] += sub[i];
}
while(q--) {
int T, l, r;
cin >> T >> l >> r;
ll ans = 0;
for(int i = l; i <= r; ++i) ans += a[i];
for(int i = 1; i <= n; ++i) if(prv[i]) {
ll f = a[prv[i]] - a[i];
int intersex;
if(i+sub[i]-1 <= prv[i]+T) {
intersex = intersect(i, i+sub[i]-1, l, r);
} else {
intersex = intersect(i, prv[i]+T, l, r);
}
ans += f * intersex;
}
cout << ans << endl;
}
return 0;
}
# | 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... |