P1083 借教室

时间:2023-12-27 16:59:19

思路:前缀和, c表示对于当前的middle, 前缀和

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+1;
int n, m, now, r[maxn], d[maxn], s[maxn], t[maxn], c[maxn];
bool check(int x) {
int i, sum = 0;
if(now > x) {
for(i = x + 1; i <= now; i++) {
c[s[i]] -= d[i];
c[t[i] + 1] += d[i];
}
}
else {
for(i = now + 1; i <= x; i++) {
c[s[i]] += d[i];
c[t[i] + 1] -= d[i];
}
}
now = x;
for(int i = 1; i <= n; i++) {
sum += c[i];
if(sum > r[i]) return true;
}
return false;
}
int main() {
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];
int left = 1, right = m, ans = 0x3f3f3f;
while(left <= right) {
int middle = (left + right) / 2;
if(check(middle)) ans = min(ans, middle), right = middle - 1;
else left = middle + 1;
}
if(ans == 0x3f3f3f) cout << 0;
else cout << -1 << endl << ans;
return 0;
}