poj 2398 2012-01-11
http://poj.org/problem?id=2398
___________________________________
题目大意:和poj2318一样,只是询问玩具数为x的区间有多少个。
先将区间排序一下,然后二分判断。
___________________________________
1 Program Stone; 2 var i,n,m,x1,x2,y1,y2,xi,yi:longint; 3 ui,li:array[0..10000]of longint; 4 ans,tot:array[0..10000]of longint; 5 procedure kp(t,w:longint); 6 var i,j,k,mid,mi:longint; 7 begin 8 i:=t;j:=w;mid:=ui[(t+w)div 2];mi:=li[(t+w)div 2]; 9 repeat 10 while (ui[i]<mid)or((ui[i]=mid)and(li[i]<mi)) do inc(i); 11 while (ui[j]>mid)or((ui[j]=mid)and(li[j]>mi)) do dec(j); 12 if i<=j then begin 13 k:=ui[i];ui[i]:=ui[j];ui[j]:=k; 14 k:=li[i];li[i]:=li[j];li[j]:=k; 15 inc(i);dec(j); 16 end; 17 until i>j; 18 if i<w then kp(i,w); 19 if j>t then kp(t,j); 20 end; 21 procedure init; 22 var i,j,k:longint; 23 begin 24 readln(n,m,x1,y1,x2,y2); 25 ui[0]:=x1;li[0]:=x1; 26 ui[n+1]:=x2;li[n+1]:=x2; 27 if n=0 then halt; 28 for i:=1 to n do readln(ui[i],li[i]); 29 kp(1,n); 30 end; 31 Function cross(x1,y1,x2,y2,x3,y3,x4,y4:longint):longint; 32 begin 33 cross:=(x2-x1)*(y4-y3)-(y2-y1)*(x4-x3); 34 end; 35 function ef(x,y:longint):longint; 36 var i,j,k,left,right,mid:longint; 37 begin 38 left:=0;right:=n+1; 39 while left+1<right do 40 begin 41 mid:=(left+right)div 2; 42 k:=cross(li[mid],y2,ui[mid],y1,li[mid],y2,x,y); 43 if k<0 then left:=mid; 44 if k>0 then right:=mid; 45 end; 46 ef:=left; 47 end; 48 Procedure print; 49 var i:longint; 50 begin 51 writeln('Box'); 52 for i:=0 to n do 53 inc(tot[ans[i]]); 54 for i:=1 to m do 55 if tot[i]<>0 then writeln(i,': ',tot[i]); 56 fillchar(tot,sizeof(tot),0); 57 fillchar(ans,sizeof(ans),0); 58 end; 59 Begin 60 assign(input,'input.in');reset(input); 61 while true do 62 begin 63 init; 64 for i:=1 to m do 65 begin 66 readln(xi,yi); 67 inc(ans[ef(xi,yi)]); 68 end; 69 print; 70 end; 71 end.
_____MildTheorem