A Pie for a Pie

提交数: 11, 通过率: 45.45%, 平均分: 60

题目描述:

Bessie and Elsie have each baked N pies (1≤N≤10^5). Each of the 2N pies has a tastiness value acco
rding to Bessie and a (possibly different) tastiness value according to Elsie.Bessie is thinking abo
ut giving one of her pies to Elsie. If Elsie receives a pie from Bessie, she will feel obligated to 
give one of her pies to Bessie. So as to not appear stingy nor flamboyant, Elsie will try to pick a 
pie that is at least as tasty (in Elsie's eyes) as the pie she received, but no more than D units ta
stier (0≤D≤10^9). Such a pie may not exist, in which case Elsie will adopt a pseudonym and exile h
erself to Japan.But if Elsie does give Bessie a pie in return, Bessie will similarly try to give Els
ie a pie which is at least as tasty but no more than DD units tastier (in Bessie's eyes) as the pie 
Elsie just gave her. Should this be impossible, Bessie too will exile herself. Otherwise she will gi
ve her chosen pie to Elsie. This cycle will continue until one of the cows is exiled, an unhappy out
come, or one of the cows receives a pie which she accords a tastiness value of 0, in which case the 
gift exchange will end and both cows will be happy.Note that a pie may not be gifted twice, nor can 
either cow return a pie gifted to her.For each of the NN pies Bessie could select as her initial gif
t to Elsie, determine the minimum number of pies that could possibly be gifted in the resulting exch
ange before the cows are happy.
两只牛,牛A和牛B,他们对水果派的品味不同,每个人都有n<=1e5个派,每个派都有两种得分a[i], b[i],其中a[
i]是牛A对该派的评分,b[i]是牛B的评分, 0 <= a[i], b[i] <= 1e9。两只牛开始礼尚往来,从牛A开始,它选一
个pie给牛B,设这个pie的评分是(a[i], b[i])。此时牛B为了不失礼节(不太小气,也不要太谄媚)要选出一个B
评分在[b[i], b[i]+d]范围内的pie(a[j], b[j]),给A。同理,A又要选出一个A评分在[a[j], a[j]+d]范围内的pi
e给B,如此往复。每个pie都只能传递一次。如果A收到了一个A评分为0的pie,或者B收到了一个B评分为0的pie,
那么礼尚往来就愉快的结束了。否则,就不愉快结束(即一个人无法选出合法的pie给对方时)。下面让你输出,
对于A的每一个pie,如果第一轮A把这个pie交给B,礼尚往来最少可以几轮愉快结束?如果无法愉快结束,就输出-1

输入格式:

The first line contains the two integers N and D.
The next 2N lines contain two space-separated integers each, 
respectively denoting the value of a particular pie according to Bessie, and the value of that pie according to Elsie.
The first N lines refer to Bessie's pies, and the remaining N lines refer to Elsie's pies.
It is guaranteed that all tastiness values are in the range [0,10^9]

输出格式:

There should be N lines in the output. 
Line i should contain a single integer: the minimum number of pies that could be gifted in a happy gift 
exchange started with Bessie's pie i. 
If no gift exchange starting with pie i is happy, then line i should contain the single integer -1 instead.

样例输入:

2 1
1 1
5 0
4 2
1 4

样例输出:

3
1
时间限制: 1000ms
空间限制: 128MB

来源: Usaco2017 Dec Gold