线段树求助!悬一关
查看原帖
线段树求助!悬一关
655082
YuTianQwQ楼主2023/9/29 16:10
#include<bits/stdc++.h>
using namespace std;
#define FASTOI ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int mx = 1e6+5;
struct st{
	int d,s,t,pd;
};
int tree[mx],a[mx];
bool IsThanZero = false;
void build(int root,int l,int r)
{
	if(l == r)
	{
		tree[root] = a[l];
		return;
	}
	int leftroot = root*2,rightroot = root*2+1,mid = (l+r)/2;
	build(leftroot,l,mid);
	build(rightroot,mid+1,r);
	tree[root] = a[leftroot]+a[rightroot];
}
void ChangeTree(int root,int s,int e,int l,int r,int x)
{
	if(s == e)
	{
		tree[root] += x;
		return ;
	}
	int leftroot = root*2,rightroot = root*2+1,mid = (s+e)/2;
	if(l <= mid) ChangeTree(leftroot,s,mid,l,r,x);
	if(r > mid) ChangeTree(rightroot,mid+1,e,l,r,x);
	tree[root] = tree[leftroot] + tree[rightroot];
	if(tree[root] < 0)
	{
		IsThanZero = true;
	}
}
int main()
{
	FASTOI;
	int n,m;
	cin >> n >> m;
	for(int i = 0;i < n;i++) cin >> a[i];
	build(0,0,n);
	vector<st> jugement(mx);
	for(int i = 0;i < m;i++)
	{
		if(jugement[i].pd != 0)
		{
			ChangeTree(0,0,n,jugement[i].s,jugement[i].t,jugement[i].d);
			jugement[i].pd = 0;
		}
		int d,s,t;
		cin >> d >> s >> t;
		jugement[i+t-s+1].d = d;
		jugement[i+t-s+1].pd = 1;
		jugement[i+t-s+1].s = s;
		jugement[i+t-s+1].t = t;
		ChangeTree(0,0,n,s,t,-d);
		if(IsThanZero) return cout << -1 << endl << i+1,0;
	}
	cout << 0;
	return 0;
}

2023/9/29 16:10
加载中...