#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int N = 1e5+10;
const int INF = INT_MAX;
const int mod = 1e9+7;
int n,q,a[N];
int tree[4*N],lazy[4*N];
void build(int id = 1, int l = 1, int r = n) {
if(l == r) {
tree[id] = a[l];
return;
}
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid+1,r);
tree[id] = max(tree[id << 1],tree[id << 1 | 1]);
}
void down(int id, int l, int r) {
int t = lazy[id];
tree[id] += t;
if(l < r) {
lazy[id << 1] += t;
lazy[id << 1 | 1] += t;
}
lazy[id] = 0;
}
void update(int u, int v, int val, int id = 1, int l = 1, int r = n) {
down(id,l,r);
if(l > r || u > v || r < u || v < l) {
return;
}
if(u <= l && r <= v) {
lazy[id] = val;
down(id,l,r);
return;
}
int mid = l + r >> 1;
update(u,v,val,id << 1,l,mid);
update(u,v,val,id << 1 | 1,mid+1,r);
tree[id] = max(tree[id << 1],tree[id << 1 | 1]);
}
int get(int u, int v, int id = 1, int l = 1, int r = n) {
down(id,l,r);
if(l > r || u > v || r < u || v < l) {
return 0;
}
if(u <= l && r <= v) {
return tree[id];
}
int mid = l + r >> 1;
return max(get(u,v,id << 1,l,mid),get(u,v,id << 1 | 1,mid+1,r));
}
int LOW(int x) {
int l = 1;
int r = n;
int res = n+1;
while(l <= r) {
int mid = l + r >> 1;
if(get(mid,mid) >= x) {
res = mid;
r = mid-1;
}
else {
l = mid+1;
}
}
return res;
}
int UP(int x) {
int l = 1;
int r = n;
int res = n+1;
while(l <= r) {
int mid = l + r >> 1;
if(get(mid,mid) > x) {
res = mid;
r = mid-1;
}
else {
l = mid+1;
}
}
return res;
}
signed main()
{
if (fopen("solve.inp", "r")) {
freopen("solve.inp", "r", stdin);
freopen("solve.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
sort(a+1,a+n+1);
build();
// for(int i = 1 ; i <= n ; i++) {
// cout << a[i] << " ";
// }
// cout << '\n';
while(q--) {
char c; cin >> c;
int x,y; cin >> x >> y;
if(c == 'F') {
int l = LOW(y);
if(l+x-1 >= n) {
update(l,n,1);
continue;
}
int tmp = get(l+x-1,l+x-1);
int r = UP(tmp)-1;
tmp = LOW(tmp);
x -= tmp-l;
update(l,tmp-1,1);
update(r-x+1,r,1);
}
else {
cout << UP(y) - LOW(x) << '\n';
}
}
return 0;
}
Compilation message
grow.cpp: In function 'void build(int, int, int)':
grow.cpp:19:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'void update(int, int, int, int, int, int)':
grow.cpp:45:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int get(int, int, int, int, int)':
grow.cpp:59:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int LOW(int)':
grow.cpp:68:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
68 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int UP(int)':
grow.cpp:85:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int main()':
grow.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen("solve.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen("solve.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
475 ms |
3676 KB |
Output is correct |
2 |
Correct |
665 ms |
5328 KB |
Output is correct |
3 |
Correct |
245 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2392 KB |
Output is correct |
2 |
Correct |
9 ms |
2392 KB |
Output is correct |
3 |
Correct |
9 ms |
2396 KB |
Output is correct |
4 |
Correct |
6 ms |
2392 KB |
Output is correct |
5 |
Correct |
276 ms |
3632 KB |
Output is correct |
6 |
Correct |
370 ms |
3664 KB |
Output is correct |
7 |
Correct |
19 ms |
2648 KB |
Output is correct |
8 |
Correct |
228 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
353 ms |
2888 KB |
Output is correct |
2 |
Correct |
353 ms |
3972 KB |
Output is correct |
3 |
Correct |
5 ms |
2392 KB |
Output is correct |
4 |
Correct |
297 ms |
3492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
333 ms |
3156 KB |
Output is correct |
2 |
Correct |
388 ms |
3824 KB |
Output is correct |
3 |
Correct |
41 ms |
2648 KB |
Output is correct |
4 |
Correct |
347 ms |
4048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
475 ms |
2940 KB |
Output is correct |
2 |
Correct |
645 ms |
4592 KB |
Output is correct |
3 |
Correct |
85 ms |
3152 KB |
Output is correct |
4 |
Correct |
205 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
548 ms |
3408 KB |
Output is correct |
2 |
Correct |
616 ms |
4688 KB |
Output is correct |
3 |
Correct |
243 ms |
4832 KB |
Output is correct |
4 |
Correct |
85 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
532 ms |
3408 KB |
Output is correct |
2 |
Correct |
490 ms |
4724 KB |
Output is correct |
3 |
Correct |
243 ms |
4944 KB |
Output is correct |
4 |
Correct |
83 ms |
3112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
681 ms |
3380 KB |
Output is correct |
2 |
Correct |
661 ms |
3408 KB |
Output is correct |
3 |
Correct |
86 ms |
3928 KB |
Output is correct |
4 |
Correct |
600 ms |
4824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
633 ms |
3612 KB |
Output is correct |
2 |
Correct |
666 ms |
4964 KB |
Output is correct |
3 |
Execution timed out |
1018 ms |
5200 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
573 ms |
3920 KB |
Output is correct |