题目地址


注意点:

  • 需要一开始build一次才能保证minn的正确性.

#include<cstdio>
#include<iostream>
using namespace std;
const int MAXN=2e6,INF=2e9,root=1;
struct Node{
	int l,r;
	int minn=INF,lazy;
}tr[4*MAXN];
int rest[MAXN];
void build(int now,int l,int r){
	tr[now].l=l,tr[now].r=r;
	if(l==r){
		tr[now].minn=rest[l];
		return;
	}
	int mid=(l+r)>>1;
	build(now<<1,l,mid);
	build((now<<1)|1,mid+1,r);
	tr[now].minn=min(tr[now<<1].minn,tr[(now<<1)|1].minn);
}
void insert(int now,int ll,int rr,int val){
	int l=tr[now].l,r=tr[now].r;
	if(l>=ll&&r<=rr){
		tr[now].minn+=val;
		tr[now].lazy+=val;
		return;
	}
	if(tr[now].lazy){
		tr[now<<1].minn+=tr[now].lazy;
		tr[now<<1].lazy+=tr[now].lazy;
		tr[(now<<1)|1].minn+=tr[now].lazy;
		tr[(now<<1)|1].lazy+=tr[now].lazy;
		tr[now].lazy=0;
	}
	int mid=(l+r)>>1;
	if(ll<=mid)insert(now<<1,ll,rr,val);
	if(rr>mid)insert((now<<1)|1,ll,rr,val);
	tr[now].minn=min(tr[now<<1].minn,tr[(now<<1)|1].minn);
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&rest[i]);
	}
	build(root,1,n);
	for(int i=1;i<=m;i++){
		int d,s,t;
		scanf("%d%d%d",&d,&s,&t);
		insert(root,s,t,-d);
		if(tr[root].minn<0){
			printf("-1\n");
			printf("%d\n",i);
			return 0;
		}
	}
	printf("0\n");
	return 0;
}