#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX_TREE 100005
int numTree, numQuery;
int h[MAX_TREE];
void init(void){
cin >> numTree >> numQuery;
for (int i = 1; i <= numTree; ++i) cin >> h[i];
sort(h + 1, h + numTree + 1);
}
template<class T = int> struct FenwickTree{
T *F = nullptr;
int n;
FenwickTree(int _n = 0){
resize(_n);
}
void resize(int _n = 0){
this -> n = _n;
if (F != nullptr) delete[] F;
F = new T[n + 5];
clear();
}
void clear(void){
fill(F, F + 1 + n, 0);
}
void upd(int p, T val){
for (; p <= n; p += p & (-p)) F[p] += val;
}
T query(int p){
T res = 0;
for (; p > 0; p -= p & (-p)) res += F[p];
return res;
}
int lower_bound(int val){
int res = 0;
for (int i = __lg(n); i >= 0; --i){
if ((res | MASK(i) <= n) && val > F[res | MASK(i)]){
val -= F[res | MASK(i)];
res |= MASK(i);
}
}
return res + 1;
}
int upper_bound(T val){
int res = 0;
for (int i = __lg(n); i >= 0; --i){
if ((res | MASK(i) <= n) && val >= F[res | MASK(i)]){
val -= F[res | MASK(i)];
res |= MASK(i);
}
}
return res + 1;
}
};
int sub[MAX_TREE];
void process(void){
FenwickTree <int> fen(numTree);
for (int i = 1; i <= numTree; ++i){
sub[i] = h[i] - h[i - 1];
fen.upd(i, sub[i]);
}
for (int i = 1; i <= numQuery; ++i){
char c; cin >> c;
if(c == 'F'){
int k, lim; cin >> k >> lim;
int l = fen.lower_bound(lim);
int r = min(l + k - 1, numTree);
if (r == numTree) fen.upd(l, 1);
else{
int last = fen.query(r);
int last_pos = fen.upper_bound(last) - 1;
int first_pos = fen.lower_bound(last);
if (last_pos == r){
fen.upd(l, 1); fen.upd(r + 1, -1);
}
else{
fen.upd(l, 1); fen.upd(r, -1);
int nw_pos = last_pos - (first_pos - l - 1);
fen.upd(nw_pos, 1); fen.upd(min(last_pos + 1, numTree), -1);
}
}
}
else{
int l, r; cin >> l >> r;
l = fen.lower_bound(l);
r = fen.upper_bound(r);
cout << r - l << '\n';
}
}
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
init();
process();
}
Compilation message
grow.cpp: In instantiation of 'long long int FenwickTree<T>::lower_bound(long long int) [with T = long long int]':
grow.cpp:106:40: required from here
grow.cpp:75:32: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
75 | if ((res | MASK(i) <= n) && val > F[res | MASK(i)]){
| ^
grow.cpp: In instantiation of 'long long int FenwickTree<T>::upper_bound(T) [with T = long long int]':
grow.cpp:113:52: required from here
grow.cpp:85:32: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
85 | if ((res | MASK(i) <= n) && val >= F[res | MASK(i)]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
5208 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
1720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
1884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
9 ms |
4188 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
9 ms |
4700 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
4696 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
5212 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
5032 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
5984 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |