题目链接:

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. Gradle 构建工具

    参考文章: 作者:ghui 链接:https://www.zhihu.com/question/30432152/answer/48239946 来源:知乎 著作权归作者所有.商业转载请联系作者获得授 ...

  2. 【转】使用 Python Mock 类进行单元测试

    出处:https://www.oschina.net/translate/unit-testing-with-the-python-mock-class?lang=chs&page=2#

  3. systemctl使用说明

    # systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ...

  4. iOS表格制作

    由于项目上的需求,需要做一个表格出来,来显示流程状态.刚开始脑子一头雾水,没有一点思路,但是靠着自己的座右铭--“世上无难事,只怕有心人”,克服了所有困难.好,不说了,讲正事. 制作表格,还是需要ta ...

  5. MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现

    原文链接:http://hi.baidu.com/wangzhiqing999/item/7ca215d8ec9823ee785daa2b MySQL 下 ROW_NUMBER / DENSE_RAN ...

  6. windy数(简单数位DP)

    1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 6306  Solved: 2810[Submit][Sta ...

  7. 九度OJ 1196:成绩排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4339 解决:1476 题目描述: 用一维数组存储学号和成绩,然后,按成绩排序输出. 输入: 输入第一行包括一个整数N(1<=N< ...

  8. js滚动到指定位置显示或隐藏元素

    $(function(){ $(window).scroll(function(){ var scroll_top=$(window).scrollTop(); console.log(scroll_ ...

  9. JQuery日记 5.11 Sizzle选择器(五)

    //设置当前document和document相应的变量和方法 setDocument = Sizzle.setDocument = function( node ) { var hasCompare ...

  10. Cocos2d-x 3.1 环境搭建和创建project

    Cocos2d-x 3.x改版了非常多,之前搭过一次环境,可是没截图.这次趁着重装电脑,一边搭建一边截图.此博文仅仅是为了记录而不是为了教学,所以很多其它讲的是搭建过程.本文基本上參考这篇博客:htt ...