503. 借教室 - AcWing题库
想到了差分没想到二分法。同时注意INT的范围是10位。
#include<iostream>
using namespace std;
int n,m;
const int N = 1e6+10;
int r[N],s[N],d[N],t[N];
long long b[N];
bool check(int mid){for(int i=1;i<=n;i++){b[i]=r[i]-r[i-1];}for(int i=1;i<=mid;i++){b[s[i]]-=d[i];b[t[i]+1]+=d[i];}for(int i=1;i<=n;i++){b[i]+=b[i-1];if(b[i]<0){return true;}}return false;
}
int main(){cin>>n>>m;for(int i=1;i<=n;i++){scanf("%d",&r[i]);}for(int i=1;i<=m;i++){scanf("%d%d%d",&d[i],&s[i],&t[i]);}if(!check(m)){printf("0");return 0;}int l=1,r=m;while(l<r){int mid = (l+r)/2;if(check(mid)){r=mid;}else{l=mid+1;}}printf("-1\n");printf("%d",r);return 0;
}