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. union共用体的对齐

    union DATE { char a; ]; double b; }; DATE max; cout<< sizeof(max) << endl; 这个问题很好回答,并且我把 ...

  2. Unity3D动画面板编辑器状态属性对照表

    不推荐用AnimationUtility.SetEditorCurve问题很多,推荐AnimationCurve.AddKey.通过AnimationUtility.GetAllCurves可以获得编 ...

  3. 集合运算 蓝桥杯 set容器

    题目描述 给出两个整数集合A.B,求出他们的交集.并集以及B在A中的余集. 输入格式 第一行为一个整数n,表示集合A中的元素个数. 第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素. 第三行 ...

  4. Qt禁止调整窗口的大小

    项目中使用的是基于对话框的程序,所以限制调整窗口大小会比较合适,下面是两种方法. 1.使用代码修改 #include "dialog.h" #include "ui_di ...

  5. 【转】CentOS下expect 安装

      Expect是在Tcl基础上创建起来的,它还提供了一些Tcl所没有的命令,它可以用来做一些linux下无法做到交互的一些命令操作,在远程管理方面发挥很大的作用. spawn命令激活一个Unix程序 ...

  6. Phoenix的数据类型和操作符、函数

    其实官方文档已经有这些东西了,如下: http://phoenix.apache.org/language/functions.html http://phoenix.apache.org/langu ...

  7. 一题关于PHP的CTF

    if(isset($_GET['time'])){ if(!is_numeric($_GET['time'])){ echo 'The time must be number.'; }else if( ...

  8. linux - fpga-framebuff驱动

    * linux/drivers/video/fpga_fb.c --fpga graphics adaptor frame buffer device *  Created 16 Sep2011 *  ...

  9. 微信小程序6 - 页面之间传参及通知系统封装

    1. 简单传参 wx.navigateTo({ url: '/pages/demo/index/index?id=1' }) /pages/demo/index/index.js 中 onLoad(o ...

  10. C++ 匿名对象产生场景

    //匿名对象产生的三种场景 #include<iostream> using namespace std; class Point{ public: Point(int a,int b){ ...