显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
int n,m,c[2*N];
struct po{
int x,y;
}p[N];
void add(int x,int y){for(;x<=m;x+=x&-x) c[x]+=y;}
int ask(int x){
int y=0;
for(;x;x-=x&-x) y+=c[x];
return y;
}
bool cmp(po a,po b){
if(a.y==b.y) return a.x<b.x;
return a.y>b.y;
}
int b[N*2],cnt;
signed main(){
freopen("Tokitsukazee.in","r",stdin);
freopen("Tokitsukazee.out","w",stdout);
scanf("%lld",&n);
for(int i = 1;i<=n;i++){
scanf("%lld%lld",&p[i].x,&p[i].y);
b[++cnt]=p[i].x; b[++cnt]=p[i].y;
}
sort(b+1,b+1+cnt); sort(p+1,p+1+n,cmp);
m=unique(b+1,b+1+cnt)-b-1;
for(int i = 1;i<=n;i++)
p[i].x=lower_bound(b+1,b+1+m,p[i].x)-b,
p[i].y=lower_bound(b+1,b+1+m,p[i].y)-b;
int ans=0;
for(int i = 1;i<=n;i++){
if(ask(p[i].x)-ask(p[i].x-1)==0) add(p[i].x,1);
if(p[i].y==p[i+1].y)
ans+=ask(p[i].x)*(ask(p[i+1].x-1)-ask(p[i].x-1));
else ans+=ask(p[i].x)*(ask(m)-ask(p[i].x-1));
}
printf("%lld",ans);
}