以时间为下标,1~n,每个位置的值初始化为当天可以出租的教室数量,按顺序处理订单,对于每个订单d s t,在区间[s,t]减去d,如果出现了区间最小值出现了小于零的情况,说明不符合,直接输出然后return 0,否则处理完全部订单之后输出0即可。
#include <cctype> #include <cfloat> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <algorithm> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <list> #include <map> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #include <unordered_map> #include <unordered_set> #define ll long long #define pll pair<long long, long long> #define P pair<int, int> #define PP pair<P, P> #define eps 1e-10 #define PI 3.1415926535898 #define It set<node>::iterator using namespace std; const int maxn=1e6+10; inline ll Read() { char ch=getchar(); ll num=0; while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)) { num=(num<<3)+(num<<1)+ch-'0'; ch=getchar(); } return num; } struct node { int Left,Right; ll lazy,val; }NODE[maxn<<2]; void PushUp(int idx) { NODE[idx].val=min(NODE[idx<<1].val,NODE[idx<<1|1].val); } void PushDown(int idx) { if (NODE[idx].lazy) { NODE[idx<<1].val-=NODE[idx].lazy; NODE[idx<<1|1].val-=NODE[idx].lazy; NODE[idx<<1].lazy+=NODE[idx].lazy; NODE[idx<<1|1].lazy+=NODE[idx].lazy; NODE[idx].lazy=0; } } void build(int l, int r, int idx) { NODE[idx]={l,r,0,0}; if (l==r) { NODE[idx].val=Read(); return; } int m=(l+r)>>1; build(l,m,idx<<1); build(m+1,r,idx<<1|1); PushUp(idx); } ll sum[maxn]; void Updata(int l, int r, ll sum, int idx) { if (NODE[idx].Left==l&&NODE[idx].Right==r) { NODE[idx].val-=sum; NODE[idx].lazy+=sum; return; } PushDown(idx); int m=(NODE[idx].Left+NODE[idx].Right)>>1; if (r<=m) Updata(l,r,sum,idx<<1); else if (l>m) Updata(l,r,sum,idx<<1|1); else { Updata(l,m,sum,idx<<1); Updata(m+1,r,sum,idx<<1|1); } PushUp(idx); } int main() { int n=Read(),m=Read(); build(1,n,1); for (int i=1; i<=m; ++i) { ll sum=Read(); int l=Read(); int r=Read(); Updata(l,r,sum,1); if (NODE[1].val<0) { printf("-1\n"); printf("%d\n",i); return 0; } } printf("0"); return 0; }