[Luogu] 借教室
https://www.luogu.org/problemnew/show/P1083
二分第i天不满足
前缀和 + 差分判断
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 1000010; int n, m, ans; int a[N], d[N], x[N], y[N], s[N], sum; inline int read(){ int x = 0; char c = getchar(); while(c < '0' || c > '9') c = getchar(); while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x; } inline bool judge(int v){ memset(s, 0, sizeof(s)); sum = 0; for(int i = 1; i <= v; i ++) { s[x[i]] += d[i]; s[y[i]+1] -= d[i]; } for(int i = 1; i <= n; i ++) { sum += s[i]; if(sum > a[i]) return 0; } return 1; } int main() { n = read(); m = read(); for(int i=1; i<=n; i++) a[i] = read(); for(int i=1; i<=m; i++) d[i] = read(), x[i] = read(), y[i] = read(); int l = 1, r = m; while(l <= r){ int mid = (l + r) >> 1; if(!judge(mid)) ans = mid, r = mid - 1; else l = mid + 1; } if(!ans) printf("0"); else printf("-1\n%d",ans); return 0; }