平面中n个点,求每个点左下点的数量,直接上树状数组,题目数据以y为第一关键字,x为第二关键字升序排列了,所以只需从1到n依次插入横坐标,统计之前的小于当前点的横坐标的点数即可,注意树状数组上限是坐标最大值,不是n,同时坐标还有0的可能,所以都加1才行。
View Code
1 program pku2352(input,output); 2 var 3 c : array[0..40000] of longint; 4 x,y,answer : array[0..40000] of longint; 5 n,maxx : longint; 6 procedure init; 7 var 8 i : longint; 9 begin10 maxx:=0;11 readln(n);12 for i:=1 to n do13 begin14 readln(x[i],y[i]);15 inc(x[i]);16 if x[i]>maxx then17 maxx:=x[i];18 end;19 end; { init }20 function lowbit(x :longint ):longint;21 begin22 exit(x and(-x));23 end; { lowbit }24 procedure insect(pos,w: longint );25 begin26 while pos<=maxx do27 begin28 inc(c[pos],w);29 pos:=pos+lowbit(pos);30 end;31 end; { insect }32 function find(pos :longint ):longint;33 begin34 find:=0;35 while pos>0 do36 begin37 inc(find,c[pos]);38 pos:=pos-lowbit(pos);39 end;40 end; { find }41 procedure main;42 var43 i : longint;44 begin45 for i:=1 to n do46 begin47 inc(answer[find(x[i])]);48 insect(x[i],1);49 end;50 end; { main }51 procedure print;52 var53 i : longint;54 begin55 for i:=0 to n-1 do56 writeln(answer[i]);57 end; { print }58 begin59 init;60 main;61 print;62 end.