const tw:array [1..11] of integer=(1,2,4,8,16,32,64,128,256,512,1024);var a:array [1..500,1..2] of longint; y,z:array [1..500] of longint; n,i,j,ss,en:integer; t:boolean; fr,la:longint; tree:array [1..2000] of boolean;procedure paixu(r,c:longint); var i,j,k:longint; begin if r>=c then exit; i:=r; j:=c; k:=z[r]; repeat while (z[j]>=k)and(i
y[ss] then begin inc(ss);y[ss]:=z[i];end; end;end;procedure ins(p,l,r,a,b:longint);var m:integer;begin if tree[p] then begin if (l=a) and (r=b) then tree[p]:=false else begin m:=(l+r) div 2; if b<=m then ins(p*2,l,m,a,b) else begin if a>=m then ins(p*2+1,m,r,a,b) else begin ins(p*2,l,m,a,m);ins(p*2+1,m,r,m,b);end; end; end; end;end;function co(p,l,r:integer):longint;begin if tree[p]=false then co:=y[r]-y[l] else begin if r-l=1 then co:=0 else co:=co(p*2,l,(l+r) div 2)+co(p*2+1,(l+r) div 2,r); end;end;begin readln(n); for i:=1 to n do begin read(a[i,1],a[i,2]);z[i*2-1]:=a[i,1];z[i*2]:=a[i,2];end; paixu(1,n*2); fang; fillchar(tree,sizeof(tree),true); for en:=1 to 11 do if tw[en]+1>=ss then break; for i:=1 to n do begin t:=true; for j:=1 to ss do begin if t then begin if a[i,1]=y[j] then begin fr:=j;t:=false;end; end else if a[i,2]=y[j] then la:=j; end; ins(1,1,tw[en]+1,fr,la); end; writeln(co(1,1,tw[en]+1));end.
2008年06月09日 12点06分
4