碰到区间问题要想到差分。
此题一开始想成[1, n]二分,原因是读题不认真,题目要求按订单一个个顺序判断,只要有一个不符合就打回去,不用管后面的,所以是[1, m]满足单调性,故二分订单。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1000010;
int n, m;
int r[maxn], sum[maxn];
struct node{
int d, s, t;
} rent[maxn];
bool judge(int x){
memset(sum, 0, sizeof(sum));
for(int i = 1; i <= x; i++)
sum[rent[i].s] += rent[i].d,
sum[rent[i].t+1] -= rent[i].d;
int now = 0;
for(int i = 1; i <= n; i++){
now += sum[i];
if(now > r[i]) return 0;
}
return 1;
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> r[i];
for(int i = 1; i <= m; i++)
cin >> rent[i].d >> rent[i].s >> rent[i].t;
int l = 1, r = m + 1;//一开始想成n了
while(l < r){
int mid = (l+r)>>1;
if(judge(mid)) l = mid + 1;
else r = mid;
}
if(l < m) cout << "-1" << endl << l << endl;
else cout << "0" << endl;
return 0;
}