eolymp
bolt
Try our new interface for solving problems
Məsələlər

John`s Inversions

John`s Inversions

John has recently come across the definition of an inversion. An \textit{\textbf{inversion}} in a sequence of numbers \textbf{s_k} is any pair \textbf{s_i}, \textbf{s_j} such that \textbf{i} < \textbf{j} and \textbf{s_i} > \textbf{s_j}. John believes that inversions are a perfect tool for estimation of how well a sequence of numbers is sorted. The smaller the number of inversions in the sequence, the better it is sorted. For example, the sequences sorted in the ascending order have zero inversions in them. Peter gave John a stack of \textbf{n} cards. Each card has two numbers written on it - one in red and one in blue color. John is anxious to apply his knowledge of inversions to that stack. He places the cards in front of him in arbitrary order and calculates the total number of \textit{\textbf{nice inversions}} in front of him. John considers an inversion to be nice if it consists of the numbers of the same color. In our case nice inversion can be formed by either two blue or two red numbers. If the number of nice inversions is too big by John's standards, he rearranges the cards and repeats the process. Your task is to help John find out the minimal possible number of nice inversions he can get while following his algorithm. \InputFile The first line of the input file contains one integer number \textbf{n} - the number of cards in the deck (\textbf{1} ≤ \textbf{n} ≤ \textbf{100000}). The following \textbf{n} lines describe one card each. The \textbf{i}-th line contains two integer numbers \textbf{r_i} and \textbf{b_i} (\textbf{1} ≤ \textbf{r_i}, \textbf{b_i} ≤ \textbf{10^9}) - the numbers written on \textbf{i}-th card in red and blue colors respectively. \OutputFile Print the minimal possible number of nice inversions.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 256 MiB
Giriş verilənləri #1
3
10 3
20 2
30 1
Çıxış verilənləri #1
3
Giriş verilənləri #2
4
2 2
5 25
2 1
10 9
Çıxış verilənləri #2
1