Time Limit: 3 sec / Memory Limit: 1024 MB
Score : 400 points
Problem Statement
You are given N intervals of real numbers. The i-th interval is Find the number of pairs such that the i-th and j-th intervals intersect.
Constraints
-
All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
ll n;
cin>>n;
vector <int> l(n), r(n);
for(int i=0; i<n; ++i){
cin>>l[i]>>r[i];
}
sort(l.begin(),l.end());
sort(r.begin(),r.end());
ll ans=n*(n-1)/2;
for(int i=0,j=0;i<n;++i){
while(j<n&&r[j]<l[i])
j++;
ans-=j;
}
cout<<ans<<endl;
return 0;
}