比赛 欢乐五一练练练 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 借教室 最终得分 100
用户昵称 sxysxy 运行时间 1.684 s
代码语言 C++ 内存使用 19.36 MiB
提交时间 2017-04-26 19:17:04
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#define MAXN 1000002
int R[MAXN];
int d[MAXN], s[MAXN], t[MAXN];
int n;
int fast_read()
{
    int x = 0;
    char c;
    while(c = getchar())
    {
        if(c >= '0' && c <= '9')
        {
            x = c^0x30;
            break;
        }
    }
    while(isdigit(c = getchar()))x = (x<<3)+(x<<1)+(x^0x30);
    return x;
}
int p[MAXN];
bool solve(int k)
{
    memset(p, 0, sizeof(p));
    int sum = 0;
    for(int i = 1; i <= k; i++)
    {
        p[s[i]] += d[i];
        p[t[i]+1] -= d[i];
    }
    for(int i = 1; i <= n; i++)
    {
        sum += p[i];
        if(sum > R[i])     //涓嶅?
            return false;
    }
    return true;
}
int main()
{
    freopen("classrooms.in", "r", stdin);
    freopen("classrooms.out", "w", stdout);
    int m;
   // n = fast_read();
   // m = fast_read();
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", R+i);
    for(int i = 1; i <= m; i++)
    {
     //   d[i] = fast_read();
      //  s[i] = fast_read();
      //  t[i] = fast_read();
        scanf("%d %d %d", d+i, s+i, t+i);
    }
    int l = 1, r = m;
    int k;
    int res = 0;
    while(l <= r)
    {
        k = (l+r)>>1;
        if(solve(k))
            l = k+1;
        else
        {
            res = k;
            r = k-1;
        }   
    }
    if(res == 0)
        puts("0");
    else
        printf("-1 \n%d", res);
    return 0;            
}