40pts

P2801 教主的魔法

Shadow_Lord @ 2023-03-16 18:39:38

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
inline int read()
{
    int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return s*w;
}
int n,a[N],b[N],l[N],r[N],belong[N],block,num,q,add[N];
char xx;
void build()
{
    block=int(sqrt(n));
    num=n/block;
    if(n%block) num++;
    for(int i=1;i<num;i++)
    {
        l[i]=(i-1)*block+1;
        r[i]=i*block;
        sort(b+l[i],b+r[i]+1);
    }
    l[num]=(num-1)*block+1;
    r[num]=n;
    sort(b+l[num],b+r[num]+1);
    for(int i=1;i<=n;i++)
    {
        belong[i]=(i-1)/block+1;
    }
}
void reset(int x)
{
    for(int i=l[x];i<=r[x];i++) b[i]=a[i];
    sort(b+l[x],b+r[x]+1);
}
void update(int left,int right,int v)
{
    if(belong[left]==belong[right])
    {
        for(int i=left;i<=right;i++)a[i]+=v;
        reset(belong[left]);
        return ;
    }
    for(int i=left;i<=r[belong[left]];i++)a[i]+=v;
    reset(belong[left]);
    for(int i=belong[left]+1;i<belong[right];i++)add[i]+=v;
    for(int i=l[belong[right]];i<=right;i++)a[i]+=v;
    reset(belong[right]);
}
int fin(int x,int v)
{
    int left=l[x],right=r[x];
    if(a[r[x]]<v)return 0;
    while(left<right)
    {
        int mid=(left+right)>>1;
        if(a[mid]>=v)
        {
            right=mid;
        }
        else left=mid+1;
    }
    right=(right+left)>>1;
    return r[x]-right+1;
}
int ask(int left,int right,int v)
{
    int sum=0;
    if(belong[left]==belong[right])
    {
        for(int i=left;i<=right;i++)
        {
            if(a[i]+add[belong[i]]>=v)sum++;
        }
        return sum;
    }
    for(int i=left;i<=r[belong[left]];i++)if(a[i]+add[belong[i]]>=v)sum++;
    for(int i=belong[left]+1;i<belong[right];i++)sum+=fin(i,v-add[i]);
    for(int i=l[belong[right]];i<=right;i++)if(a[i]+add[belong[i]]>=v)sum++;
    return sum;
}
int main()
{
    n=read();q=read();
    for(int i=1;i<=n;i++)a[i]=read();
    build();
    while(q--)
    {
        cin>>xx;
        if(xx=='M')
        {
            update(read(),read(),read());
        }
        else printf("%d\n",ask(read(),read(),read()));
    }
    return 0;
}

by 5k_sync_closer @ 2023-03-16 18:44:21

@wanye811 update(read(),read(),read()); 这种东西调用顺序不固定的。


by Wangzj512 @ 2023-03-16 18:48:08

也不能说不固定,一般是从右往左


by Shadow_Lord @ 2023-03-16 18:49:13

@5k_sync_closer 改了还是40分,这里好像没影响


|