https://www.luogu.com.cn/problem/P1083
ac代码:
#include<iostream> #include<cstring> using namespace std; const int maxn=1e6+5; int n,m; int room[maxn],d[maxn],l[maxn],r[maxn],a[maxn],need[maxn]; bool check(int x){ memset(a,0,sizeof(a)); for(int i=1;i<=x;i++){ a[l[i]]+=d[i]; a[r[i]+1]-=d[i]; } for(int i=1;i<=n;i++){ need[i]=need[i-1]+a[i];//差分思想 if(need[i]>room[i]) return 0; } return 1; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++) cin>>room[i]; for(int i=1;i<=m;i++){ cin>>d[i]>>l[i]>>r[i]; } if(check(m)){cout<<"0";return 0;} int left=1,right=m; while(left<right){ int mid=(left+right)/2; if(check(mid)) left=mid+1; else right=mid;//mid可能是答案,所以right=mid而不是mid-1; } cout<<"-1"<<endl<<left; return 0; }