算法:BIT
分析:基本上算裸的BIT了,以前也做过类似的题,可是又忘了……哎。
分析:基本上算裸的BIT了,以前也做过类似的题,可是又忘了……哎。
先按x关键字排序,若x相等即按y排序,y小的放在前面,这样我们在顺序统计的时候只要计算当前的y之前有多少个比它小的,再用i减去即使比它大的,也就是交点的个数。
program POJ3067;
const
maxn=1000000;
type
atp=record
x,y:longint;
end;
var
test,n,m,bian,tem1:longint;
ans:int64;
a:array [0..maxn] of longint;
map:array [0..maxn] of atp;
procedure qsort(l,r:longint);
var
i,j:longint;
tt,t:atp;
begin
i:=l;
j:=r;
t:=map[(l+r) shr 1];
repeat
while (map[i].x<t.x) or ((map[i].x=t.x) and (map[i].y<t.y)) do inc(i);
while (map[j].x>t.x) or ((map[j].x=t.x) and (map[j].y>t.y)) do dec(j);
if i<=j then
begin
tt:=map[i];
map[i]:=map[j];
map[j]:=tt;
inc(i);
dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
procedure init;
var
i:longint;
begin
fillchar(a,sizeof(a),0);
ans:=0;
readln(n,m,bian);
for i:=1 to bian do readln(map[i].x,map[i].y);
qsort(1,bian);
end;
procedure insert(x:longint);
begin
while x<=m do
begin
inc(a[x]);
inc(x,(x and (-x)));
end;
end;
function find(x:longint):longint;
begin
find:=0;
while x>=1 do
begin
inc(find,a[x]);
dec(x,(x and (-x)));
end;
end;
procedure main;
var
i:longint;
begin
for i:=1 to bian do
begin
insert(map[i].y);
inc(ans,i-find(map[i].y));
end;
end;
begin
readln(test);
for tem1:=1 to test do
begin
init;
main;
writeln('Test case ',tem1,': ',ans);
end;
end.
本文通过一道具体的题目,介绍了BIT(二进制索引树)算法的应用。文章首先对输入数据进行排序处理,然后利用BIT算法高效地计算出交点数量,最后提供了完整的Pascal语言实现代码。
256

被折叠的 条评论
为什么被折叠?



