求助!孩子T飞了
查看原帖
求助!孩子T飞了
151647
sycqwq楼主2020/6/7 16:36

rt线段树

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int lazy[maxn<<2],n,m,minn[maxn<<2],a[maxn];
struct node{
	int l,r,t;
}b[maxn];
int min(int x,int y)
{
	return x<y?x:y;
}
void push(int rt)
{
	if(lazy[rt])
	{
		lazy[rt<<1]+=lazy[rt];
		lazy[rt<<1|1]+=lazy[rt];
		minn[rt<<1]-=lazy[rt];
		minn[rt<<1|1]-=lazy[rt];
		lazy[rt]=0;
	}
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
		minn[rt]=a[l];
		return;
	}
	int mid=l+r>>1;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
}
void update(int l,int r,int rt,int i,int j,int k)
{
//	cout<<i<<' '<<j<<endl;
	if(l==r)
	{
//		cout<<k<<' ';
	
		minn[rt]-=k;
		lazy[rt]+=k;
		return;
	}
	push(rt);
	int mid=l+r>>1;
	if(i<=mid)
		update(l,mid,rt<<1,i,j,k);
	if(mid<j)
		update(mid+1,r,rt<<1|1,i,j,k);
	minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
		
}
int query(int l,int r,int rt,int i,int j)
{
	if(l>=i&&r<=j)
        return minn[rt];
    push(rt);
    int mid=(l+r)>>1;
    int ans=19260817;
    if(i<=mid)ans=min(ans,query(l,mid,rt<<1,i,j));
    if(mid<j)ans=min(query(mid+1,r,rt<<1|1,i,j),ans);
    return ans;
}
int cmp(node x,node y)
{
	return x.t<y.t;
}
int main(){
	ios::sync_with_stdio(false);
cout.tie(NULL);
	memset(minn,19260817,sizeof(minn));
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	build(1,n,1);
	for(int i=1;i<=m;i++)
	{
		cin>>b[i].t>>b[i].l>>b[i].r;
		
	}
//	sort(b+1,b+m+1,cmp);
	for(int i=1;i<=m;i++)
	{
		update(1,n,1,b[i].l,b[i].r,b[i].t);
//		cout<<x<<endl;
		if(minn[1]<0)
		{
			cout<<-1<<endl;
			cout<<i;
			return 0;
		}
	}
	cout<<0;
	return 0;
}

2020/6/7 16:36
加载中...