Description
Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(i) and has height h(i) (1 <= x(i),h(i) <= 1,000,000,000). A cow feels "crowded" if there is another cow at least twice her height within distance D on her left, and also another cow at least twice her height within distance D on her right (1 <= D <= 1,000,000,000). Since crowded cows produce less milk, Farmer John would like to count the number of such cows. Please help him.
N头牛在一个坐标轴上,每头牛有个高度。现给出一个距离值D。
如果某头牛在它的左边,在距离D的范围内,如果找到某个牛的高度至少是它的两倍,且在右边也能找到这样的牛的话。则此牛会感觉到不舒服。
问有多少头会感到不舒服。
Input
* Line 1: Two integers, N and D.
* Lines 2..1+N: Line i+1 contains the integers x(i) and h(i). The locations of all N cows are distinct.
Output
* Line 1: The number of crowded cows.
Sample Input
10 3
6 2
5 3
9 7
3 6
11 2
INPUT DETAILS: There are 6 cows, with a distance threshold of 4 for feeling crowded. Cow #1 lives at position x=10 and has height h=3, and so on.
Sample Output
OUTPUT DETAILS: The cows at positions x=5 and x=6 are both crowded.
{$inline on}
var
d1,d2,a,b:array[0..60000]of longint;
t1,w1,t2,w2,d,ans,now,ed,n,m,i,j,k:longint;
procedure swap(var a,b:longint);
var
t:longint;
begin
t:=a; a:=b; b:=t;
end;
procedure kp(l,r:longint);
var
mm,i,j,m,t:longint;
begin
i:=l;j:=r;m:=a[(l+r) div 2];mm:=b[(l+r) div 2];
while i<=j do
begin
while (a[i]<m)or((m=a[i])and(b[i]<mm)) do inc(i);
while (a[j]>m)or((m=a[j])and(b[j]>mm)) do dec(j);
if i<=j then
begin
swap(a[i],a[j]);
swap(b[i],b[j]);
inc(i); dec(j);
end;
end;
if i<r then kp(i,r);
if j>l then kp(l,j);
end;
procedure insert1(x:longint);inline;
begin
while (b[d1[w1]]<b[x])and(w1>=t1) do
dec(w1);
inc(w1);
d1[w1]:=x;
end;
procedure insert2(x:longint);inline;
begin
while (b[d2[w2]]<b[x])and(w2>=t2) do
dec(w2);
inc(w2);
d2[w2]:=x;
end;
procedure doit(x:longint);inline;
var
i,j,k:longint;
begin
while (t1<w1)and(a[x]-a[d1[t1]]>d) do
inc(t1);
while (t2<w2)and((a[d2[t2]]-a[x]>d)or(a[d2[t2]]-a[x]<0))do
inc(t2);
while (a[now]-a[x]<=d)and(now<=n) do
begin
insert2(now);
inc(now);
end;
if b[d1[t1]]>=b[x]*2 then
if b[d2[t2]]>=b[x]*2 then
inc(ans);
insert1(x);
end;
begin
readln(n,d);
for i:=1 to n do
readln(a[i],b[i]);
kp(1,n);
now:=2; t1:=1; t2:=1;
for i:=1 to n do
doit(i);
writeln(ans);
end.