//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
int a[maxn], seg[4 * maxn], tag[4 * maxn];
void push(int cur, int l, int r) {
seg[cur] += tag[cur];
if (r - l > 1) {
tag[cur * 2] += tag[cur];
tag[cur * 2 + 1] += tag[cur];
}
tag[cur] = 0;
}
void modify(int cur, int l, int r, int ql, int qr, int val) {
if (r <= l || qr <= l || ql >= r) return;
push(cur, l, r);
if (ql <= l && qr >= r) {
tag[cur] += val;
push(cur, l, r);
return;
}
int mid = (l + r) / 2;
modify(cur * 2, l, mid, ql, qr, val);
modify(cur * 2 + 1, mid, r, ql, qr, val);
seg[cur] = max(seg[cur * 2] + tag[cur * 2], seg[cur * 2 + 1] + tag[cur * 2 + 1]);
}
int query(int cur, int l, int r, int x) { //number of things <= x in range [l, r)
if (r <= l) return 0;
push(cur, l, r);
if (seg[cur] <= x) return r - l;
if (r - l == 1) return 0;
int mid = (l + r) / 2;
if (seg[cur * 2] + tag[cur * 2] > x) return query(cur * 2, l, mid, x);
else return query(cur * 2, l, mid, x) + query(cur * 2 + 1, mid, r, x);
}
int getval(int cur, int l, int r, int ind) {
if (r <= l) return 0;
if (ind >= r) return r;
push(cur, l, r);
if (r - l == 1) return seg[cur];
int mid = (l + r) / 2;
if (ind < mid) return getval(cur * 2, l, mid, ind);
else return getval(cur * 2 + 1, mid, r, ind);
}
int main() {
io
int n, m;
cin >> n >> m;
for (int i = 0;i < n;i++) cin >> a[i];
sort(a, a + n);
for (int i = 0;i < n;i++) {
modify(1, 0, n, i, i + 1, a[i]);
}
while (m--) {
char type;
cin >> type;
if (type == 'F') {
int ci, hi;
cin >> ci >> hi;
int lef = query(1, 0, n, hi - 1);
int val = getval(1, 0, n, lef + ci - 1);
int rig = query(1, 0, n, val - 1), r2 = query(1, 0, n, val);
//cout << lef << " " << rig << endl;
modify(1, 0, n, lef, rig, 1);
int cnt = ci - (rig - lef);
//cout << r2 - cnt << " " << r2 << endl;
modify(1, 0, n, max(rig, r2 - cnt), r2, 1);
//for (int i = 0;i < n;i++) cout << getval(1, 0, n, i) << " ";
//cout << endl;
} else {
int mi, ma;
cin >> mi >> ma;
cout << query(1, 0, n, ma) - query(1, 0, n, mi - 1) << "\n";
}
}
}
/*
5 7
1 3 2 5 2
F 2 1
C 3 6
F 2 3
C 6 8
F 2 1
F 2 2
C 3 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
2924 KB |
Output is correct |
2 |
Incorrect |
158 ms |
4588 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
4 ms |
492 KB |
Output is correct |
3 |
Incorrect |
3 ms |
492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
876 KB |
Output is correct |
2 |
Incorrect |
62 ms |
1900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
1004 KB |
Output is correct |
2 |
Correct |
67 ms |
1900 KB |
Output is correct |
3 |
Correct |
13 ms |
620 KB |
Output is correct |
4 |
Correct |
61 ms |
1960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
1900 KB |
Output is correct |
2 |
Correct |
140 ms |
4076 KB |
Output is correct |
3 |
Correct |
24 ms |
1388 KB |
Output is correct |
4 |
Correct |
116 ms |
4204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
2796 KB |
Output is correct |
2 |
Correct |
144 ms |
4332 KB |
Output is correct |
3 |
Incorrect |
142 ms |
4332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
2924 KB |
Output is correct |
2 |
Correct |
107 ms |
4204 KB |
Output is correct |
3 |
Incorrect |
145 ms |
4460 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
2956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
3052 KB |
Output is correct |
2 |
Incorrect |
159 ms |
4588 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
3308 KB |
Output is correct |