### NOIP2012 Day2 借教室-二分答案+差分

``#include<cstdio>#include<iostream>#define MID (l+r)>>1#define maxn 1000005using namespace std;long long n,m,c[maxn],d[maxn],s[maxn],t[maxn],l,r,mid,sum[maxn],tmp,last;bool check(int x){    tmp=0;    for(int i=1;i<=n;++i){        tmp+=sum[i];        if(tmp>c[i])return false;    }    return true;}void add(int i,int j){    for(;i<=j;++i){        sum[s[i]]+=d[i];        sum[t[i]+1]-=d[i];    }    return ;}void cancel(int i,int j){    for(;i<=j;++i){        sum[s[i]]-=d[i];        sum[t[i]+1]+=d[i];    }    return ;}int main(){        scanf("%lld%lld",&n,&m);        for(int i=1;i<=n;++i)scanf("%lld",&c[i]);            l=1;r=m;mid=MID;        for(int i=1;i<=mid;++i)scanf("%lld%lld%lld",&d[i],&s[i],&t[i]);        last=mid;        add(l,mid);        while(l<r){            if(check(mid)){                l=mid+1;                mid=MID;                if(last<mid){                    for(int i=last+1;i<=mid;++i)scanf("%lld%lld%lld",&d[i],&s[i],&t[i]);                    last=mid;                }                add(l,mid);            }            else {                r=mid;                mid=MID;                cancel(mid+1,r);            }        }        if(check(l))printf("%d\n",0);        else printf("%d\n%lld\n",-1,l);    return 0;}``