codeforces 589G G. Hiring(树状数组+二分)

题目链接:

G. Hiring

time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

The head of human resources department decided to hire a new employee. He created a test exercise for candidates which should be accomplished in at most m working days. Each candidate has to pass this test exercise. During the j-th day a candidate is allowed to be in the office for at most tj units of time.

Overall, n candidates decided to apply for the job and sent out their resumes. Based on data received the head has defined two parameters describing every candidate: di and ri. The parameter di is the time to get prepared for work which the i-th candidate spends each morning. This time doesn't depend on day. The parameter ri is the total working time needed for the i-th candidate to accomplish the whole test exercise.

Thus the time spent in the office in the j-th day consists of di units of time to get prepared and some units of time to proceed with the exercise. A candidate can skip entire working day and do not come to the office. Obviously in this case he doesn't spend di units of time to prepare.

To complete the exercise a candidate should spend exactly ri units of time working on the exercise (time to prepare is not counted here).

Find out for each candidate what is the earliest possible day when he can fully accomplish the test exercise. It is allowed to skip working days, but if candidate works during a day then he must spend di units of time to prepare for work before he starts progressing on the exercise.

Input

The first line contains two integer numbers n,  m (1 ≤ n,  m ≤ 2·105) — the number of candidates and the maximum number of working days to do the test exercise.

The second line contains m integer numbers t1, t2, ..., tm (1 ≤ tj ≤ 106) — the durations of working days in time units.

The following n lines contain two integers each: di,  ri (0 ≤ di ≤ 106,  1 ≤ ri ≤ 106) — how much time in the beginning of a day is required for i-th candidate before he starts his work on the test exercise and how much time it is needed for him to accomplish this task.

Output

Output a sequence of n integer numbers b1, b2, ..., bn, where bi is the earliest day when the i-th candidate can finish the test exercise.

In case the i-th candidate cannot finish the test exercise in m days output bi = 0.

Days in this problem are numbered from 1 to m in the order they are given in the input.

Examples
input
3 3
4 2 5
1 3
2 5
3 4
output
1 3 0 

题意:m天,每天允许的工作时间是t[i],n个工人,每个工人的工作总量需要时间为r[i],而且每天工作前要预热d[i]时间,问最早能在第几天完成工作;
思路:假设答案是ans,那么在前ans天中每天允许的时间大于d[i]的就是可以工作的一天t[i]-d[i]就是这天能完成的工作量,前ans天的加一块就>=r[i个工人],把这些按大到小排序,然后update到树状数组中,一个数组记录总量,一个记录有多少天update了sum-num*d[i]和r[i]比较就好了,这时又需要二分来快速找到答案ans;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+4;
int n,m,a[N],ans[N],num[N],L,R;
long long sum[N];
struct nod
{
    friend bool operator< (nod x,nod y)
    {
        return x.b>y.b;
    }
    int b,pos;
};
nod qu[N];
struct node
{
    int l,r,pos;
};
node po[N];
bool cmp(node x,node y)
{
    return x.l>y.l;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int nu)
{
    while(x<=m)
    {
        num[x]++;
        sum[x]+=(long long)nu;
        x+=lowbit(x);
    }
}
long long get_sum(int x)
{
    long long s=0;
    while(x>0)
    {
        s+=sum[x];
        x-=lowbit(x);
    }
    return s;
}
long long get_num(int x)
{
    long long s=0;
    while(x>0)
    {
        s+=num[x];
        x-=lowbit(x);
    }
    return s;
}
int get_ans(int x,int s)
{
    long long fx=(long long)x;
    int l=1,r=m,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(get_sum(mid)-get_num(mid)*fx<s)l=mid+1;
        else r=mid-1;
    }
    if(l>m)return 0;
    return l;
}
int main()
{
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&a[i]);
        qu[i].b=a[i];
        qu[i].pos=i;
    }
    sort(qu+1,qu+m+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&L,&R);
        po[i].l=L;
        po[i].r=R;
        po[i].pos=i;
    }
    sort(po+1,po+n+1,cmp);
    int flag=1;
    for(int i=1;i<=n;i++)
    {
        while(qu[flag].b>po[i].l&&flag<=m)
        {
            int y=qu[flag].pos;
            update(y,a[y]);
            flag++;
        }
        ans[po[i].pos]=get_ans(po[i].l,po[i].r);
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d ",ans[i]);
    }
    return 0;
}

 

你可能感兴趣的:(codeforces 589G G. Hiring(树状数组+二分))