P1083 [NOIP 2012 提高组] 借教室

点击查看代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=1e6+10;
int n,m;
int r[N];
int d[N],s[N],t[N];
LL b[N];

bool check(int x)
{
    memset(b,0,sizeof b);

    for(int i=1;i<=x;i++){
        b[s[i]]+=d[i];
        b[t[i]+1]-=d[i];
    }

    LL cur=0;
    for(int i=1;i<=n;i++){
        cur+=b[i];
        if(cur>r[i]){
            return false;
        }
    }

    return true;
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);

    cin>>n>>m;

    for(int i=1;i<=n;i++) cin>>r[i];

    for(int i=1;i<=m;i++) cin>>d[i]>>s[i]>>t[i];

    if(check(m)){
        cout<<"0"<<endl;
        return 0;
    }

    int L=1,R=m+1;
    int ans;

    while(L<=R){
        int mid=L+(R-L)/2;
        if(check(mid)){
            L=mid+1;
        }else{
            ans=mid;
            R=mid-1;
        }
    }

    cout<<"-1"<<endl<<ans;

    return 0;
    
}
posted @ 2026-02-09 23:03  AnoSky  阅读(3)  评论(0)    收藏  举报