social activity

Description

bbq job is to manage the school's community activities, specifically allocated for each classroom group activities. Limited classroom should arrange for these associations, it is not easy.

Each community activities with k, t1, t2 are represented: the society activities on Day t1 ~ t2 of need within the k-th class (including t1, t2).

According to the order assigned classroom bbq always apply for community activities. If one day is not enough remaining number of classrooms to meet the requirements of a society, then stop the distribution of the classroom. bbq need to inform the society, the times of their community activities can not be carried out.

Input

Each group comprises a first input data line two positive integers n, m, the total number of the total number of days and community activities.

The second line contains a positive integer n, where r is the i-th
i
, It represents the number of i-sky classroom.

Then there are m rows, the information of each behavior of a community activities, comprising three positive integer K, T . 1 , T 2. (K, t1, t2 as described in the title).

 

Provisions:

Days numbered and community activities are an integer number from 1 to start.

1≤n, m≤10
6
,0≤ri≤10
9
,0≤k≤109  ,1≤t1≤t2≤n。

Output

If the application can meet all societies, the output is 0.

Otherwise, output a positive integer, community activities need to inform bbq activities can not be numbered.

Sample Input

4 3
2 5 4 3
2 1 3
3 2 4
4 2 4

Sample Output

2


#include<iostream>
#include<cstring>
using namespace std;
const int N=1e6+50;
long long n,m;
long long a[N],room[N],t1[N],t2[N],sum[N];
int check(int mid){
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=mid;i++){
        sum[t1[i]]-=room[i];
        sum[t2[i]+1]+=room[i];
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        cnt+=sum[i];
        if(a[i]+cnt<0)
            return 0;
    }
    return 1;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=m;i++){
        cin>>room[i]>>t1[i]>>t2[i];
    }
    int l=1,r=m+1;
    while(l<r){
        int mid=(l+r)>>1;
        if(check(mid))
            l=mid+1;
        else
            r=mid;    
    }
    if(l==m+1)
        cout<<0<<endl;
    else
        cout<<l<<endl;    
    return 0;
} 
View Code

 

First, to clarify the meaning of this question, I would like him to read several times.

The effect of the problem is said to give the community a classroom assignment, but the total classroom is limited, and the rest of the classroom every day is different, put it plainly, it is to be on the range of subtraction, to take the sample in question is the total number of days in the first row 4 represents a group activities is represented by the number of 4,3. The second line indicates the number of remaining four days in the classroom every day, and the next three rows represent information of community activities, including the number of classrooms needed, and continued for several days. Take the first community activity information, the 213 represents the first 1-3 days in, use two classrooms every day, this time in the put 2,543,254 each minus 2,

On behalf of these classrooms are occupied, this time to update the information on the remaining classroom 0323 and then look at the second information 324 community activities on behalf of the club activities in the first 2-4 days, use three classrooms everyday, time to put 0323 in the 323 each subtract 3, then the rest of the classroom can be found on the third day two, not subtract 3, then, assign classroom activity is stopped, bbq on the report says community activities can not meet the requirements, then the program is over, so just find the first in a range of community activities so that negative values ​​on the line.

This knowledge questions related to the answers and there are two points difference array

Under the first-half of the answer, when the problem results monotone, you can use half the answer to solve, take this question, if the i-th community demands not met, then the future of the i-th community requires that all communities are can not meet, think about this. So we just can not find the first meet of community activities on the line, then use half the answer,

 

 Using the binary answer needs to find the answer to the minimum interval and a maximum value l r, then treated with (l + r) >> 1 represented mid, (l + r) >> 1 is meant, (l + r) divided by 2 the integer part, then it also needs a check function, the mid pass into this function, the function to determine the mid eligibility, half of the answer, the writing check function is more important, check function also because of the subject-specific the other half part of the answer can be templated

 

 

 

Take this question, we write the check function is used to determine the association activities can not be satisfied, if this activity is referred to mid met, (see above, satisfying the condition represented by 1 and 0 is not satisfied condition), we have l = mid + 1, because we are looking for is satisfied and does not satisfy the border areas, and we are looking for the first activity is not satisfied, mid + 1 may be satisfied or may not be Satisfy.

 

 If the active mid referred to are not met (as shown above), we have r = mid, this time we should change the boundaries of the right border, pay attention, because mid is not satisfied, and we are looking for is not satisfied so we can not lose mid, if r = mid-1, you're wrong.

Kept so narrow, l = r to the end time, the end of two minutes.

Here to explain why the code r = m + 1, number of community activities to m is not ended yet, then the scope of the answer should be to m ah, why do plus 1, because there is another case, that is, all the community activities have been met, so we use the m + 1 to indicate this case, that the scope of the answer is 1 to m + 1 instead of 1 to m.

 

 

 

Check on the writing function, here the nature of the difference array. First explain what is the difference array.

If there are two said arrays a and b, if b [i] = a [i] -a [i-1], then b is called a array of arrays differential array.

For example, there is an array a; 1 3 3 4 2 5 6 7 4

After the array b is a difference; 1201-2311-3

Array b has a characteristic that the value of (b [0] + b [1] + `` `` `+ b [i]) b add up the array equal to a [i] value,

Difference in addressing the array interval subtraction method is useful, it is to take a sample of this problem, the 2,5,4,3 deposited into an array a [1] to a [4], the

 

 

 

 

Guess you like

Origin www.cnblogs.com/fate-/p/12239140.html