-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Intersecting_Intervals.cpp
More file actions
69 lines (61 loc) · 1.57 KB
/
Copy pathD_Intersecting_Intervals.cpp
File metadata and controls
69 lines (61 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// #include <bits/stdc++.h>
using namespace std;
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <iterator>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#define int long long
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// सुखदुःखे समे कृत्वा लाभालाभौ जयाजयौ। //
// ततो युद्धाय युज्यस्व नैवं पापमवाप्स्यसि॥ //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void solve()
{
int n,l,r, res = 0;
cin >> n;
vector<pair<int, int>> v(n);
vector<int> v1(n), v2(n);
for (int i = 0; i < n; i++)
{
cin >> l >> r;
v[i] = {l, r};
}
std::sort(begin(v), end(v),[&](const pair<int, int> &a, const pair<int, int> &b){
if(a.second == b.second)
return a.first < b.first;
else
return a.second < b.second;
});
for (int i = 0; i < n;i++){
v1[i] = v[i].first;
v2[i] = v[i].second;
}
for (int i = 0; i < n; i++)
{
int x = upper_bound(begin(v1)+i, end(v1), v2[i]) - (begin(v1)) - i - 1;
res += x;
}
cout << res << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// cout<<fixed<<setprecision(0);
int t = 1;
// cin >> t;
while (t--)
solve();
}