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;
#define N 100000
int n, q, a[N], bit[N];
void add(int l, int r, int x) {
if (r < l) return;
for (; l < n; l |= l + 1) bit[l] += x;
for (++r; r < n; r |= r + 1) bit[r] -= x;
}
int query(int i) {
int sum = 0;
for (; i >= 0; i &= i + 1, --i) sum += bit[i];
return sum;
}
template <class T, class U> T firstTrue(T lo, T hi, U f) {
assert(lo <= hi);
++hi;
while (lo < hi) {
T mid = lo + (hi - lo) / 2;
f(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; ++i) add(i, i, a[i]);
for (int i = 0; i < q; ++i) {
char c;
int a, b;
cin >> c >> a >> b;
if (c == 'F') {
int l = firstTrue(0, n - 1, [&](int i) { return query(i) >= b; });
if (l == n) continue;
int r = l + a - 1;
if (r >= n - 1) {
add(l, n - 1, 1);
continue;
}
int x = query(r);
int l_ = firstTrue(l, n - 1, [&](int i) { return query(i) >= x; });
int r_ = firstTrue(l_, n - 1, [&](int i) { return query(i) > x; }) - 1;
add(l, l_ - 1, 1);
add(r_ - (a - (l_ - l)) + 1, r_, 1);
} else {
int l = firstTrue(0, n - 1, [&](int i) { return query(i) >= a; });
int r = firstTrue(0, n - 1, [&](int i) { return query(i) > b; });
cout << r - l << '\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... |