//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,q;
int arr[100005];
struct node{
int s,e,m;
int val=0;
node *l,*r;
node (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void update(int i,int j,int k){
if (s==i && e==j) val+=k;
else if (j<=m) l->update(i,j,k);
else if (m<i) r->update(i,j,k);
else l->update(i,m,k),r->update(m+1,j,k);
}
int query(int i){
if (s==e) return val;
else if (i<=m) return val+l->query(i);
else return val+r->query(i);
}
} *root=new node(0,100005);
int find(int val){
int lo=-1,hi=n,mi;
while (hi-lo>1){
mi=lo+hi>>1;
if (root->query(mi)<=val) lo=mi;
else hi=mi;
}
return lo;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>q;
rep(x,0,n) cin>>arr[x];
sort(arr,arr+n);
rep(x,0,n) root->update(x,x,arr[x]);
char c;
int a,b;
while (q--){
cin>>c;
if (c=='F'){
cin>>a>>b;
int l=find(b-1)+1;
if (l+a>=n){
if (l<=n-1) root->update(l,n-1,1);
}
else{
int r=l+a-1;
int l2=find(root->query(r)-1)+1;
int r2=find(root->query(r));
if (l!=l2) root->update(l,l2-1,1);
//cerr<<l<<" "<<r<<endl;
//cerr<<l2<<" "<<r2<<endl;
//cerr<<l2+r2-r<<" "<<r2<<endl;
root->update(l2+r2-r,r2,1);
}
//rep(x,0,n) cout<<root->query(x)<<" "; cout<<endl;
}
else{
cin>>a>>b;
cout<<find(b)-find(a-1)<<endl;
}
}
}
Compilation message
grow.cpp: In constructor 'node::node(int, int)':
grow.cpp:37:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | s=_s,e=_e,m=s+e>>1;
| ~^~
grow.cpp: In function 'int find(int)':
grow.cpp:62:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | mi=lo+hi>>1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
365 ms |
10928 KB |
Output is correct |
2 |
Correct |
530 ms |
11804 KB |
Output is correct |
3 |
Correct |
196 ms |
11780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
9708 KB |
Output is correct |
2 |
Correct |
18 ms |
9708 KB |
Output is correct |
3 |
Correct |
17 ms |
9708 KB |
Output is correct |
4 |
Correct |
18 ms |
9708 KB |
Output is correct |
5 |
Correct |
196 ms |
9984 KB |
Output is correct |
6 |
Correct |
239 ms |
11024 KB |
Output is correct |
7 |
Correct |
25 ms |
9836 KB |
Output is correct |
8 |
Correct |
178 ms |
10676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
250 ms |
11112 KB |
Output is correct |
2 |
Correct |
240 ms |
11244 KB |
Output is correct |
3 |
Correct |
18 ms |
9836 KB |
Output is correct |
4 |
Correct |
195 ms |
10732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
257 ms |
11336 KB |
Output is correct |
2 |
Correct |
267 ms |
11140 KB |
Output is correct |
3 |
Correct |
40 ms |
9936 KB |
Output is correct |
4 |
Correct |
243 ms |
11268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
358 ms |
11236 KB |
Output is correct |
2 |
Correct |
487 ms |
11496 KB |
Output is correct |
3 |
Correct |
76 ms |
10368 KB |
Output is correct |
4 |
Correct |
174 ms |
11500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
465 ms |
11212 KB |
Output is correct |
2 |
Correct |
535 ms |
11628 KB |
Output is correct |
3 |
Correct |
199 ms |
11748 KB |
Output is correct |
4 |
Correct |
71 ms |
10220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
371 ms |
11216 KB |
Output is correct |
2 |
Correct |
386 ms |
11528 KB |
Output is correct |
3 |
Correct |
210 ms |
11788 KB |
Output is correct |
4 |
Correct |
75 ms |
10220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
561 ms |
11808 KB |
Output is correct |
2 |
Correct |
522 ms |
11416 KB |
Output is correct |
3 |
Correct |
108 ms |
10924 KB |
Output is correct |
4 |
Correct |
383 ms |
11244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
487 ms |
11684 KB |
Output is correct |
2 |
Correct |
511 ms |
11840 KB |
Output is correct |
3 |
Correct |
638 ms |
11992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
589 ms |
12472 KB |
Output is correct |