| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
给pas的同学一点心得离散化,要将右端点加一
type
node=record
x,y,l,r,c:longint;
end;
var
a:array [1..10000,1..3] of longint;
b,c:array [0..20000] of longint;
tree:array [1..80000] of node;
flag:array [1..10001] of boolean;
cases,casesth,tot,sum,n,sum1,ans:longint;
procedure init;
var
i:longint;
begin
readln(n);
sum:=0; tot:=0; ans:=0;
fillchar(flag,sizeof(flag),false);
for i:=1 to n do
begin
readln(a[i,1],a[i,2]); inc(a[i,2]);
inc(sum); b[sum]:=a[i,1];
inc(sum); b[sum]:=a[i,2];
a[i,3]:=i+1;
end;
end;
procedure qsort(x,y:longint);
var
p,q,mid:longint;
begin
p:=x;
q:=y;
mid:=b[(x+y) div 2];
repeat
while b[x]<mid do inc(x);
while b[y]>mid do dec(y);
if x<=y then
begin
b[0]:=b[x];
b[x]:=b[y];
b[y]:=b[0];
inc(x);
dec(y);
end;
until x>y;
if p<y then qsort(p,y);
if x<q then qsort(x,q);
end;
procedure cut_repeat;
var
i,j,k:longint;
begin
sum1:=0;
for i:=1 to sum do
if b[i]<>b[i-1] then
begin
inc(sum1);
c[sum1]:=b[i];
end;
sum:=sum1;
end;
function two_find(x:longint):longint;
var
l,r,mid:longint;
begin
l:=1; r:=sum;
while l<=r do
begin
mid:=(l+r) div 2;
if c[mid]=x then exit(mid);
if c[mid]<x then l:=mid+1
else r:=mid-1;
end;
end;
procedure divided;
var
i,j:longint;
begin
for i:=1 to n do
begin
j:=two_find(a[i,1]); a[i,1]:=j;
j:=two_find(a[i,2]); a[i,2]:=j;
end;
end;
procedure buildtree(x,y:longint);
var
i:longint;
begin
inc(tot);
i:=tot;
tree[i].x:=x;
tree[i].y:=y;
tree[i].c:=1;
if x+1=y then exit;
tree[i].l:=tot+1;
buildtree(x,(x+y) div 2);
tree[i].r:=tot+1;
buildtree((x+y) div 2,y);
end;
procedure change(p,x,y,c:longint);
var
mid:longint;
begin
if tree[p].x+1=tree[p].y then begin tree[p].c:=c; exit; end;
if (x=tree[p].x) and (y=tree[p].y) then begin tree[p].c:=c; exit; end;
if tree[p].c>0 then
begin
tree[tree[p].l].c:=tree[p].c;
tree[tree[p].r].c:=tree[p].c;
tree[p].c:=-1;
end;
mid:=(tree[p].x+tree[p].y) div 2;
if x>=mid then change(tree[p].r,x,y,c)
else if y<=mid then change(tree[p].l,x,y,c)
else
begin
change(tree[p].l,x,mid,c);
change(tree[p].r,mid,y,c);
end;
end;
procedure main;
var
i,j:longint;
begin
for i:=1 to n do change(1,a[i,1],a[i,2],a[i,3]);
end;
procedure count(p:longint);
begin
if tree[p].c>0 then
begin
flag[tree[p].c]:=true;
exit;
end;
count(tree[p].l);
count(tree[p].r);
end;
procedure print;
var
i:longint;
begin
for i:=2 to n+1 do
if flag[i] then inc(ans);
writeln(ans);
end;
begin
readln(cases);
for casesth:=1 to cases do
begin
init;
qsort(1,sum);
cut_repeat;
divided;
buildtree(1,sum);
main;
count(1);
print;
end;
end.
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator