0pts TLE求调
查看原帖
0pts TLE求调
1153677
Treap_Kongzs楼主2024/10/20 23:42

RT

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
#define ll long long

ll arr[maxn];
ll buf[maxn];
struct node
{
    int l;
    int r;
    ll val;
    int lazy;
};
node sgt[maxn*4];
ll delta[maxn];

inline int lson(int pos)
{
    return pos<<1;
}

inline int rson(int pos)
{
    return (pos<<1)|1;
}

void pushup(int pos)
{
    sgt[pos].val=sgt[lson(pos)].val+sgt[rson(pos)].val;
}

void pushdown(int pos)
{
    sgt[lson(pos)].lazy+=sgt[pos].lazy;
    sgt[rson(pos)].lazy+=sgt[pos].lazy;
    sgt[lson(pos)].val+=sgt[pos].lazy*(sgt[lson(pos)].r-sgt[lson(pos)].l+1);
    sgt[rson(pos)].val+=sgt[pos].lazy*(sgt[rson(pos)].r-sgt[rson(pos)].l+1);
    sgt[pos].lazy=0;
}

void build(int pos,int l,int r)
{
    sgt[pos].l=l;
    sgt[pos].r=r;
    sgt[pos].lazy=0;
    if(l==r)
    {
        sgt[pos].val=arr[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson(pos),l,mid);
    build(rson(pos),mid+1,r);
    pushup(pos);
    return;
}

ll query(int pos,int l,int r)
{
    if(sgt[pos].l>=l&&sgt[pos].r<=r)
    {
        return sgt[pos].val;
    }
    pushdown(pos);
    ll ans=0;
    int mid=(sgt[pos].l+sgt[pos].r)>>1;
    if(l<=mid)ans+=query(lson(pos),l,r);
    if(r>mid)ans+=query(rson(pos),l,r);
    return ans;
}

void change(int pos,int l,int r,int num)
{
    if(sgt[pos].l>=l&&sgt[pos].r<=r)
    {
        sgt[pos].val+=num*(sgt[pos].r-sgt[pos].l+1);
        sgt[pos].lazy+=num;
        return;
    }
    else
    {
        pushdown(pos);
        int mid=(sgt[pos].l+sgt[pos].r)>>1;
        if(l<=mid)change(lson(pos),l,r,num);
        if(r>mid)change(rson(pos),l,r,num);
        pushup(pos);
    }
}

inline void print(int n,int flag)
{
    for(int i=1;i<=n;i++)
    {
        if(flag==1)cout<<query(1,i,i)<<' ';
        else if(flag==2)cout<<delta[i]<<' ';
    }
    cout<<endl;
}

int main()
{
    /*#ifdef DEBUG
    freopen("wxyt.in","r",stdin);
    freopen("wxyt.out","w",stdout);
    #endif*/
    int n=0,q=0,w=0,bufw=0;
    ll res=0,l=0,r=0,d=0;
    cin>>n>>q>>w;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    build(1,1,n);
    //print(n,1);
    for(int i=1;i<=q;i++)
    {
        //cout<<"Game "<<i<<endl;
        res=0;
        bufw=w;
        cin>>l>>r>>d;
        change(1,l,r,d);
        //print(n,1);
        while(bufw>0)
        {
            //cout<<"New round!"<<endl;
            for(int j=1;j<=n;j++)
            {
                ll buf=query(1,j,j);
                bufw-=buf;
                if(bufw<=0)break;
                else
                {
                    //cout<<"didn't die : "<<bufw<<endl;
                    res++;
                    change(1,j,j,buf);
                    delta[j]+=buf;
                    //print(n,1);
                    //print(n,2);
                }
            }
            if(bufw<=0)
            {
                //cout<<"died"<<endl;
                cout<<res;
                if(i<n)cout<<endl;
                for(int j=1;j<=n;j++)
                {
                    change(1,j,j,-delta[j]);
                }
                //print(n,1);
                memset(delta,0,sizeof(delta));
                //print(n,2);
            }
        }
    }
    return 0;
}
2024/10/20 23:42
加载中...