扫描线思路,每遇到一个区间左端点,这个区间就与之前出现的且还没结束的所有区间相交。
样例全过,WA #12,求调玄关
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int n, sum, D[N * 2], cnt;
bool vis[N * 2];
long long ans;
struct node
{
int l, r;
}a[N];
struct haha
{
int l, r;
vector<int> whl, whr;
}ha[N * 2];
signed main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i].l >> a[i].r;
D[i] = a[i].l;
D[n + i] = a[i].r;
}
sort(D + 1, D + n * 2);
cnt = unique(D + 1, D + 1 + n * 2) - (D + 1);
for(int i = 1; i <= n; i++)
{
a[i].l = lower_bound(D + 1, D + 1 + cnt, a[i].l) - D;
a[i].r = lower_bound(D + 1, D + 1 + cnt, a[i].r) - D;
ha[a[i].l].l++;
ha[a[i].r].r++;
}
for(int i = 1; i <= cnt; i++)
{
if(ha[i].l)
{
for(int j = 1; j <= ha[i].l; j++)
{
ans += sum;
sum++;
}
}
if(ha[i].r)
{
for(int j = 1; j <= ha[i].r; j++)
sum--;
}
}
cout << ans;
return 0;
}