题目链接:

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+;
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=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
long long get_num(int x)
{
long long s=;
while(x>)
{
s+=num[x];
x-=lowbit(x);
}
return s;
}
int get_ans(int x,int s)
{
long long fx=(long long)x;
int l=,r=m,mid;
while(l<=r)
{
mid=(l+r)>>;
if(get_sum(mid)-get_num(mid)*fx<s)l=mid+;
else r=mid-;
}
if(l>m)return ;
return l;
}
int main()
{
memset(num,,sizeof(num));
memset(sum,,sizeof(sum));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d",&a[i]);
qu[i].b=a[i];
qu[i].pos=i;
}
sort(qu+,qu+m+);
for(int i=;i<=n;i++)
{
scanf("%d%d",&L,&R);
po[i].l=L;
po[i].r=R;
po[i].pos=i;
}
sort(po+,po+n+,cmp);
int flag=;
for(int i=;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=;i<=n;i++)
{
printf("%d ",ans[i]);
}
return ;
}

codeforces 589G G. Hiring(树状数组+二分)的更多相关文章

  1. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

  2. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  3. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  4. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  5. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  6. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  7. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

  8. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  9. Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]

    题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...

随机推荐

  1. html5小趣味知识点系列(一)autofocus

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 请求SQL数据是存在<null>,的解决方法

    删除字典中的null 我们在处理服务器传过来的数据过程中,如果数据中出现null,我们是没法进行本地持久化处理的.在使用NSUserDaults保存本地时,如果其中一个字段的value为NULL值,就 ...

  3. PHP-Manual的学习----【语言参考】----【类型】-----【对象】

    Object 对象1.对象初始化要创建一个新的对象 object ,使用 new 语句实例化一个类: class foo{    function do_foo(){        echo &quo ...

  4. 一个手动备份MySQL数据库的脚本

    #!/bin/bash username=root hostname=localhost password=root mysql -u$username -h$hostname -p$password ...

  5. hdu_1226超级密码(BFS)

    超级密码 Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息:密码是 ...

  6. EasyNVR流媒体直播之:零基础实现摄像头的全平台直播 (二)公网直播的实现

    接上回(https://blog.csdn.net/xiejiashu/article/details/81276870),我们实现内网直播,可以实现直播的web观看,该篇博文我们将实现公网的直播. ...

  7. spring boot ajax post 前后端

    1 传输的数据格式是json 1.1 前端ajax json的所有的key都必须是双引号引用的,并且最外层也要用双引号引用.例如 "{"a":b, "b&quo ...

  8. Python的自省机制

    什么是自省? 在日常生活中,自省(introspection)是一种自我检查行为. 在计算机编程中,自省是指这种能力:检查某些事物以确定它是什么.它知道什么以及它能做什么.自省向程序员提供了极大的灵活 ...

  9. IE模式下背景图片不显示

    初衷是想给这个提交按钮<input type="submit" value=" />加上背景图片,用了以下css样式: .subtn input { back ...

  10. python -virtualenvwrapper 切换不同的python版本

    环境: 安装了python2.7和python3.4, 两个版本都安装了virtualenv和virtualenvwrapper 在windows cmd中键入mkvirtualenv -p C:\P ...