题库练习 Processing Queries
← 上一题 下一题 →

A10317 | Processing Queries

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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 $t_{i}$ and needs to be processed for $d_{i}$ units of time. All $t_{i}$ are guaranteed to be distinct.

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

1. If server is free and query queue is empty, then server immediately starts to process this query.
2. 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.
3. 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.

输入格式

The first line of the input contains two integers $n$ and $b$ ( $1<=n,b<=200000$ ) — 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 $t_{i}$ and $d_{i}$ ( $1<=t_{i},d_{i}<=10^{9}$ ), where $t_{i}$ is the moment of time when the $i$ -th query appears and $d_{i}$ is the time server needs to process it. It is guaranteed that $t_{i-1}<t_{i}$ for all $i>1$ .

输出格式

Print the sequence of $n$ integers $e_{1},e_{2},...,e_{n}$ , where $e_{i}$ 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.

输入输出样例

输入 #1
5 1
2 9
4 8
10 9
15 2
19 1
输出 #1
11 19 -1 21 22 
输入 #2
4 1
2 8
4 8
10 9
15 2
输出 #2
10 18 27 -1 
C++ 编辑器
输入
输出