Eric_jx @ 2023-07-06 11:21:45
#include<bits/stdc++.h>
using namespace std;
int block[1000005],bl[1000005],br[1000005],b[1000005],a[1000005],lazy[1000005];
int main(){
int n,q;
cin>>n>>q;
int size=sqrt(n);
int tot=n/size;
if(n%size){
tot++;
}
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
block[i]=(i-1)/size+1;
}
for(int i=1;i<=tot;i++){
bl[i]=(i-1)*size+1;
br[i]=i*size;
}
br[tot]=n;
for(int i=1;i<=tot;i++){
sort(b+bl[i],b+br[i]+1);
}
while(q--){
char mm;
int l,r,x;
cin>>mm;
scanf("%d%d%d",&l,&r,&x);
if(mm=='M'){
if(block[l]==block[r]){
for(int i=l;i<=r;i++){
a[i]+=x;
}
for(int i=bl[block[l]];i<=br[block[l]];i++){
b[i]=a[i];
}
sort(b+bl[block[l]],b+br[block[l]]+1);
continue;
}
for(int i=l;i<=br[block[l]];i++){
a[i]+=x;
}
for(int i=bl[block[l]];i<=br[block[l]];i++){
b[i]=a[i];
}
sort(b+bl[block[l]],b+br[block[l]]+1);
for(int i=block[l]+1;i<=block[r]-1;i++){
lazy[i]+=x;
}
for(int i=bl[block[r]];i<=r;i++){
a[i]+=x;
}
for(int i=bl[block[r]];i<=br[block[r]];i++){
b[i]=a[i];
}
sort(b+bl[block[r]],b+br[block[r]]+1);
}
else{
int ans=0;
if(block[l]==block[r]){
for(int i=l;i<=r;i++){
if(a[i]+lazy[block[l]]>=x){
ans++;
}
}
cout<<ans<<"\n";
continue;
}
for(int i=l;i<=br[block[l]];i++){
if(a[i]+lazy[block[l]]>=x){
ans++;
}
}
for(int i=block[l]+1;i<=block[r]-1;i++){
int ll=bl[i],rr=br[i],ansid=-1;
while(ll<=rr){
int mid=(ll+rr)>>1;
if(b[mid]+lazy[i]>=x){
ansid=mid;
rr=mid-1;
}
else{
ll=mid+1;
}
}
ans+=br[i]-ansid+1;
}
for(int i=bl[block[r]];i<=r;i++){
if(a[i]+lazy[block[r]]>=x){
ans++;
}
}
cout<<ans<<"\n";
}
}
return 0;
}
by Eric_jx @ 2023-07-06 11:25:57
草,找到问题了,此贴结。
by zzzzzzpppyy @ 2023-07-25 19:33:40
@jinxiang1122 哪里有问题呀
by Eric_jx @ 2023-07-25 19:42:57
@zzzzzzpppyy
二分后忘了判断
if(ansid!=-1){
ans+=br[i]-ansid+1;
}
by Lysea @ 2023-07-29 22:46:44
感谢您的帖子,不然我这个错估计得调几个小时。