只需要找到第一个不能分配的订单所以只需要二分一下是第几个订单不能分配就可以,判定的过程用差分数组。
#include <bits/stdc++.h>
#define IO \
ios::sync_with_stdio(false); \
// cout.tie(0);
#define lson(x) node << 1, start, mid
#define rson(x) node << 1 | 1, mid + 1, end
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 5e5 + 10;
const int maxm = 1e4 + 10;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
// int dis[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
LL n, m;
LL r[maxn];
LL d[maxn], s[maxn], t[maxn];
LL c[maxn];
bool check(int mid)
{
for (int i = 1; i <= n; i++)
c[i] = r[i] - r[i - 1];
for (int i = 1; i <= mid; i++)
{
c[s[i]] -= d[i];
c[t[i] + 1] += d[i];
}
for (int i = 1; i <= n; i++)
{
c[i] += c[i - 1];
if (c[i] < 0)
return false;
}
return true;
}
int main()
{
#ifdef WXY
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
IO;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> r[i];
for (int i = 1; i <= m; i++)
{
cin >> d[i] >> s[i] >> t[i];
}
if (check(m) == true)
{
return cout << 0, 0;
}
LL L = 1, R = m;
LL ans = 1;
while (L <= R)
{
LL mid = L + R >> 1;
if (check(mid))
{
ans = mid + 1;
L = mid + 1;
}
else
R = mid - 1;
}
cout << -1 << "\n";
cout << ans;
return 0;
}