求助求助RE#6-#10
查看原帖
求助求助RE#6-#10
682780
ykc1146187241楼主2024/2/26 19:54
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000005;
const int SQN=10005;
int a[MAXN]={};
int st[SQN]={};
int ed[SQN]={};
int belong[MAXN]={};
int sz[SQN]={};
int n,q;
int lazy[SQN]={};
vector<int> ms[SQN]={};
void update(int index){
    for(int i=0;i<sz[index];i++){

        ms[index][i]=a[st[index]+i];
    }
    sort(ms[index].begin(),ms[index].end());
}
void init(int n){
    int sq=sqrt(n);
    for(int i=1;i<=sq;i++){
        st[i]=n/sq*(i-1)+1;
        ed[i]=n/sq*i;
    }
    ed[sq]=n;
    for(int i=1;i<=sq;i++){
        for(int j=st[i];j<=ed[i];j++){
            belong[j]=i;
        }
    }
    for(int i=1;i<=n;i++){
        sz[i]=ed[i]-st[i]+1;
    }
}

void modify(int l,int r,int w){

    if(belong[l]==belong[r]){
        for(int i=l;i<=r;i++){
            a[i]+=w;
        }
        update(belong[l]);
        return ;
    }
    for(int i=l;i<=ed[belong[l]];i++){
        a[i]+=w;
    }
    for(int i=st[belong[r]];i<=r;i++){
        a[i]+=w;
    }
    update(belong[l]);
    update(belong[r]);
//    cout<<l<<" "<<r<<" "<<w<<belong[l]+1<<" "<<belong[r]<<endl;
    for(int i=belong[l]+1;i<belong[r];i++){
        lazy[i]+=w;
    }
}
void query(int l,int r,int c){
    int tot=0;
    if(belong[l]==belong[r]){
        for(int i=l;i<=r;i++){
            if(a[i]+lazy[belong[l]]>=c){
                tot++;
            }
        }
        cout<<tot<<endl;
        return ;
    }
    for(int i=l;i<=ed[belong[l]];i++){
        if(a[i]+lazy[belong[l]]>=c){
//            cout<<a[i]<<" "<<lazy[belong[l]]<<endl;
            tot++;
        }
    }
    for(int i=st[belong[r]];i<=r;i++){
        if(a[i]+lazy[belong[r]]>=c){
//            cout<<a[i]<<" "<<lazy[belong[r]]<<endl;
            tot++;
        }
    }
    for(int i=belong[l]+1;i<belong[r];i++){
        int temp= ms[i].end()-std::lower_bound(ms[i].begin(), ms[i].end(),c-lazy[i]);
        tot+=temp;
    }
    cout<<tot<<endl;
//    for(int i=1;i<=10;i++){
//        for(auto item:ms[i]){
//            cout<<item<<" ";
//        }
//        cout<<lazy[i];
//        cout<<endl;
//    }
//    for(int i=l;i<=r;i++){
//        cout<<a[i]<<" ";
//    }
};
int main(){
    cin>>n>>q;
    int sq=sqrt(n);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
//    for(int i=1;i<=sq;i++){
//        ms[i].resize(sq);
//    }
    init(n);
    for (int i = 1; i <= sq; ++i) {
        for (int j = st[i]; j <= ed[i]; ++j) {
            ms[i].push_back(a[j]);
        }
    }
    for (int i = 1; i <= sq; ++i)
        sort(ms[i].begin(), ms[i].end());
    for(int i=0;i<q;i++){
        char opcode;
        int l,r,w;
        cin>>opcode>>l>>r>>w;
        if(opcode=='M'){
           modify(l,r,w);
        }
        else{
           query(l,r,w);
        }
    }
}


2024/2/26 19:54
加载中...