2 a:array [0..10000] of longint;
3 data:array [0..1000,1..2] of longint;
4 f:array [0..1000] of longint;
5 n,i,j,max1,maxk1,max2,maxk2,ans,k:longint;
7 procedure qsort(l,r:longint);
13 m:=data[(l+r) div 2,1];
15 while data[i,1]<m do inc(i);
16 while data[j,1]>m do dec(j);
19 data[0]:=data[i]; data[i]:=data[j]; data[j]:=data[0];
23 if l<j then qsort(l,j);
24 if r>i then qsort(i,r);
29 for i:=1 to n do readln(data[i,1],data[i,2]);
32 if data[i,1]=data[i+1,1] then
34 if data[i,2]<data[i+1,2] then
36 data[i+1,1]:=maxint; data[i+1,2]:=maxint;
40 data[i,1]:=maxint; data[i,2]:=maxint;
44 while data[n,1]=maxint do dec(n);
46 for j:=data[i,1] to data[i,2] do
48 fillchar(f,sizeof(f),0);
51 if f[i]>=2 then continue;
52 max1:=a[data[i,1]]; maxk1:=data[i,1];
53 max2:=a[data[i,2]]; maxk1:=data[i,2];
54 for k:=data[i,1]+1 to data[i,2]-1 do
55 if a[k]>max1 then begin max2:=max1; maxk2:=maxk1; max1:=a[k]; maxk1:=k; end
56 else if a[k]>max2 then begin max2:=a[k]; maxk2:=k; end;
59 inc(ans); a[maxk2]:=0;
61 while (maxk2>=data[j,1]) and (maxk2<=data[j,2]) do
66 inc(ans); a[maxk1]:=0;
68 while (maxk1>=data[j,1]) and (maxk1<=data[j,2]) do