模拟题(1)

· · 题解

::::info[题目] 我自己重新搞的链接:https://www.luogu.com.cn/problem/U492916 ::::

::::info[思路]

按照贪心的思想,我们容易得到:

根据数据范围我们很容易就能想到是分块(但是正解好像是线段树上二分耶)。

所以我们可以用我之前说过的块状双端队列来实现。

然后每回统计每个块的和,每回询问 k_{min} 时只需要遍历每个块然后在遍历一个块中的元素即可(这时候就别用二分了,否则时间就会容易搞成 O(\sqrt n\log n)!)。

总时间复杂度为 O((n+q)\sqrt n)。

代码

#include<cstring>
#include<iostream>
#include<deque>
using namespace std;
namespace Fastio{
    struct Reader{
        template<typename T>Reader&operator>>(T&x){
            x=0;short f=1;char c=getchar();
            while(c<'0'||c>'9'){if(c=='-')f=-f;c=getchar();}
            while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
            x*=f;return*this;
        }Reader&operator>>(double&x){
            x=0;double t=0;short f=1,s=0;char c=getchar();
            while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}
            while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();
            else{x*=f;return*this;}
            while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();
            while(s--)t/=10.0;x=(x+t)*f;
            return*this;
        }Reader&operator>>(long double&x){
            x=0;long double t=0;short f=1,s=0;char c=getchar();
            while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}
            while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();
            else{x*=f;return*this;}
            while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();
            while(s--)t/=10.0;x=(x+t)*f;
            return*this;
        }Reader&operator>>(__float128&x){
            x=0;__float128 t=0;short f=1,s=0;char c=getchar();
            while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}
            while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();
            else{x*=f;return*this;}
            while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();
            while(s--)t/=10.0;x=(x+t)*f;
            return*this;
        }Reader&operator>>(char&c){
            c=getchar();
            while(c==' '||c=='\n'||c=='\r')c=getchar();
            return*this;
        }Reader&operator>>(char*str){
            int len=0;char c=getchar();
            while(c==' '||c=='\n'||c=='\r')c=getchar();
            while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();
            str[len]='\0';
            return*this;
        }Reader&operator>>(string&str){
            str.clear();char c=getchar();
            while(c==' '||c=='\n'||c=='\r')c=getchar();
            while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();
            return*this;
        }
        Reader(){}
    }cin;
    const char endl='\n';
    struct Writer{
        const int Setprecision=6;
        typedef int mxdouble;
        template<typename T>Writer&operator<<(T x){
            if(x==0){putchar('0');return*this;}
            if(x<0)putchar('-'),x=-x;
            static short sta[40];
            short top=0;
            while(x>0)sta[++top]=x%10,x/=10;
            while(top>0)putchar(sta[top]+'0'),top--;
            return*this;
        }Writer&operator<<(double x){
            if(x<0)putchar('-'),x=-x;
            mxdouble _=x;
            x-=(double)_;
            static short sta[40];
            short top=0;while(_>0)sta[++top]=_%10,_/=10;
            if(top==0)putchar('0');
            while(top>0)putchar(sta[top]+'0'),top--;
            putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;
            while(_>0)sta[++top]=_%10,_/=10;
            for(int i=0;i<Setprecision-top;i++)putchar('0');
            while(top>0)putchar(sta[top]+'0'),top--;
            return*this;
        }Writer&operator<<(long double x){
            if(x<0)putchar('-'),x=-x;
            mxdouble _=x;
            x-=(long double)_;
            static short sta[40];
            short top=0;while(_>0)sta[++top]=_%10,_/=10;
            if(top==0)putchar('0');
            while(top>0)putchar(sta[top]+'0'),top--;
            putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;
            while(_>0)sta[++top]=_%10,_/=10;
            for(int i=0;i<Setprecision-top;i++)putchar('0');
            while(top>0)putchar(sta[top]+'0'),top--;
            return*this;
        }Writer&operator<<(__float128 x){
            if(x<0)putchar('-'),x=-x;
            mxdouble _=x;
            x-=(__float128)_;
            static short sta[40];
            short top=0;while(_>0)sta[++top]=_%10,_/=10;
            if(top==0)putchar('0');
            while(top>0)putchar(sta[top]+'0'),top--;
            putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;
            while(_>0)sta[++top]=_%10,_/=10;
            for(int i=0;i<Setprecision-top;i++)putchar('0');
            while(top>0)putchar(sta[top]+'0'),top--;
            return*this;
        }Writer&operator<<(char c){putchar(c);return*this;}
        Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
        Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
        Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}
        Writer(){}
    }cout;
}using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
//别管了,快读。
#define int long long
int bl[100010],mo[100010];
const int S=350;
const int num=S;
int N,Q,input[100010],fu_sum,fu_n,zheng_sum;
class square{
    public :deque<int>a[S];
    public :int sum[S];
    public :int n,line;
    public :inline int size(){return n;}
    public :inline bool empty(){return !n;}
    public :inline int operator[](int pos){return a[bl[pos]][mo[pos]];}
    public :inline void push_back(int x){
        sum[line]+=x,a[line].push_back(x),n++;
        if(a[line].size()==num)line++;
    };
    public :inline void insert(int pos,int x){
        sum[bl[pos]]+=x,a[bl[pos]].insert(a[bl[pos]].begin()+mo[pos],x),++n;
        for(register int i=bl[pos];i<line;i++){
            sum[i+1]+=a[i].back(),a[i+1].push_front(a[i].back());
            sum[i]-=a[i].back(),a[i].pop_back();
        }
        if(a[line].size()==num)line++;
    };
    public :inline void erase(int pos){
        if(a[line].empty())line--;
        sum[bl[pos]]-=a[bl[pos]][mo[pos]],a[bl[pos]].erase(a[bl[pos]].begin()+mo[pos]),n--;
        for(register int i=bl[pos];i<line;i++){
            sum[i]+=a[i+1].front(),a[i].push_back(a[i+1].front());
            sum[i+1]-=a[i+1].front(),a[i+1].pop_front();
        }
    };
};
class ordered_square{
    private:square a;
    private:int lef,rig;
    public :inline int size(){return a.n;}
    public :inline int operator[](int pos){return a[pos];}
    public :inline void insert(int x){
        if(a.empty()||x>=a[a.size()-1]){a.push_back(x);return;}
        if(x<=a[0]){a.insert(0,x);return;}
        lef=0,rig=a.size()-1;
        while(lef<rig){
            if(x>a[(lef+rig)>>1])lef=(lef+rig+2)>>1;
            else rig=(lef+rig)>>1;
        }
        a.insert(lef,x);
    }
    public :inline void erase(int x){
        lef=0,rig=a.size()-1;
        while(lef<rig){
            if(x>a[(lef+rig)>>1])lef=(lef+rig+2)>>1;
            else rig=(lef+rig)>>1;
        }
        if(a[lef]==x)a.erase(lef);
    }
    public :inline int ask(int x){
        int he=0;int i=0;
        for(;i<=a.line;++i){
            if(he+a.sum[i]>x){break;}
            else he+=a.sum[i];
        }
        for(i*=num;;++i){
            he+=a[i];
            if(he>x)break;
        }
        return a.n-i;
    }
}a;
int x,y;
signed main()
{
    cin>>N>>Q;
    for(int i=0;i<num;++i)mo[i]=i;
    for(int i=num;i<N;++i)bl[i]=bl[i-num]+1,mo[i]=mo[i-num];
    for(int i=0;i<N;++i){
        cin>>input[i];
        if(input[i]<=0)fu_sum-=input[i],++fu_n;
        else a.insert(input[i]),zheng_sum+=input[i];
    }

    while(Q--){
        cin>>x>>y;--x;
        if(input[x]<=0){
            fu_sum+=input[x],--fu_n;
        }else a.erase(input[x]),zheng_sum-=input[x];
        input[x]=y;
        if(y<=0)fu_sum-=y,++fu_n;
        else a.insert(y),zheng_sum+=input[x];
        if(zheng_sum<=fu_sum)cout<<N-fu_n+1<<'\n';
        else cout<<N-fu_n-a.ask(fu_sum)+1<<'\n';
    }
    return 0;
}

::::