#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
int n, q, a[maxn], bit[maxn];
void upd(int l, int r, int k) {
if (r < l) return;
for (; l < n; l |= l + 1) bit[l] += k;
for ( ++r; r < n; r |= r + 1) bit[r] -= k;
}
int qry(int k) {
int ret = 0;
for ( ; k >= 0; k &= k + 1, --k) ret += bit[k];
return ret;
}
template<class T, class U> T firstTrue(T lo, T hi, U f) { // benjamin qi
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)
upd(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 qry(i) >= b;
});
if (l == n)
continue;
int r = l + a - 1;
if (r >= n - 1) {
upd(l, n - 1, 1);
continue;
}
int k = qry(r);
int l_ = firstTrue(l, n - 1, [&](int i) {
return qry(i) >= k;
});
int r_ = firstTrue(l_, n - 1, [&](int i) {
return qry(i) > k;
}) - 1;
upd(l, l_ - 1, 1);
upd(r_ - (a - (l_ - l)) + 1, r_, 1);
} else {
int l = firstTrue(0, n - 1, [&](int i) {
return qry(i) >= a;
});
int r = firstTrue(0, n - 1, [&](int i) {
return qry(i) > b;
});
cout << r - l << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
1152 KB |
Output is correct |
2 |
Correct |
104 ms |
2824 KB |
Output is correct |
3 |
Correct |
38 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
49 ms |
1392 KB |
Output is correct |
6 |
Correct |
68 ms |
1664 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
33 ms |
1060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
652 KB |
Output is correct |
2 |
Correct |
61 ms |
1692 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
37 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
804 KB |
Output is correct |
2 |
Correct |
70 ms |
1640 KB |
Output is correct |
3 |
Correct |
6 ms |
468 KB |
Output is correct |
4 |
Correct |
61 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
896 KB |
Output is correct |
2 |
Correct |
93 ms |
2296 KB |
Output is correct |
3 |
Correct |
22 ms |
860 KB |
Output is correct |
4 |
Correct |
34 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
1044 KB |
Output is correct |
2 |
Correct |
95 ms |
2264 KB |
Output is correct |
3 |
Correct |
36 ms |
2508 KB |
Output is correct |
4 |
Correct |
20 ms |
864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
1272 KB |
Output is correct |
2 |
Correct |
84 ms |
2328 KB |
Output is correct |
3 |
Correct |
38 ms |
2532 KB |
Output is correct |
4 |
Correct |
17 ms |
820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
1128 KB |
Output is correct |
2 |
Correct |
94 ms |
2256 KB |
Output is correct |
3 |
Correct |
29 ms |
1736 KB |
Output is correct |
4 |
Correct |
62 ms |
2180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
1196 KB |
Output is correct |
2 |
Correct |
111 ms |
2684 KB |
Output is correct |
3 |
Correct |
106 ms |
3008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
1488 KB |
Output is correct |