B. Processing Queries

time limit per test5 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment ti and needs to be processed for di units of time. All ti are guaranteed to be distinct.

When a query appears server may react in three possible ways:

If server is free and query queue is empty, then server immediately starts to process this query.

If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue.

If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.

As soon as server finished to process some query, it picks new one from the queue (if it’s not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.

For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.

Input

The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.

Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers ti and di (1 ≤ ti, di ≤ 109), where ti is the moment of time when the i-th query appears and di is the time server needs to process it. It is guaranteed that ti - 1 < ti for all i > 1.

Output

Print the sequence of n integers e1, e2, …, en, where ei is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or  - 1 if the corresponding query will be rejected.

Examples

input

5 1

2 9

4 8

10 9

15 2

19 1

output

11 19 -1 21 22

input

4 1

2 8

4 8

10 9

15 2

output

10 18 27 -1

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue> using namespace std;
#define MAX 200000
struct Node
{
int id;
long long int st;
long long int time;
}a[MAX+5];
int cmp(Node a,Node b)
{
return a.st<b.st;
}
queue<Node> q;
long long int ans[MAX+5];
int n,b;
int main()
{
scanf("%d%d",&n,&b);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i].st,&a[i].time);
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
int i=2;
long long int now=a[1].st+a[1].time;
ans[1]=now;
while(1)
{
if(i>n&&q.size()==0)
break;
while(i<=n)
{
if(a[i].st>=now)
break;
if(q.size()>=b)
ans[a[i++].id]=-1;
else
{
q.push(a[i++]);
}
}
if(q.size()==0)
{now=a[i].st+a[i].time;ans[i]=now;i++;continue;}
Node term=q.front();
q.pop();
now+=term.time;
ans[term.id]=now;
}
for(int i=1;i<=n;i++)
{
if(i==n)
printf("%lld\n",ans[i]);
else
printf("%lld ",ans[i]);
}
return 0;
}

Code Forces 644B Processing Queries的更多相关文章

  1. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  2. CROC 2016 - Qualification B. Processing Queries 模拟

    B. Processing Queries 题目连接: http://www.codeforces.com/contest/644/problem/B Description In this prob ...

  3. Spark SQL includes a cost-based optimizer, columnar storage and code generation to make queries fast.

    https://spark.apache.org/sql/ Performance & Scalability Spark SQL includes a cost-based optimize ...

  4. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  5. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  6. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  7. codeforces644B. Processing Queries (模拟)

    In this problem you have to simulate the workflow of one-thread server. There are n queries to proce ...

  8. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  9. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

随机推荐

  1. php实现aes加密类

    php实现的aes加密类,代码中有使用方法. <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null ...

  2. atitit.面向过程的编程语言异常处理 c语言 asp vbs 的try catch 实现

    atitit.面向过程的编程语言异常处理 c语言 asp vbs 的try catch 实现 1. 返回值法.and全局ERROR 变量法 1 2. 抛出异常Err.Raise 1 3. 实现try  ...

  3. 使用burpsuite来扫描漏洞

    一张图说明全文.

  4. 用Zend OPCache提高PHP的性能

    Zend OPCache的前身是Zend Optimizer + (Zend O+),在PHP5.5的发行版本中自带了Zend O+,并重新命名为:Zend OPCache.但是默认是没有启用的,可以 ...

  5. android 编译模块

    android 编译模块 在写完.c文件之后,需要加载到android上进行测试.使用arm-linux-gcc编译,并添加到android开发板上运行失败. 由于android与linux不同,需要 ...

  6. 【BZOJ】1650: [Usaco2006 Dec]River Hopscotch 跳石子(二分+贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1650 看到数据和最小最大时一眼就是二分... 但是仔细想想好像判断时不能贪心? 然后看题解还真是贪心 ...

  7. 【vijos】1164 曹冲养猪(中国剩余定理)

    https://vijos.org/p/1164 好赞orz. 对于求一组线性同余方程 x=a[i](mod m[i]) 这里任意两个m[i]和m[j]都互质 那么可以用中国剩余定理来做. 对中国剩余 ...

  8. 整理混乱JS代码。

    一. 拷贝到MyEclipse的js文件 内然后 Ctrl+Shift+F 自动格式化代码. 二.百度搜索 :格式化js.

  9. 清除信号量队列导致zabbix自动关闭

    前几天在海外UCloud机器上部署了一套zabbix proxy和zabbix agentd,可是第二天一大早就收到邮件说zabbix_proxy挂掉了,上去查一下发现两台机器中的一台的proxy和a ...

  10. WinDbg 解决Font.ToLogFont AccessViolationExcetion

    有个程序总是在windows 2003 server 异常退出. 并且,  查看调用栈也肯奇怪, 应该是很正常的调用. 怀疑是堆溢出. 开启heap trace : C:\Program Files\ ...