#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n, m;
int t[4 * N];
int lazy[4 * N];
void push(int v, int tl, int tr) {
if(lazy[v]) {
t[v] += lazy[v] * (tr - tl + 1);
if(tl != tr) {
lazy[2 * v + 1] += lazy[v];
lazy[2 * v + 2] += lazy[v];
}
lazy[v] = 0;
}
}
void update(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if(l > r) return;
if(tl == l && tr == r) {
lazy[v]++;
push(v, tl, tr);
return;
}
int tm = (tl + tr) >> 1;
update(2 * v + 1, tl, tm, l, min(r, tm));
update(2 * v + 2, tm + 1, tr, max(l, tm + 1), r);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
int get(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if(l > r) return 0;
if(tl == l && tr == r) return t[v];
int tm = (tl + tr) >> 1;
int lf = get(2 * v + 1, tl, tm, l, min(r, tm));
int rg = get(2 * v + 2, tm + 1, tr, max(l, tm + 1), r);
return lf + rg;
}
int get(int pos) {
return get(0, 1, n, pos, pos);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
vector <int> a(n + 1, 0);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
a[0] = -1;
sort(a.begin(), a.end());
while(m--) {
char type;
cin >> type;
if(type == 'F') {
int c, h;
cin >> c >> h;
int low = 1, high = n, pos = n + 1;
while(low <= high) {
int mid = (low + high) >> 1;
if(a[mid] + get(mid) >= h) {
pos = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
if(pos == n + 1) continue;
int to = pos + c - 1;
to = min(n, to);
if(to == n || a[to + 1] + get(to + 1) != a[to] + get(to)) {
update(0, 1, n, pos, to);
} else {
int f = a[to] + get(to);
low = 1, high = n;
int l = 0;
while(low <= high) {
int mid = (low + high) >> 1;
if(a[mid] + get(mid) < f) {
l = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
low = 1, high = n;
int r = n + 1;
while(low <= high) {
int mid = (low + high) >> 1;
if(a[mid] + get(mid) > f) {
r = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
l++;
r--;
int len = to - l + 1;
update(0, 1, n, pos, l - 1);
update(0, 1, n, r - len + 1, r);
}
} else {
int mn, mx;
cin >> mn >> mx;
int low = 1, high = n, l = n + 1;
while(low <= high) {
int mid = (low + high) >> 1;
if(a[mid] + get(mid) >= mn) {
l = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
low = 1, high = n;
int r = 0;
while(low <= high) {
int mid = (low + high) >> 1;
if(a[mid] + get(mid) <= mx) {
r = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
if(l > r) {
cout << "0\n";
continue;
}
cout << r - l + 1 << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
556 ms |
3832 KB |
Output is correct |
2 |
Correct |
609 ms |
4600 KB |
Output is correct |
3 |
Correct |
301 ms |
4488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
12 ms |
504 KB |
Output is correct |
4 |
Correct |
8 ms |
476 KB |
Output is correct |
5 |
Correct |
274 ms |
1752 KB |
Output is correct |
6 |
Correct |
373 ms |
1888 KB |
Output is correct |
7 |
Correct |
20 ms |
632 KB |
Output is correct |
8 |
Correct |
234 ms |
1432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
361 ms |
1400 KB |
Output is correct |
2 |
Correct |
358 ms |
2040 KB |
Output is correct |
3 |
Correct |
7 ms |
476 KB |
Output is correct |
4 |
Correct |
279 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
341 ms |
888 KB |
Output is correct |
2 |
Correct |
410 ms |
1868 KB |
Output is correct |
3 |
Correct |
34 ms |
632 KB |
Output is correct |
4 |
Correct |
352 ms |
2228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
549 ms |
2424 KB |
Output is correct |
2 |
Correct |
718 ms |
4216 KB |
Output is correct |
3 |
Correct |
95 ms |
1400 KB |
Output is correct |
4 |
Correct |
242 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
636 ms |
3576 KB |
Output is correct |
2 |
Correct |
723 ms |
4216 KB |
Output is correct |
3 |
Correct |
297 ms |
4408 KB |
Output is correct |
4 |
Correct |
92 ms |
1400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
587 ms |
3592 KB |
Output is correct |
2 |
Correct |
543 ms |
4216 KB |
Output is correct |
3 |
Correct |
300 ms |
4472 KB |
Output is correct |
4 |
Correct |
92 ms |
1400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
644 ms |
4092 KB |
Output is correct |
2 |
Correct |
729 ms |
4088 KB |
Output is correct |
3 |
Correct |
83 ms |
3576 KB |
Output is correct |
4 |
Correct |
626 ms |
4096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
714 ms |
3932 KB |
Output is correct |
2 |
Correct |
622 ms |
4600 KB |
Output is correct |
3 |
Correct |
750 ms |
4760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
634 ms |
4368 KB |
Output is correct |