Comments
+7
const int N=150000*3+10;
#define mid ((l+r)>>1)
#define ls (x<<1)
#define rs (x<<1|1)
int n,m;
struct node{
    int mi,cnt;
    int ans,ad;
}t[4*N];
int a[N],buc[N];
int st,lim;
void pushup(int x){
    t[x].mi=min(t[ls].mi,t[rs].mi);
    t[x].cnt=(t[ls].mi==t[x].mi?t[ls].cnt:0)+(t[rs].mi==t[x].mi?t[rs].cnt:0);
    t[x].ans=t[ls].ans+t[rs].ans;
}
void tag(int x,int c){
    t[x].mi+=c;
    t[x].ans=(t[x].mi==0)?t[x].cnt:0;
    t[x].ad+=c;
}
void pushdown(int x){
    if(t[x].ad){
        tag(ls,t[x].ad);tag(rs,t[x].ad);
        t[x].ad=0;
    }
}
void build(int x,int l,int r){
    if(l==r){
        t[x].cnt=1;t[x].ans=1;
        return;
    }
    build(ls,l,mid);
    build(rs,mid+1,r);
    pushup(x);
}
void upda(int x,int l,int r,int L,int R,int c){
    if(L<=l&&r<=R){
        tag(x,c);return;
    }
    pushdown(x);
    if(L<=mid) upda(ls,l,mid,L,R,c);
    if(mid<R) upda(rs,mid+1,r,L,R,c);
    pushup(x);
}
int query(int x,int l,int r,int L,int R){
    if(L<=l&&r<=R) return t[x].ans;
    pushdown(x);
    if(R<=mid) return query(ls,l,mid,L,R);
    if(mid<L) return query(rs,mid+1,r,L,R);
    return query(ls,l,mid,L,R)+query(rs,mid+1,r,L,R);
}
void chan(int x,int c){
    int k=x-buc[x]+1-(c>0);
    upda(1,1,lim,k,k,c);
    buc[x]+=c;
}
+6

Like this problem: https://www.luogu.com.cn/problem/P5324
With some thinking, the problem comes to the given one by rakkoon. Here's the editorial: https://www.luogu.com.cn/problem/solution/P5324

+7

Check the idea of Segment Tree Beats and maybe hopefully it'll help.