#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;
int n, q, bit[100001], a[100001];
void update(int l, int r, int val) {
for (; l <= n; l += (l & (-l))) bit[l] += val;
for (r++; r <= n; r += (r & (-r))) bit[r] -= val;
}
int query(int pos) {
int ans = 0;
for (; pos; pos -= (pos & (-pos))) ans += bit[pos];
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
FOR(i, 1, n + 1) cin >> a[i];
sort(a + 1, a + n + 1);
FOR(i, 1, n + 1) update(i, i, a[i]);
while (q--) {
char c;
int a, b;
cin >> c >> a >> b;
if (c == 'F') {
int l1 = 1, r1 = n;
while (l1 != r1) {
int mid = (l1 + r1) / 2;
if (query(mid) < b) l1 = mid + 1;
else r1 = mid;
}
if (query(l1) < b) continue;
if (n - l1 < a) {
update(l1, n, 1);
continue;
}
int l2 = l1 - 1, r2 = l1 + a - 1, mx = query(r2);
while (l2 != r2) {
int mid = (l2 + r2 + 1) / 2;
if (query(mid) == mx) r2 = mid - 1;
else l2 = mid;
}
int l3 = 1, r3 = n;
while (l3 != r3) {
int mid = (l3 + r3 + 1) / 2;
if (query(mid) > mx) r3 = mid - 1;
else l3 = mid;
}
update(l1, l2, 1);
a -= l2 - l1 + 1;
update(l3 - a + 1, l3, 1);
} else {
if (a > query(n) || b < query(1)) {
cout << "0\n";
continue;
}
int l1 = 1, r1 = n, l2 = 1, r2 = n;
while (l1 != r1) {
int mid = (l1 + r1) / 2;
if (query(mid) < a) l1 = mid + 1;
else r1 = mid;
}
while (l2 != r2) {
int mid = (l2 + r2 + 1) / 2;
if (query(mid) > b) r2 = mid - 1;
else l2 = mid;
}
cout << l2 - l1 + 1 << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
2424 KB |
Output is correct |
2 |
Correct |
121 ms |
2860 KB |
Output is correct |
3 |
Correct |
51 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
7 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
57 ms |
1400 KB |
Output is correct |
6 |
Correct |
71 ms |
1656 KB |
Output is correct |
7 |
Correct |
8 ms |
512 KB |
Output is correct |
8 |
Correct |
27 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
1660 KB |
Output is correct |
2 |
Correct |
69 ms |
1912 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
35 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
2040 KB |
Output is correct |
2 |
Correct |
76 ms |
1784 KB |
Output is correct |
3 |
Correct |
11 ms |
512 KB |
Output is correct |
4 |
Correct |
67 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
2040 KB |
Output is correct |
2 |
Correct |
104 ms |
2296 KB |
Output is correct |
3 |
Correct |
23 ms |
896 KB |
Output is correct |
4 |
Correct |
47 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
2040 KB |
Output is correct |
2 |
Correct |
109 ms |
2424 KB |
Output is correct |
3 |
Correct |
52 ms |
2556 KB |
Output is correct |
4 |
Correct |
23 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
2168 KB |
Output is correct |
2 |
Correct |
78 ms |
2424 KB |
Output is correct |
3 |
Correct |
55 ms |
2684 KB |
Output is correct |
4 |
Correct |
23 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
2940 KB |
Output is correct |
2 |
Correct |
104 ms |
2296 KB |
Output is correct |
3 |
Correct |
37 ms |
1912 KB |
Output is correct |
4 |
Correct |
56 ms |
2280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
2552 KB |
Output is correct |
2 |
Correct |
120 ms |
2808 KB |
Output is correct |
3 |
Correct |
123 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
3448 KB |
Output is correct |