11
5
2015
0

[Codeforces Round #322] Kojiro and Furrari


E. Kojiro and Furrari
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Motorist Kojiro spent 10 years saving up for his favorite car brand, Furrari. Finally Kojiro's dream came true! Kojiro now wants to get to his girlfriend Johanna to show off his car to her.

Kojiro wants to get to his girlfriend, so he will go to her along a coordinate line. For simplicity, we can assume that Kojiro is at the point fof a coordinate line, and Johanna is at point e. Some points of the coordinate line have gas stations. Every gas station fills with only one type of fuel: Regular-92Premium-95 or Super-98. Thus, each gas station is characterized by a pair of integers ti and xi — the number of the gas type and its position.

One liter of fuel is enough to drive for exactly 1 km (this value does not depend on the type of fuel). Fuels of three types differ only in quality, according to the research, that affects the lifetime of the vehicle motor. A Furrari tank holds exactly s liters of fuel (regardless of the type of fuel). At the moment of departure from point f Kojiro's tank is completely filled with fuel Super-98. At each gas station Kojiro can fill the tank with any amount of fuel, but of course, at no point in time, the amount of fuel in the tank can be more than s liters. Note that the tank can simultaneously have different types of fuel. The car can moves both left and right.

To extend the lifetime of the engine Kojiro seeks primarily to minimize the amount of fuel of type Regular-92. If there are several strategies to go from f to e, using the minimum amount of fuel of type Regular-92, it is necessary to travel so as to minimize the amount of used fuel of type Premium-95.

Write a program that can for the m possible positions of the start fi minimize firstly, the amount of used fuel of type Regular-92 and secondly, the amount of used fuel of type Premium-95.

Input

The first line of the input contains four positive integers e, s, n, m (1 ≤ e, s ≤ 109, 1 ≤ n, m ≤ 2·105) — the coordinate of the point where Johanna is, the capacity of a Furrari tank, the number of gas stations and the number of starting points.

Next n lines contain two integers each ti, xi (1 ≤ ti ≤ 3,  - 109 ≤ xi ≤ 109), representing the type of the i-th gas station (1 representsRegular-922 — Premium-95 and 3 — Super-98) and the position on a coordinate line of the i-th gas station. Gas stations don't necessarily follow in order from left to right.

The last line contains m integers fi ( - 109 ≤ fi < e). Start positions don't necessarily follow in order from left to right.

No point of the coordinate line contains more than one gas station. It is possible that some of points fi or point e coincide with a gas station.

Output

Print exactly m lines. The i-th of them should contain two integers — the minimum amount of gas of type Regular-92 and type Premium-95, if Kojiro starts at point fi. First you need to minimize the first value. If there are multiple ways to do it, you need to also minimize the second value.

If there is no way to get to Johanna from point fi, the i-th line should look like that "-1 -1" (two numbers minus one without the quotes).

Sample test(s)
input
8 4 1 1
2 4
0
output
0 4
input
9 3 2 3
2 3
1 6
-1 0 1
output
-1 -1
3 3
3 2
input
20 9 2 4
1 5
2 10
-1 0 1 2
output
-1 -1
-1 -1
-1 -1
-1 -1

题目大意:在一条一维坐标轴上,给出终点e和油箱容量s。有n个加油站,给出它们的位置和提供的汽油的种类(1~3),每个加油站可以提供无限多的该品种汽油。有m个询问,每个询问给出一个起点f,求从这个点以满油箱出发去终点,最少使用多少1号油以及使用最少的1号油的前提下最少使用多少2号油。(保证起点在终点左侧)。车子可以同时装不同种的汽油,1单位汽油可以走1单位路程。


用DP分别求出从某个加油站以空油箱出发,用1、2、3号油,只用2、3号油,只用3号油,到达终点所需的额外汽油量。

对于每个询问,把起点看成一个提供3号油的加油站,二分出离它最近的下一个加油站,求出从起点出发到终点的上面3个DP值,分别设为ans1,ans2,ans3。若$ans3>0$则不能到达终点,否则所求的两个值分别为$ans2$和$ans3-ans2$。

代码在此。

Category: 题解 | Tags: DP 二分 Codeforces | Read Count: 419

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com