Noip2012换教室(线段树)

简述题意

给出有一颗线段树,对其进行m次区间修改操作,如其区间最小值小于0,则此区间修改不成立,输出-1及区间修改的次序。若都成立,则输出0

#include<bits/stdc++.h>
#define rson rt<<1
#define lson rt<<1|1
using namespace std;
const int maxn=1e6+100;
long long lazy[maxn<<2],minv[maxn<<2],w[maxn];
int n,m;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
inline int min(long long x,long long y)
{
    return x>y?y:x;
}
inline void pushup(int rt)
{
    minv[rt]=min(minv[rson],minv[lson]);
}
inline void pushdown(int rt,int m)
{
    if(lazy[rt])
    {
        lazy[lson]+=lazy[rt];
        lazy[rson]+=lazy[rt];
        minv[lson]-=lazy[rt];
        minv[rson]-=lazy[rt];
        lazy[rt]=0;
    }
}
inline void build(int l,int r,int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        minv[rt]=w[l];
        return;
    }
    int m=(r+l)>>1;
    build(l,m,lson);
    build(m+1,r,rson);
    pushup(rt);
}
inline void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        lazy[rt]+=c;
        minv[rt]-=c;
        return;
    }
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m)
    update(L,R,c,l,m,lson);
    if(m<R)
    update(L,R,c,m+1,r,rson);
    pushup(rt);
}
int main()
{   
    n=read();m=read();
    for(int i=1;i<=n;i++)
    {
    	w[i]=read();
    }
    build(1,n,1);
    for(int i=1;i<=m;i++)
    {
        int d=read(),s=read(),t=read();
        update(s,t,d,1,n,1);
        if(minv[1]<0)
        {
            printf("-1\n%d\n",i);
            exit(0);
        }
    }
    printf("0");
    return 0;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值