线段树二分出现内存问题求调
查看原帖
线段树二分出现内存问题求调
915727
hao_zi6366楼主2024/10/20 21:14
#include<bits/stdc++.h>
#define lson (p<<1)
#define rson ((p<<1)+1)
#define mid ((l+r)>>1)
#define N 200005
typedef long long ll;
int n,q;
int a[N];
ll sum[N<<2],lazyt[N<<2];
void pushup(int p){
    sum[p]=sum[lson]+sum[rson];
}
void build(int p,int l,int r){
    if(l==r){
        sum[p]=a[l];
        return ;
    }
    build(lson,l,mid);
    build(rson,mid+1,r);
    pushup(p);
}
void add(int p,int l,int r,int k){
    lazyt[p]+=k;
    sum[p]+=ll(r-l+1)*k;
}
void pushdown(int p,int l,int r){
    if(lazyt[p]==0)return ;
    add(lson,l,mid,lazyt[p]);
    add(rson,mid+1,r,lazyt[p]);
    lazyt[p]=0;
}
ll query_sum(int p,int l,int r,int x,int y){
    if(x<=l && r<=y){
        return sum[p];
    }
    pushdown(p,l,r);
    ll res=0;
    if(x<=mid)res+=query_sum(lson,l,mid,x,y);
    if(y>mid)res+=query_sum(rson,mid+1,r,x,y);
    return res;
}
void update(int p,int l,int r,int x,int y,int k){
    if(x<=l && r<=y){
        add(p,l,r,k);
        return ;
    }
    pushdown(p,l,r);
    if(x<=mid)update(lson,l,mid,x,y,k);
    if(y>mid)update(rson,mid+1,r,x,y,k);
    pushup(p);
}
ll db_sum(int x){
    ll res=query_sum(1,1,n,1,n);
    return (1<<x)*res-res;
}
int main(){
    std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
    
    ll w;
    std::cin>>n>>q>>w;
    for(int i=1;i<=n;i++){
        std::cin>>a[i];
    }
    build(1,1,n);
    while(q--){
        int x,y,d;
        std::cin>>x>>y>>d;
        update(1,1,n,x,y,d);
        int ce=0,wie=0;
        int l=0,r=45;
        while(l<=r){
            int mmd=(l+r)>>1;
            if(db_sum(mmd)<=w){
                ce=mmd;
                l=mmd+1;
            }
            else{
                r=mmd-1;
            }
        }
        l=0,r=n+1;
        ll smm=w-db_sum(ce);
        while(l<=r){
            int mmd=(l+r)>>1;
            if(query_sum(1,1,n,1,mmd)*(1<<(ce-1))<=smm){
                wie=mmd;
                l=mmd+1;
            }else{
                r=mmd-1;
            }
        }
        std::cout<<ce*n+wie<<"\n";
    }
	return 0;
}
2024/10/20 21:14
加载中...