#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define int i64
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define pii pair<int, int>
#define fi first
#define se second
#define sz(x) (int)(x).size()
struct BIT{
vi bit;
BIT(int n) : bit(n + 2) {}
void add(int idx, int val){
for(++idx; idx < sz(bit); idx += idx & -idx){
bit[idx] += val;
}
}
int sum(int idx){
int ret = 0;
for(++idx; idx; idx -= idx & -idx)
ret += bit[idx];
return ret;
}
};
int bs(int lo, int hi, function<bool(int)> check){
hi++;
while(lo < hi){
int mid = lo + (hi - lo) / 2;
if(check(mid))
hi = mid;
else
lo = mid + 1;
}
return lo;
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, m;
char type;
cin >> n >> m;
vi a(n);
BIT bit(n);
for(int &i : a)
cin >> i;
sort(a.begin(), a.end());
auto range_add = [&](int l, int r, int val) -> void {
bit.add(l, val);
bit.add(r + 1, -val);
};
for(int i = 0; i < n; i++){
range_add(i, i, a[i]);
}
while(m--){
int a, b;
cin >> type >> a >> b;
if(type == 'C'){
int up_l = bs(0, n - 1, [&](int idx) {return bit.sum(idx) >= a;} );
int low_r = bs(0, n - 1, [&](int idx) {return bit.sum(idx) > b;} ) - 1;
cout << low_r - up_l + 1 << "\n";
}
else{
int l = bs(0, n - 1, [&](int idx) {return bit.sum(idx) >= b;} ), r = l + a - 1;
if(l == n) continue;
if(r >= n - 1){
range_add(l, n - 1, 1);
continue;
}
int last_val = bit.sum(r);
int l_ = bs(l, n - 1, [&](int idx) {return bit.sum(idx) >= last_val;} ), r_ = bs(l, n - 1, [&](int idx) {return bit.sum(idx) > last_val;} ) - 1;
range_add(l, l_ - 1, 1);
range_add(r_ - (a - (l_ - 1 - l + 1)) + 1, r_, 1);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
3184 KB |
Output is correct |
2 |
Correct |
85 ms |
3556 KB |
Output is correct |
3 |
Correct |
32 ms |
3384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
34 ms |
1548 KB |
Output is correct |
6 |
Correct |
47 ms |
1764 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
21 ms |
1240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
1780 KB |
Output is correct |
2 |
Correct |
47 ms |
1936 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
24 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
1876 KB |
Output is correct |
2 |
Correct |
44 ms |
1748 KB |
Output is correct |
3 |
Correct |
5 ms |
680 KB |
Output is correct |
4 |
Correct |
47 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
2392 KB |
Output is correct |
2 |
Correct |
78 ms |
2780 KB |
Output is correct |
3 |
Correct |
13 ms |
1116 KB |
Output is correct |
4 |
Correct |
24 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
2680 KB |
Output is correct |
2 |
Correct |
78 ms |
2952 KB |
Output is correct |
3 |
Correct |
48 ms |
3164 KB |
Output is correct |
4 |
Correct |
19 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
2896 KB |
Output is correct |
2 |
Correct |
49 ms |
3004 KB |
Output is correct |
3 |
Correct |
31 ms |
3152 KB |
Output is correct |
4 |
Correct |
13 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
3408 KB |
Output is correct |
2 |
Correct |
75 ms |
2760 KB |
Output is correct |
3 |
Correct |
21 ms |
2652 KB |
Output is correct |
4 |
Correct |
66 ms |
2876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
3300 KB |
Output is correct |
2 |
Correct |
95 ms |
3376 KB |
Output is correct |
3 |
Correct |
87 ms |
3668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
4204 KB |
Output is correct |