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;
typedef long long LL;
int N,Q;
int H[2000000], l[2000000], r[2000000];
LL ans[2000000];
vector<pair<int,int>> QU, M;
LL pairs(LL n) {
return n*(n-1)/2 + n;
}
int main() {
cin >> N >> Q;
for (int i = 0; i < N; i++) {
cin >> H[i];
M.emplace_back(H[i],i);
l[i] = i+1;
r[i] = i-1;
}
for (int i = 0; i < Q; i++) {
int y;
cin >> y;
QU.emplace_back(y,i);
}
sort(QU.begin(), QU.end());
sort(M.begin(), M.end());
int k = 0;
LL cnt = 0;
for (auto m: M) {
int h,i;
tie(h,i) = m;
while (k < Q && QU[k].first < h) {
ans[QU[k].second] = cnt;
k++;
}
cnt += pairs(r[i]-l[i]-1);
cnt -= pairs(r[i]-i-1);
cnt -= pairs(i-l[i]-1);
if (l[i] != -1) {
l[r[i]] = l[i];
}
if (r[i] != N) {
r[l[i]] = r[i];
}
}
while (k < Q) {
ans[QU[k].second] = cnt;
k++;
}
for (int i = 0; i < Q; i++) cout << ans[i] << "\n";
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... |
# | 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... |