线段树求调
查看原帖
线段树求调
766182
czyzh楼主2023/2/1 21:00
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e6 + 10, inf = 2147483647;
struct node
{
	ll l, r, val;
	ll lazy;
} tree[N * 4];
inline ll read()
{
	ll x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') f = -1; 
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}
inline void push_up(int p)
{
	tree[p].val = min(tree[p << 1].val, tree[p << 1 | 1].val);
}
inline void build(int p, int l, int r)
{
	tree[p].l = l, tree[p].r = r;
	if(l == r) 
	{
		tree[p].val = read();
		return ;
	}
	ll mid = (l + r) >> 1;
	build(p << 1, l, mid);
	build(p << 1 | 1, mid + 1, r);
	push_up(p);
}
inline void push_down(int p)
{
	if(tree[p].lazy)
	{
		tree[p << 1].val -= tree[p].lazy;
		tree[p << 1 | 1].val -= tree[p].lazy;
		tree[p << 1].lazy -= tree[p].lazy;
		tree[p << 1 | 1].lazy -= tree[p].lazy;
		tree[p].lazy = 0;
	}
}
inline void change(int p, int l, int r, int val)
{
	if(l <= tree[p].l && r >= tree[p].r)
	{
		tree[p].val -= val;
		tree[p].lazy += val;
		return ;
	}
	push_down(p);
	ll mid = (tree[p].l + tree[p].r) >> 1;
	if(l <= mid) change(p << 1, l, r, val);
	if(r > mid) change(p << 1 | 1, l, r, val);
	push_up(p);
}
inline ll get_min(int p, int l, int r)
{
	if(l <= tree[p].l && r >= tree[p].r) return tree[p].val;
	push_down(p);
	ll mid = (tree[p].l + tree[p].r) >> 1, val = inf;
	if(l <= mid) val = min(val, get_min(p << 1, l, r));
	if(r > mid) val = min(val, get_min(p << 1 | 1, l, r));
	return val;
}
int main()
{
	ll n = read(), m = read();
	build(1, 1, n);
	for(ll i = 1; i <= m; i++)
	{
		ll val = read(), l = read(), r = read();
		if(get_min(1, l, r) < val)
		{
			printf("-1\n%d\n" , i);
			return 0;
		}
		change(1, l, r, val);
	}
	printf("0");
	return 0;
}
2023/2/1 21:00
加载中...