题解:一个萌萌哒RMQ问题(HansBug:吐槽下翻译——输入里面明明说了the location is distinct,也就是说每只牛坐标唯一,这样子虽然没有实质性变化,但是简化了不少问题,毕竟题目说是左边和右边的牛,没说此位置上的= =),将坐标排序,然后就是区间查询最大值啦,然后没了
1 /**************************************************************
2 Problem: 3314
3 User: HansBug
4 Language: Pascal
5 Result: Accepted
6 Time:608 ms
7 Memory:6008 kb
8 ****************************************************************/
9
10 var
11 i,j,k,l,m,n:longint;
12 a,b:array[0..100000,1..2] of longint;
13 c:array[0..17,0..60000] of longint;
14 function max(x,y:longint):longint;
15 begin
16 if x>y then max:=x else max:=y;
17 end;
18 procedure swap(var x,y:longint);
19 var z:longint;
20 begin
21 z:=x;x:=y;y:=z;
22 end;
23 procedure sort(l,r:longint);
24 var i,j,x:longint;
25 begin
26 i:=l;j:=r;x:=a[(l+r) div 2,1];
27 repeat
28 while a[i,1]<x do inc(i);
29 while a[j,1]>x do dec(j);
30 if i<=j then
31 begin
32 swap(a[i,1],a[j,1]);
33 swap(a[i,2],a[j,2]);
34 inc(i);dec(j);
35 end;
36 until i>j;
37 if i<r then sort(i,r);
38 if l<j then sort(l,j);
39 end;
40 function getmax(x,y:longint):longint;
41 var i:longint;
42 begin
43 if y<x then exit(-1);
44 i:=trunc(ln(y-x+1)/ln(2));
45 exit(max(c[i,x],c[i,y-trunc(exp(ln(2)*i))+1]));
46 end;
47 begin
48 readln(n,m);
49 for i:=1 to n do readln(a[i,1],a[i,2]);
50 sort(1,n);
51 for i:=1 to n do c[0,i]:=a[i,2];
52 for i:=1 to trunc(ln(n)/ln(2)+1) do
53 for j:=1 to n-trunc(exp(ln(2)*i))+1 do
54 c[i,j]:=max(c[i-1,j],c[i-1,j+trunc(exp(ln(2)*(i-1)))]);
55 k:=1;fillchar(b[1],sizeof(b[1]),0);
56 for i:=2 to n do
57 begin
58 while (a[k,1]+m)<a[i,1] do inc(k);
59 if getmax(k,i-1)>=(a[i,2]*2) then b[i,1]:=1;
60 end;
61 k:=n;fillchar(b[1],sizeof(b[1]),0);
62 for i:=n-1 downto 1 do
63 begin
64 while (a[k,1]-m)>a[i,1] do dec(k);
65 if getmax(i+1,k)>=(a[i,2]*2) then b[i,2]:=1;
66 end;
67 l:=0;for i:=1 to n do inc(l,(b[i,1]+b[i,2]) div 2);
68 writeln(l);
69 readln;
70
71 end.