- 借教室
- 2017-02-03 11:55:26 @
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
inline int Read() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 - '0' + ch, ch = getchar(); }
return x * f;
}
const int N = 1e6 + 5;
int n, m, r[N], addv[N << 2], minv[N << 2];
inline void PushUp(int rt) { minv[rt] = min(minv[rt << 1], minv[rt << 1 | 1]); }
inline void PushDown(int rt) {
addv[rt << 1] += addv[rt], minv[rt << 1] += addv[rt];
addv[rt << 1 | 1] += addv[rt], minv[rt << 1 | 1] += addv[rt];
addv[rt] = 0;
}
void Build(int L, int R, int rt) {
if (L == R) {
minv[rt] = r[L];
return;
}
int mid = (L + R) >> 1;
Build(L, mid, rt << 1), Build(mid + 1, R, rt << 1 | 1);
PushUp(rt);
}
inline bool Query(int l, int r, int v, int L, int R, int rt) {
if (l <= L && r >= R) {
addv[rt] -= v;
minv[rt] -= v;
return minv[rt] >= 0;
}
PushDown(rt);
int mid = (L + R) >> 1;
bool res = true;
if (r <= mid) res = Query(l, r, v, L, mid, rt << 1);
else if (l > mid) res = Query(l, r, v, mid + 1, R, rt << 1 | 1);
else res = Query(l, r, v, L, mid, rt << 1) & Query(l, r, v, mid + 1, R, rt << 1 | 1);
PushUp(rt);
return res;
}
int main() {
n = Read(), m = Read();
for (int i = 1; i <= n; i++) r[i] = Read();
Build(1, n, 1);
bool ok = true; int cnt = 0;
while (m--) {
cnt++;
int d, s, t; scanf("%d%d%d", &d, &s, &t);
if (Query(s, t, d, 1, n, 1)) continue;
else { ok = false; break; }
}
if (ok) printf("0\n");
else printf("-1\n%d\n", cnt);
return 0;
}