Friday, 11 September 2015

*Chef and Reunion

Chef and Reunion

PROBLEM:

There are N males and N females (N<=10^5). Each male and female have indexes (unique for all males and unique for all females). All males are arranged in one line (standing at equal distances) according to increasing value of indexes. Similarily for females in another line.
Now pairings are done(between males and females) and line is drawn between each pair. We have to find how many pair of lines intersect each other.

OR
Today is the reunion of all chefs in the world. Our Chef wants to make this moment more happier. He arranged a mass wedding in this reunion. For this, he made a strange stage and drew two horizontal parallel lines on the stage. There are N unmarried male chefs in the reunion and he gave each male chef i an unique number Mi. Then all male chefs will stand in the first line drawn by Chef. But they will stand in increasing order of their number. That means chef with the lowest number will stand at the leftmost position of the line, then right to him would be the chef with the second lowest number and so on. Similarly, there are N female chefs in the reunion and Chef also gave each female chef j an unique number Fj (sequences Fj and Mi can have equal numbers). Then all female chefs will stand in the other line following the same rule(will stand in increasing order of the numbers) as the male chef.
Now chef will choose all the marriage pairs himself. He will select a female chef and a male chef (both of them have not selected before) and will draw a straight line between them. He calls this line a marriage line. He will do this for the rest of the chefs.
You will be given the N marriage lines; you have to find how many marriage line pairs intersect with each other.

Input

First line contains a single integer N. The i-th line of the next N lines contain two space separated integers Mi and Fi, means there is a marriage line between male chef Mi and female chef Fi. No marriage line will be mentioned twice.

Output

Output the number of marriage line pairs that intersect with each other on a single line.

Constraints

  • 1 ≤ N ≤ 100000 (105)
  • 0 ≤ Mi, Fi ≤ 1000000000 (109)

Example

Input:
3
2 3
3 6
5 4

Output:
1

Input:
4
5 12
10 11
11 9
30 1

Output:
6


Explanation

Example case 1. Only marriage lines (3, 6) and (5, 4) intersect with each other.
Example case 2. All the marriage lines intersect with each other.

*******************************************editorial*************************************************************************************
we need to calculate inversion count
we consider male value as index and females value = value at that index 
since male's value is value so wee need to sort  given pairs according to male (sice index must be increasing )
now just count no of inversion  in females value (after sorting according to  male )..
*************************************************code******************************************************************************
 #include<iostream>
using namespace std;
 typedef long long int lli;
 #include<bits/stdc++.h>
 lli ic=0;
lli merge (lli arr[],lli start,lli mid,lli end)
   {
   
     lli i=start, j=mid+1;
     lli p=start;
      lli temp[100000];
      lli lc=0;
       while(i<=mid && j<=end)
        {
       
        if(arr[i]>arr[j])
        {
        lc+=mid-i+1;
        temp[p++]=arr[j];
        j++;
}
else
{
temp[p++]=arr[i++];
}
       
}
while(i<=mid) temp[p++]=arr[i++];
while(j<=end) temp[p++]=arr[j++];
for(int i=start;i<p;i++) arr[i]=temp[i];
return lc;
   }
  
 void  divide(lli arr[],lli start,lli end)
  {
   
  if(start<end)
  {
   
  lli mid=(start+end)/2;
   
  divide(arr,start,mid);
  divide(arr,mid+1,end);
  ic+=merge(arr,start,mid,end);
  }
   

  }
  
 int  main()
  {
 
   
    ic=0;
    lli n;
    cin>>n;
   lli arr[n+10];
   vector<pair<lli,lli> > v;
    for(int i=0;i<n;i++)
     {
        lli a,b;
         cin>>a>>b;
         v.push_back(make_pair(a,b));
        
 }
 sort(v.begin(),v.end());
  for(int i=0;i<n;i++)
   arr[i]=v[i].second;
 divide(arr,0,n-1);
  cout<<ic<<endl;
return 0;
  }

No comments:

Post a Comment