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. Delphi记录record中的变体

    program Day4; {$APPTYPE CONSOLE} uses SysUtils, Util in 'Util.pas'; type TPerson = packed record ID ...

  2. 微软七届MVP桂素伟:移动互联网与职业规划

    原文地址:http://student.csdn.net/mcd/topic/163587/955481 2014年10月19日在哈尔滨工业大学举办了CSDN高校俱乐部全国巡讲. 此次邀请到了微软七届 ...

  3. Ubuntu14.04.3 64位环境下openjdk7编译

    系统环境:Ubuntu14.04.3 -amd64 in VMWare1.安装openjdk7依赖 sudo apt- sudo apt--jdk sudo apt-get install build ...

  4. python中sorted方法和列表的sort方法使用详解

    一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...

  5. 基于HTML5坦克大战游戏简化版

    之前我们有分享过不少经典的HTML5游戏,有些还是很有意思的,比如HTML5版切水果游戏和HTML5中国象棋游戏.今天要分享的是一款简化版的HTML5坦克大战游戏,方向键控制坦克的行进方向,空格键发射 ...

  6. netifd

    Netifd是OpenWrt中用于进行网络配置的守护进程,基本上所有网络接口设置以及内核的netlink事件都可以由netifd来处理完成. 在启动netifd之前用户需要将所需的配置写入uci配置文 ...

  7. eclipse代码补全按键修改成Tab

    https://www.eclipse.org/downloads/compare.php?release=oxygen 下载eclipse带有源文件的版本 打开Eclipse,点击 window - ...

  8. koa介绍

    https://github.com/koajs/koa https://github.com/demopark/koa-docs-Zh-CN ctx.response.type = 'json'; ...

  9. Toad 补充与培训 & 常用菜单

    Toad 常用菜单 新版本 toad 软件中, 比较有用的菜单 (toad10.6 版本) 下边菜单, 在日常工作中出现过的, 显示为 粉色 , 蓝色 表示次一级的重要 session 菜单 new ...

  10. Thinkphp3.2 PHPMailer 发送 QQ邮箱 163邮箱

    在进入正题这前先看下网易(163)邮箱的服务器地址和端口号 类型 服务器名称 服务器地址 SSL协议端口号 非SSL协议端口号 收件服务器 POP pop.163.com 995 110 收件服务器 ...