95pts 求调
查看原帖
95pts 求调
600834
Skadiiiii楼主2024/10/21 12:56

WA了最后一个点,蒟蒻实在调不出来了

还有我这个复杂度应该是qlogn的吧,为什么能跑800ms+?
哪里的常数可以优化呢?谢谢dalao们

#include<bits/stdc++.h>
using namespace std;
int n,q;
long long w;
int a[200002];
struct point
{
    int l,r,add;
    long long sum;
}tr[200002*4];
void build(int id,int l,int r)
{
    tr[id].l=l,tr[id].r=r;
    if(l==r)
    {
        tr[id].sum=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
    tr[id].sum=tr[id<<1].sum+tr[id<<1|1].sum;
}
void pushdown(int id)
{
    if(!tr[id].add)return;
    tr[id<<1].add+=tr[id].add;
    tr[id<<1].sum+=1ll*tr[id].add*(tr[id<<1].r-tr[id<<1].l+1);
    tr[id<<1|1].add+=tr[id].add;
    tr[id<<1|1].sum+=1ll*tr[id].add*(tr[id<<1|1].r-tr[id<<1|1].l+1);
    tr[id].add=0;
}
void update(int id,int l,int r,int x)
{
    if(l<=tr[id].l&&tr[id].r<=r)
    {
        tr[id].sum+=1ll*x*(tr[id].r-tr[id].l+1);
        tr[id].add+=x;
        return;
    }
    pushdown(id);
    int mid=(tr[id].l+tr[id].r)>>1;
    if(l<=mid)update(id<<1,l,r,x);
    if(mid<r)update(id<<1|1,l,r,x);
    tr[id].sum=tr[id<<1].sum+tr[id<<1|1].sum;
}
long long solve(int id,long long t,long long f)
{
    if(tr[id].l==tr[id].r)
    {
        if(tr[id].sum*f<t)return 1;
        else return 0;
    }
    pushdown(id);
    long long res=0;
    if(tr[id<<1].sum*f<t)
    {
        res+=tr[id<<1].r-tr[id<<1].l+1;
        res+=solve(id<<1|1,t-tr[id<<1].sum*f,f);
    }
    else res+=solve(id<<1,t,f);
    return res;
}
int main()
{
    // freopen("wxyt2.in","r",stdin);
    // freopen("wxyt.out","w",stdout);
    scanf("%d%d%lld",&n,&q,&w);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    build(1,1,n);
    int l,r,d;
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d%d",&l,&r,&d);
        update(1,l,r,d);
        long long ans=0,t=w,f=1;
        while(tr[1].sum*f<t)t-=tr[1].sum*f,f<<=1,ans+=n;
        printf("%lld\n",ans+solve(1,t,f));
    }
    return 0;
}
2024/10/21 12:56
加载中...