#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n , q;
int a[N];
int tree[4 * N] , lazy[4 * N];
void build(int x , int l , int r) {
if(l == r) {
tree[x] = a[l];
return;
}
int mid = l + r >> 1;
build(x << 1 , l , mid);
build(x << 1 | 1 , mid + 1 , r);
// tree[x] = max(tree[x << 1] , tree[x << 1 | 1]);
}
void down(int x , int l , int r) {
int val = lazy[x];
if(val == 0) {
return;
}
if(l == r) tree[x] += val;
else {
lazy[x << 1] += val;
lazy[x << 1 | 1] += val;
}
lazy[x] = 0;
}
void update(int x , int l , int r , int L , int R) {
down(x , l , r);
if(l > R || r < L) {
return;
}
if(L <= l && r <= R) {
lazy[x]++;
down(x , l , r);
return;
}
int mid = l + r >> 1;
update(x << 1 , l , mid , L , R);
update(x << 1 | 1 , mid + 1 , r , L , R);
// tree[x] = max(tree[x << 1] , tree[x << 1 | 1]);
}
int get(int x , int l , int r , int pos) {
down(x , l , r);
if(l > pos || r < pos) {
return -1;
}
if(l == r) {
return tree[x];
}
int mid = l + r >> 1;
return max(get(x << 1 , l , mid , pos) , get(x << 1 | 1 , mid + 1 , r , pos));
}
int get(int pos) {
return get(1 , 1 , n , pos);
}
int binary_search_1(int x) {
int l = 1;
int r = n;
int ans = -1;
while(l - r <= 0) {
int mid = l + r >> 1;
if(get(mid) >= x) {
r = mid - 1;
ans = mid;
}
else l = mid + 1;
}
return ans;
}
int binary_search_2(int x) {
int l = 1;
int r = n;
int ans = -1;
while(l - r <= 0) {
int mid = l + r >> 1;
if(get(mid) <= x) {
l = mid + 1;
ans = mid;
}
else r = mid - 1;
}
return ans;
}
main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen("solve.inp" , "r")) {
freopen("solve.inp" , "r" , stdin);
freopen("solve.out" , "w" , stdout);
}
cin >> n >> q;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1 , a + 1 + n);
build(1 , 1 , n);
while(q--) {
char c;
cin >> c;
if(c == 'F') {
int u , v;
cin >> u >> v;
int l1 = binary_search_1(v);
if(l1 == -1) continue;
int r1 = min(n , l1 + u - 1);
int l2 = binary_search_1(get(r1));
int r2 = binary_search_2(get(r1));
if(l1 <= l2 - 1) {
update(1 , 1 , n , l1 , l2 - 1);
}
int tmp = max(l2 , r2 - u + (l2 - l1) + 1);
if(tmp <= r2) {
update(1 , 1 , n , tmp , r2);
}
}
else {
int u , v;
cin >> u >> v;
int mn = binary_search_1(u);
int mx = binary_search_2(v);
if(mn == -1 or mx == -1) {
cout << 0 << '\n';
}
else cout << mx - mn + 1 << '\n';
}
}
return 0x0;
}
Compilation message
grow.cpp: In function 'void build(int, int, int)':
grow.cpp:15:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'void update(int, int, int, int, int)':
grow.cpp:44:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int get(int, int, int, int)':
grow.cpp:58:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int binary_search_1(int)':
grow.cpp:71:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int binary_search_2(int)':
grow.cpp:86:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: At global scope:
grow.cpp:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
96 | main() {
| ^~~~
grow.cpp: In function 'int main()':
grow.cpp:100:10: 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:10: 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 |
504 ms |
3528 KB |
Output is correct |
2 |
Correct |
730 ms |
3644 KB |
Output is correct |
3 |
Correct |
747 ms |
3352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2396 KB |
Output is correct |
5 |
Correct |
261 ms |
2628 KB |
Output is correct |
6 |
Correct |
347 ms |
2860 KB |
Output is correct |
7 |
Correct |
22 ms |
2392 KB |
Output is correct |
8 |
Correct |
233 ms |
3004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
3148 KB |
Output is correct |
2 |
Correct |
342 ms |
2788 KB |
Output is correct |
3 |
Correct |
5 ms |
2392 KB |
Output is correct |
4 |
Correct |
269 ms |
2780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
2900 KB |
Output is correct |
2 |
Correct |
394 ms |
2900 KB |
Output is correct |
3 |
Correct |
44 ms |
2392 KB |
Output is correct |
4 |
Correct |
379 ms |
3144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
532 ms |
2904 KB |
Output is correct |
2 |
Correct |
708 ms |
3664 KB |
Output is correct |
3 |
Correct |
86 ms |
2648 KB |
Output is correct |
4 |
Correct |
577 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
588 ms |
3420 KB |
Output is correct |
2 |
Correct |
674 ms |
3408 KB |
Output is correct |
3 |
Correct |
731 ms |
3308 KB |
Output is correct |
4 |
Correct |
92 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
552 ms |
3408 KB |
Output is correct |
2 |
Correct |
504 ms |
3432 KB |
Output is correct |
3 |
Correct |
720 ms |
3348 KB |
Output is correct |
4 |
Correct |
92 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
724 ms |
3408 KB |
Output is correct |
2 |
Correct |
713 ms |
3748 KB |
Output is correct |
3 |
Correct |
97 ms |
3424 KB |
Output is correct |
4 |
Correct |
567 ms |
3488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
624 ms |
3464 KB |
Output is correct |
2 |
Correct |
697 ms |
3408 KB |
Output is correct |
3 |
Correct |
965 ms |
3664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
613 ms |
3752 KB |
Output is correct |