#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define vi vector<int>
#define vp32 vector<p32>
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define MOD %1000000007
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//never guess
//never debug without reviewing code
//never try adding ones or substracting them
//only step by step debug when necessay
struct segtree
{
int size = 1, lazy[(int)1e6] = {0};
segtree(int n){while(size<n) size*=2;}
void upd(int l, int r){
upd(l,r,0,0,size);
}
void upd(int l, int r, int x, int lx, int rx){
if(lx>=r||rx<=l) return;
if(lx>=l&&rx<=r){
lazy[x]++;
return;
}
int m = (lx+rx)/2;
upd(l,r,2*x+1,lx,m);
upd(l,r,2*x+2,m,rx);
}
int get(int i){
return get(i,0,0,size);
}
int get(int i, int x, int l, int r){
if(r-l==1) return lazy[x];
int m = (r+l)/2;
int res;
if(i<m) res = get(i,2*x+1,l,m);
else res = get(i,2*x+2,m,r);
return res+lazy[x];
}
};
int main()
{
fast_cin();
#ifndef ONLINE_JUDGE
#ifdef _WIN32
freopen("input.in", "r", stdin);
freopen("input.out", "w", stdout);
#endif
#endif
int n,m; cin>>n>>m;
int arr [n];
for(int & sub : arr){
cin>>sub;
}
segtree sgt(n);
sort(arr,arr+n);
for (size_t i = 0; i < m; i++)
{
char t; cin>>t;
if(t=='F'){
int h,c; cin>>c>>h;
//binary search the element with at least h
int l = -1, r = n;
while(r-l>1){
int mid = (r+l)/2;
if(sgt.get(mid)+arr[mid]<h) l = mid;
else r = mid;
}
if(r+c-1>n-1) c = n-r;
int last_tree = r+c-1;
//binary search the index with value strictly less than the value of r
int la = -1, ra = n;
while(ra-la>1){
int mid = (ra+la)/2;
if(sgt.get(mid)+arr[mid]>=sgt.get(last_tree)+arr[last_tree]) ra = mid;
else la = mid;
}
if(la>=r) sgt.upd(r,la + 1);
int done = la+1-r;
//binary search largest index with value not greater than r;
la = 0, ra = n;
while(ra-la>1){
int mid = (ra+la)/2;
if(sgt.get(mid)+arr[mid]>sgt.get(last_tree)+arr[last_tree]) ra = mid;
else la = mid;
}
sgt.upd(la+1+done-c,la+1);
}
else{
int mn, mx; cin>>mn>>mx;
int l = -1, r = n;
while(r-l>1){
int mid = (r+l)/2;
if(arr[mid]+sgt.get(mid)<mn) l = mid;
else r = mid;
}
int p = r;
l = -1, r = n;
while(r-l>1){
int mid = (r+l)/2;
if(arr[mid]+sgt.get(mid)<=mx) l = mid;
else r = mid;
}
cout<<r-p<<'\n';
}
}
return 0;
}
Compilation message
grow.cpp: In function 'int main()':
grow.cpp:73:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
73 | for (size_t i = 0; i < m; i++)
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
151 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4144 KB |
Output is correct |
2 |
Correct |
7 ms |
4188 KB |
Output is correct |
3 |
Correct |
5 ms |
4188 KB |
Output is correct |
4 |
Correct |
4 ms |
4188 KB |
Output is correct |
5 |
Correct |
90 ms |
5296 KB |
Output is correct |
6 |
Incorrect |
75 ms |
5460 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
4416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
146 ms |
4688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
220 ms |
4664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
144 ms |
4692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
4700 KB |
Output is correct |
2 |
Incorrect |
248 ms |
5968 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
188 ms |
4708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
5256 KB |
Output is correct |