说实话竟然没想到还能这样差分,这道题我们需要二分查找m个订单,对于每次二分用一次差分,然后看如果只考虑1到mid个订单是否会出现教室不够用的情况,如果够用说明导致教室不够用的订单在后面,应该让begin=mid+1;反之让end-1;其实这道题就是让我们找第一个出现教室不够用的订单
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
#include<cstring>
#define int long long
const int N = 1e6+5;
using namespace std;
char* p1, * p2, buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{int x = 0, f = 1;char ch = nc();while (ch < 48 || ch>57){if (ch == '-')f = -1;ch = nc();}while (ch >= 48 && ch <= 57)x = x * 10 + ch - 48, ch = nc();return x * f;
}
int a[N], b[N], c[N];
int p[N];
int cha[N];
int n, m;
int d[N], s[N], t[N];
int check() {int begin = 1, end = m;int mid;int res;while (begin <= end) {memset(b, 0, sizeof(b));mid = (begin + end) >> 1;for (int i = 1; i <= mid; i++) {b[s[i]] += d[i];b[t[i] + 1] -= d[i];}int p = 0;for (int i = 1; i <= n; i++) {b[i] += b[i - 1];if (b[i] > a[i]) {p = 1;break;}}if (p == 1)end=mid-1,res=mid;else begin=mid+1;}return res;
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m;for (int i = 1; i <= n; i++) {cin >> a[i];}for (int i = 1; i <= m; i++) {cin >> d[i] >> s[i] >> t[i];b[s[i]] += d[i];b[t[i] + 1] -= d[i];}int w = 0;for (int i = 1; i <= n; i++) {b[i] += b[i - 1];if (b[i] > a[i]) {w = 1; break;}}if (w == 0)cout << 0;else cout << -1 << endl << check() << endl;return 0;
}