AcWing 503. 借教室
原题链接
简单
作者:
也许
,
2021-03-26 20:47:03
,
所有人可见
,
阅读 323
#include <iostream>
using namespace std;
const int N = 1000010;
int a[N];
int n, m;
int d[N], s[N], t[N];
int b[N];
bool check(int mid)
{
for (int i = 1; i <= n; i++) b[i] = a[i];
for (int i = n; i >= 1; i--) b[i] -= b[i - 1];
for (int i = 1; i <= mid; i++)
{
b[s[i]] -= d[i], b[t[i] + 1] += d[i];
}
int res = 0;
for (int i = 1; i <= n; i++)
{
res += b[i];
if (res < 0) return true;
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
for (int i = 1; i <= m; i++) scanf("%d%d%d", d + i, s + i, t + i);
int l = 0, r = m;
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
if (l == m) cout << 0;
else cout << -1 << endl << r << endl;
return 0;
}