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;
signed main() {
int n,q; cin>>n>>q;
vector<int>v(n);
for(auto &z : v) {
cin>>z;
}
bool inc = 1;
for(int i = 0 ; i < n-1 ; i++) {
if(v[i] > v[i+1])
inc = 0;
}
v.push_back(INT_MAX);
auto solve = [&] (int h) -> void {
long long p = n;
int l = 0, r = n;
while(l <= r) {
int mid = (l+r)/2;
if(v[mid] > h) {
p = mid, r = mid-1;
} else l = mid+1;
}
cout << (p * (p+1))/2 << '\n';
};
while(q--) {
int h; cin>>h;
if(h == (int)1e6) {
long long x = n;
cout << (x * (x+1))/2 << '\n';
continue;
}
if(inc) {
solve(h);
continue;
}
long long cnt = 0;
long long ans = 0;
for(int i = 0 ; i <= n ; i++) {
if(v[i] <= h) {
cnt++;
} else {
ans += (cnt * (cnt+1))/2;
cnt = 0;
}
}
cout << ans << '\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... |
# | 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... |