#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 id = 1, int l = 1, int r = n) {
down(id,l,r);
if(id == 1 && tree[id] < x) {
return n+1;
}
if(l == r) {
return l;
}
int mid = l + r >> 1;
down(id << 1,l,mid);
down(id << 1 | 1,mid+1,r);
if(tree[id << 1] >= x) {
return LOW(x,id << 1,l,mid);
}
return LOW(x,id << 1 | 1,mid+1,r);
}
int UP(int x, int id = 1, int l = 1, int r = n) {
down(id,l,r);
if(id == 1 && tree[id] <= x) {
return n+1;
}
if(l == r) {
return l;
}
int mid = l + r >> 1;
down(id << 1,l,mid);
down(id << 1 | 1,mid+1,r);
if(tree[id << 1] > x) {
return UP(x,id << 1,l,mid);
}
return UP(x,id << 1 | 1,mid+1,r);
}
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();
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, int, int, int)':
grow.cpp:71:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int UP(int, int, int, int)':
grow.cpp:88:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
88 | 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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
4628 KB |
Output is correct |
2 |
Correct |
104 ms |
5056 KB |
Output is correct |
3 |
Correct |
41 ms |
4892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
2392 KB |
Output is correct |
5 |
Correct |
38 ms |
3428 KB |
Output is correct |
6 |
Correct |
53 ms |
3956 KB |
Output is correct |
7 |
Correct |
4 ms |
2648 KB |
Output is correct |
8 |
Correct |
33 ms |
3324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
3664 KB |
Output is correct |
2 |
Correct |
43 ms |
3880 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
39 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
3924 KB |
Output is correct |
2 |
Correct |
48 ms |
3924 KB |
Output is correct |
3 |
Correct |
9 ms |
2524 KB |
Output is correct |
4 |
Correct |
44 ms |
3928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
4168 KB |
Output is correct |
2 |
Correct |
91 ms |
4692 KB |
Output is correct |
3 |
Correct |
11 ms |
3028 KB |
Output is correct |
4 |
Correct |
35 ms |
4464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
4396 KB |
Output is correct |
2 |
Correct |
94 ms |
4672 KB |
Output is correct |
3 |
Correct |
39 ms |
4840 KB |
Output is correct |
4 |
Correct |
12 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
4688 KB |
Output is correct |
2 |
Correct |
65 ms |
4532 KB |
Output is correct |
3 |
Correct |
40 ms |
4944 KB |
Output is correct |
4 |
Correct |
12 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
4944 KB |
Output is correct |
2 |
Correct |
92 ms |
4468 KB |
Output is correct |
3 |
Correct |
23 ms |
3928 KB |
Output is correct |
4 |
Correct |
80 ms |
4428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
4688 KB |
Output is correct |
2 |
Correct |
90 ms |
4836 KB |
Output is correct |
3 |
Correct |
151 ms |
5196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
5684 KB |
Output is correct |