#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BIT {
const int n;
vector<T> tree;
public:
BIT(int _n) : n(_n), tree(n + 1) {}
void update(int i, T val) {
assert(0 <= i and i <= n);
for (++i; i <= n; i += i & -i)
tree[i] += val;
}
T query(int i) {
assert(0 <= i and i <= n);
T ret = 0;
for (; i; i &= i - 1)
ret += tree[i];
return ret;
}
T query(int l, int r) {
assert(0 <= l and l <= r and r <= n);
return query(r) - query(l);
}
T get(int i) {
assert(0 <= i and i < n);
return i & 1 ? query(i, i + 1) : tree[i + 1];
}
int lower_bound(T k) {
int x = 0;
for (int pw = 1 << __lg(n); pw; pw >>= 1)
if ((x | pw) <= n && tree[x | pw] < k)
k -= tree[x |= pw];
return x;
}
int upper_bound(T k) {
if (k < 0) return -1;
int x = 0;
for (int pw = 1 << __lg(n); pw; pw >>= 1)
if ((x | pw) <= n && tree[x | pw] <= k)
k -= tree[x |= pw];
return x;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef palilo
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<int> a(n);
for (auto& x : a) cin >> x;
sort(a.begin(), a.end());
BIT<int> bit(n);
for (int i = 0; i < n; ++i)
bit.update(i, a[i] - (i ? a[i - 1] : 0));
for (int x, y; m--;) {
char op;
cin >> op >> x >> y;
if (op == 'F') {
const int it = bit.lower_bound(y);
if (it + x < n) {
const int last_value = bit.query(it + x);
const int lo = bit.lower_bound(last_value);
const int hi = bit.upper_bound(last_value);
bit.update(lo, -1);
bit.update(hi - (x - (lo - it)), 1);
bit.update(hi, -1);
}
bit.update(it, 1);
} else {
cout << bit.upper_bound(y) - bit.lower_bound(x) << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
1772 KB |
Output is correct |
2 |
Correct |
77 ms |
2684 KB |
Output is correct |
3 |
Correct |
41 ms |
2544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
37 ms |
1308 KB |
Output is correct |
6 |
Correct |
45 ms |
1604 KB |
Output is correct |
7 |
Correct |
4 ms |
332 KB |
Output is correct |
8 |
Correct |
25 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
1436 KB |
Output is correct |
2 |
Correct |
43 ms |
1648 KB |
Output is correct |
3 |
Correct |
2 ms |
328 KB |
Output is correct |
4 |
Correct |
34 ms |
1248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
1680 KB |
Output is correct |
2 |
Correct |
47 ms |
1592 KB |
Output is correct |
3 |
Correct |
5 ms |
460 KB |
Output is correct |
4 |
Correct |
47 ms |
1756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
1496 KB |
Output is correct |
2 |
Correct |
68 ms |
2168 KB |
Output is correct |
3 |
Correct |
15 ms |
844 KB |
Output is correct |
4 |
Correct |
44 ms |
2248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
1412 KB |
Output is correct |
2 |
Correct |
69 ms |
2372 KB |
Output is correct |
3 |
Correct |
39 ms |
2524 KB |
Output is correct |
4 |
Correct |
14 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
1776 KB |
Output is correct |
2 |
Correct |
51 ms |
2312 KB |
Output is correct |
3 |
Correct |
49 ms |
2632 KB |
Output is correct |
4 |
Correct |
14 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
1132 KB |
Output is correct |
2 |
Correct |
68 ms |
1756 KB |
Output is correct |
3 |
Correct |
28 ms |
1724 KB |
Output is correct |
4 |
Correct |
49 ms |
2096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
2052 KB |
Output is correct |
2 |
Correct |
89 ms |
2672 KB |
Output is correct |
3 |
Correct |
73 ms |
2964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
1584 KB |
Output is correct |