def f3(n,rs,ls) rs3=n.times.map{0} ls3=n.times.map{0} rs.each{|e| rs3[e[0]]=1 rs3[e[1]]=-1 } ls.each{|e| ls3[e[1]]=1 ls3[e[0]]=-1 } rc=0 lc=0 ans=0 n.times{|i| if rs3[i]==0 if ls3[i]==1 ans+=rc elsif ls3[i]==-1 lc-=1 end end if ls3[i]==0 if rs3[i]==1 ans+=lc elsif rs3[i]==-1 rc-=1 end end if rs3[i]==1 && ls3[i]==1 ans+=rc+1+lc rc+=1 lc+=1 end if rs3[i]==-1 && ls3[i]==-1 rc-=1 lc-=1 end #p [i,rc,lc,ans] } return ans end def fadd(tree,l,r,p1,p2,add) if r==l tree[p1]+=add else m=(r+l)/2 if (p2<=m) fadd(tree,l,m,p1*2+1,p2,add) else fadd(tree,m+1,r,p1*2+2,p2,add) end tree[p1]+=add end end def fsum(tree,l,r,p1,r1) if r10 ans+=t end end end #p [tree,ans,no,d] end return ans end #n2=131071 n2=31 tree=n2.times.map{0} n=gets.to_i xs=n.times.map{gets.to_i-1} xs2=n.times.map{|i| [i,xs[i]] } rs=xs2.select{|e| e[0]<=e[1] } ls=xs2.select{|e| e[1]<=e[0] } rs2=xs2.select{|e| e[0]