#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,i,a[100005],x,y,s[400005];
char type;
void update(ll node,ll l,ll r,ll L,ll R,ll val){
if(l>R || r<L) return;
if(l>=L && r<=R){
s[node]+=val;
return;
}
ll mid=(r+l)/2;
update(node*2,l,mid,L,R,val);
update(node*2+1,mid+1,r,L,R,val);
}
ll get(ll node,ll l,ll r,ll loc){
if(l==r) return s[node];
ll mid=(r+l)/2,z;
if(loc<=mid) z=get(node*2,l,mid,loc);
else z=get(node*2+1,mid+1,r,loc);
return s[node]+z;
}
ll find(ll x,ll y){
ll l,r,mid;
if(get(1,1,n,n)<x || get(1,1,n,1)>y) return 0;
l=1;
r=n;
while(r-l>1){
mid=(r+l+1)/2;
if(get(1,1,n,mid)>=x) r=mid;
else l=mid;
}
if(get(1,1,n,l)>=x) x=l;
else x=r;
l=1;
r=n;
while(r-l>1){
mid=(r+l+1)/2;
if(get(1,1,n,mid)>y) r=mid;
else l=mid;
}
if(get(1,1,n,r)<=y) y=r;
else y=l;
return y-x+1;
}
void change(ll c,ll h){
if(get(1,1,n,n)<h) return;
ll l,r,mid,strt,val1,val2,x,y;
l=1;
r=n;
while(r-l>1){
mid=(r+l+1)/2;
if(get(1,1,n,mid)>=h) r=mid;
else l=mid;
}
if(get(1,1,n,l)>=h) strt=l;
else strt=r;
if(strt+c-1>=n){
update(1,1,n,strt,n,1);
return;
}
val1=get(1,1,n,strt);
val2=get(1,1,n,strt+c-1);
l=1;
r=n;
while(r-l>1){
mid=(r+l+1)/2;
if(get(1,1,n,mid)>=val2) r=mid;
else l=mid;
}
if(get(1,1,n,l)>=val2) x=l;
else x=r;
l=1;
r=n;
while(r-l>1){
mid=(r+l+1)/2;
if(get(1,1,n,mid)>val2) r=mid;
else l=mid;
}
if(get(1,1,n,r)<=val2) y=r;
else y=l;
if(val1!=val2){
update(1,1,n,strt,x-1,1);
c=c-(x-strt);
update(1,1,n,y-c+1,y,1);
}
else update(1,1,n,y-c+1,y,1);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(i=1;i<=n;i++) cin >> a[i];
sort(a+1,a+n+1);
for(i=1;i<=n;i++) update(1,1,n,i,i,a[i]);
while(m--){
cin >> type >> x >> y;
if(type=='C'){
cout << find(x,y) << "\n";
}
else{
change(x,y);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
4172 KB |
Output is correct |
2 |
Correct |
266 ms |
5020 KB |
Output is correct |
3 |
Correct |
84 ms |
4800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
114 ms |
1784 KB |
Output is correct |
6 |
Correct |
127 ms |
1616 KB |
Output is correct |
7 |
Correct |
8 ms |
600 KB |
Output is correct |
8 |
Correct |
32 ms |
1292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
1620 KB |
Output is correct |
2 |
Correct |
122 ms |
1992 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
58 ms |
1460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
1872 KB |
Output is correct |
2 |
Correct |
140 ms |
1892 KB |
Output is correct |
3 |
Correct |
14 ms |
604 KB |
Output is correct |
4 |
Correct |
125 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
4316 KB |
Output is correct |
2 |
Correct |
246 ms |
4528 KB |
Output is correct |
3 |
Correct |
35 ms |
3160 KB |
Output is correct |
4 |
Correct |
57 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
4360 KB |
Output is correct |
2 |
Correct |
238 ms |
4376 KB |
Output is correct |
3 |
Correct |
70 ms |
4712 KB |
Output is correct |
4 |
Correct |
35 ms |
1496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
4332 KB |
Output is correct |
2 |
Correct |
150 ms |
4648 KB |
Output is correct |
3 |
Correct |
73 ms |
4828 KB |
Output is correct |
4 |
Correct |
41 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
5024 KB |
Output is correct |
2 |
Correct |
254 ms |
4636 KB |
Output is correct |
3 |
Correct |
52 ms |
4252 KB |
Output is correct |
4 |
Correct |
107 ms |
4428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
199 ms |
4844 KB |
Output is correct |
2 |
Correct |
259 ms |
4752 KB |
Output is correct |
3 |
Correct |
308 ms |
5152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
5340 KB |
Output is correct |