module algorithm_md contains recursive subroutine qsort(a,s,t) implicit none integer*8::a(:),m integer::s,t,i,j if(t-s<=1)return i=s j=t-1 m=a((s+t)/2) do do while(a(i)m) j=j-1 end do if(i>=j)exit a(i)=xor(a(i),a(j)) a(j)=xor(a(i),a(j)) a(i)=xor(a(i),a(j)) i=i+1 j=j-1 end do call qsort(a,s,i) call qsort(a,j+1,t) end subroutine function binarysearch(a,v) integer*8::a(:),v integer::ok,ng,middle,binarysearch ok=1 ng=size(a)+1 do while(ng-ok>1) middle=(ok+ng)/2 if(a(middle)<=v)then ok=middle else ng=middle end if end do binarysearch=ok if(a(ok)/=v)then print*,"err" call exit(1) end if end function end module module djset_md private type,public::DJSet integer::n integer*8::ans=0 integer,allocatable::upper(:) integer*8,allocatable::mi(:),ma(:),top(:) contains procedure::build=>build_ procedure::root=>root_ procedure::setUnion=>setUnion_ procedure::equiv=>equiv_ procedure::confluentUnion=>confluentUnion_ end type contains subroutine build_(o,n_) implicit none integer::i,j,k class(DJSet)::o integer::n_ o%n=n_ allocate(o%upper(o%n)) allocate(o%ma(o%n)) allocate(o%mi(o%n)) allocate(o%top(o%n)) do i=1,o%n o%upper(i)=-1 o%top(i)=i end do end subroutine recursive function root_(o,x)result(res) implicit none class(DJSet)::o integer::x,res if(o%upper(x)<0)then res=x else o%upper(x)=o%root(o%upper(x)) res=o%upper(x) end if end function subroutine confluentUnion_(o,x,y) implicit none class(DJSet)::o integer::x,y,middle if(x>y)then x=xor(x,y) y=xor(x,y) x=xor(x,y) end if do while(.not.o%equiv(x,y)) !x