#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
struct node{
int mn, mx, lz;
};
int n, q;
const int N = 1e5 + 69;
node seg[4 * N];
int a[N];
void Build(int l, int r, int pos){
seg[pos].lz = 0;
if (l == r){
seg[pos].mn = a[l];
seg[pos].mx = a[l];
return;
}
int mid = (l + r) / 2;
Build(l, mid, pos * 2);
Build(mid + 1, r, pos * 2 + 1);
seg[pos].mn = min(seg[pos * 2].mn, seg[pos * 2 + 1].mn);
seg[pos].mx = max(seg[pos * 2].mx, seg[pos * 2 + 1].mx);
}
void updlz(int l, int r, int pos){
seg[pos].mn += seg[pos].lz;
seg[pos].mx += seg[pos].lz;
if (l == r){
seg[pos].lz = 0;
return;
}
seg[pos * 2].lz += seg[pos].lz;
seg[pos * 2 + 1].lz += seg[pos].lz;
seg[pos].lz = 0;
return;
}
int ele(int l, int r, int pos, int qp){
if (seg[pos].lz != 0){
updlz(l, r, pos);
}
if (l == r) return seg[pos].mx;
int mid = (l + r) / 2;
if (qp <= mid) return ele(l, mid, pos * 2, qp);
else return ele(mid + 1, r, pos * 2 + 1, qp);
}
int fls(int l, int r, int pos, int x){
if (seg[pos].lz != 0){
updlz(l, r, pos);
}
if (seg[pos].mn > x) return 0;
if (l == r){
return l;
}
int mid = (l + r) / 2;
int qp = fls(mid + 1, r, pos * 2 + 1, x);
if (qp == 0) qp = fls(l, mid, pos * 2, x);
return qp;
}
int ffg(int l, int r, int pos, int x){
if (seg[pos].lz != 0){
updlz(l, r, pos);
}
if (seg[pos].mx < x) return n + 1;
if (l == r){
return l;
}
int mid = (l + r) / 2;
int qp = ffg(l, mid, pos * 2, x);
if (qp == n + 1) qp = ffg(mid + 1, r, pos * 2 + 1, x);
return qp;
}
void upd(int l, int r, int pos, int ql, int qr){
if (seg[pos].lz != 0){
updlz(l, r, pos);
}
if (l >= ql && r <= qr){
seg[pos].mn++;
seg[pos].mx++;
if (l != r){
seg[pos * 2].lz++;
seg[pos * 2 + 1].lz++;
}
} else if (l > qr || r < ql){
} else {
int mid = (l + r) / 2;
upd(l, mid, pos * 2, ql, qr);
upd(mid + 1, r, pos * 2 + 1, ql, qr);
seg[pos].mn = min(seg[pos * 2].mn, seg[pos * 2 + 1].mn);
seg[pos].mx = max(seg[pos * 2].mx, seg[pos * 2 + 1].mx);
}
}
void Solve()
{
cin >> n >> q;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
Build(1, n, 1);
while (q--){
char ch; cin >> ch;
int x, y; cin >> x >> y;
if (ch == 'F'){
int l = ffg(1, n, 1, y);
x = min(x, n - l + 1);
int en = l + x - 1;
int v = ele(1, n, 1, en);
int p1 = fls(1, n, 1, v - 1);
int p2 = ffg(1, n, 1, v + 1) - 1;
upd(1, n, 1, l, p1);
x -= (p1 - l + 1);
assert(p1 - l + 1 >= 0);
assert(x >= 1);
upd(1, n, 1, p2 - x + 1, p2);
// upd(1, n, 1, l, l + x - 1);
} else {
int l = ffg(1, n, 1, x);
int r = fls(1, n, 1, y);
cout << r - l + 1 << "\n";
}
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
14352 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
37 ms |
972 KB |
Output is correct |
6 |
Runtime error |
3 ms |
1364 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
7 ms |
1412 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1364 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
24 ms |
7764 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
20 ms |
14164 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
15 ms |
14164 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
7260 KB |
Output is correct |
2 |
Runtime error |
26 ms |
14236 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
23 ms |
14300 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
7768 KB |
Output is correct |