Description
Input
Output
Sample Input
0 2
2 0
-2 0
0 -2
Sample Output
数据范围
36%的数据满足:n < = 500
64%的数据满足:n < = 30000
100%的数据满足:n < = 100000
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
struct ZYYS
{
int x,y;
}a[];
int t[],num,l[],r[],n;
lol c[],ans;
bool cmp(ZYYS a,ZYYS b)
{
if (a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
void update(int x,lol y)
{
while (x<=num)
{
c[x]+=y;
x+=(x&(-x));
}
}
lol query(int x)
{
lol s=;
while (x)
{
s+=c[x];
x-=(x&(-x));
}
return s;
}
int main()
{int i,ed,j;
cin>>n;
for (i=;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
t[++num]=a[i].x;t[++num]=a[i].y;
}
sort(t+,t+num+);
num=unique(t+,t+num+)-t-;
for (i=;i<=n;i++)
{
a[i].x=lower_bound(t+,t+num+,a[i].x)-t;
a[i].y=lower_bound(t+,t+num+,a[i].y)-t;
}
sort(a+,a+n+,cmp);
for (i=;i<=n;i++)
r[a[i].y]++;
ans=n;
for (i=;i<=n;i=ed)
{
ed=i;
while (ed<=n&&a[ed].x==a[i].x) ed++;
for (j=i;j<ed;j++)
{
int y=a[j].y;
if (l[y]==)
update(y,);
if (r[y]==)
update(y,-);
l[y]++;r[y]--;
if (j>i&&j<ed)
ans+=query(y-)-query(a[j-].y);
}
}
cout<<ans<<endl;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
struct ZYYS
{
int x,y;
}a[800001];
int t[800001],num,l[800001],r[800001],n;
lol c[800001],ans;
bool cmp(ZYYS a,ZYYS b)
{
if (a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
void update(int x,lol y)
{
while (x<=num)
{
c[x]+=y;
x+=(x&(-x));
}
}
lol query(int x)
{
lol s=0;
while (x)
{
s+=c[x];
x-=(x&(-x));
}
return s;
}
int main()
{int i,ed,j;
freopen("1818.in","r",stdin);
freopen("1818.out","w",stdout);
cin>>n;
for (i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
t[++num]=a[i].x;t[++num]=a[i].y;
}
sort(t+1,t+num+1);
num=unique(t+1,t+num+1)-t-1;
for (i=1;i<=n;i++)
{
a[i].x=lower_bound(t+1,t+num+1,a[i].x)-t;
a[i].y=lower_bound(t+1,t+num+1,a[i].y)-t;
}
sort(a+1,a+n+1,cmp);
for (i=1;i<=n;i++)
r[a[i].y]++;
ans=n;
for (i=1;i<=n;i=ed)
{
ed=i;
while (ed<=n&&a[ed].x==a[i].x) ed++;
for (j=i;j<ed;j++)
{
int y=a[j].y;
if (l[y]==0)
update(y,1);
if (r[y]==1)
update(y,-1);
l[y]++;r[y]--;
if (j>i&&j<ed)
ans+=query(y-1)-query(a[j-1].y);
}
}
cout<<ans<<endl;
}