NEERC 2015 Southern Subregional G Hiring 樹狀陣列

life4711發表於2015-10-23

http://codeforces.com/contest/589/problem/G

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.

Sample test(s)
input
3 3
4 2 5
1 3
2 5
3 4
output
1 3 0 

/**
NEERC 2015 Southern Subregional G Hiring   樹狀陣列
題目大意:有n個應聘者,每個人必須做完a[i]的工作量,但是每天他們投入工作前必須熱身b[i]時間,每天每個人的工作量
           最大為t[i],問每個人最早哪天完工。
解題思路:很好的題目,二分第幾天mid,用一個樹狀陣列求到mid天的除去準備時間的總時間,另一個樹狀陣列求1到mid天有幾天因為一天
          的時間比準備時間少而刪去的天數

*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=1e6+7;

LL sum[2][maxn];
int n,m,num[maxn];
vector<int>vec[maxn];

struct note
{
    int x,y,id;
    bool operator <(const note &other)const
    {
        return x<other.x;
    }
} a[maxn];

int lowbit(int x)
{
    return x & -x;
}

LL query(int t,int x)
{
    LL ret=0;
    while(x>0)
    {
        ret+=sum[t][x];
        x-=lowbit(x);
    }
    return ret;
}

void add(int t,int x,int d=1)
{
    while(x<=m)
    {
        sum[t][x]+=d;
        x+=lowbit(x);
    }
}

int get(int t)
{
    int l=1,r=m,ans=0;
    while(l<=r)
    {
        int mid=(l+r)/2;
        LL tmp=mid-query(1,mid);
        LL cnt=query(0,mid)-tmp*a[t].x;
        if(cnt>=a[t].y)
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(sum,0,sizeof(sum));
        for(int i=0; i<maxn; i++)vec[i].clear();
        for(int i=1; i<=m; i++)
        {
            int x;
            scanf("%d",&x);
            add(0,i,x);
            vec[x].push_back(i);
        }
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].id=i;
        }
        sort(a,a+n);
        int pos=0;
        for(int i=0; i<n; i++)
        {
            while(pos<=a[i].x)
            {
                for(int j=0; j<vec[pos].size(); j++)
                {
                    int v=vec[pos][j];
                    add(0,v,-pos);
                    add(1,v);
                }
                pos++;
            }
            num[a[i].id]=get(i);
        }
        for(int i=0; i<n; i++)printf(i==n-1?"%d\n":"%d ",num[i]);
    }
    return 0;
}


相關文章