Describe:
There are n points on the plane, the i-th of which is at (xi,yi). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area.
The strange area is enclosed by three lines, x=l, y=a and x=r, as its left side, its bottom side and its right side respectively, where l, r and a can be any real numbers satisfying that l<r. The upper side of the area is boundless, which you can regard as a line parallel to the x-axis at infinity. The following figure shows a strange rectangular area.
A point (xi,yi) is in the strange rectangular area if and only if l<xia. For example, in the above figure, p1 is in the area while p2 is not.
Tokitsukaze wants to know how many different non-empty sets she can obtain by picking all the points in a strange rectangular area, where we think two sets are different if there exists at least one point in one set of them but not in the other.
Input
The first line contains a single integer n (1≤n≤2×105) — the number of points on the plane.
The i-th of the next n lines contains two integers xi, yi (1≤xi,yi≤109) — the coordinates of the i-th point.
All points are distinct.
Output
Print a single integer — the number of different non-empty sets of points she can obtain.
sample input:
3
1 1
1 2
1 3
sample output:
3
题意:
平面上有n个点,你可以画一个左边界为l, 右边界为r, 下边界为a的无上界的区域(l, r, a 都可以为实数),把一些点框在里面,求有多少种不同的点集可以被框出来。(比如图片中可以框出三种点集:{p1} , {p2}, {p1, p2}
题解:
离散化,按y从大到小扫,如果处于y这条线的点只有一个,那么统计到目前为止,左边有多少间隔,右边有多少间隔,乘起来就是这个点的贡献。
如果处于y这条线的点有多个,先把这条线上所有点的x坐标对应间隔都加入当前的间隔中,然后再把这条线上的点从左到右扫一遍,一个点的贡献为:右边的间隔数 * (当前点左边的间隔数 - 前一个点左边的间隔数)。
题目链接
ac代码:
#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) (x & -x)
using namespace std;
const int maxn = 2e5 + 50;
ll cx[maxn], cy[maxn];
int nx, ny;
ll l[maxn], r[maxn];
int n;
ll a[maxn];
void add(int i, int x)
{
while(i < maxn){
a[i] += x;
i += lowbit(i);
}
}
ll query(int i)
{
ll ans = 0;
while(i){
ans += a[i];
i -= lowbit(i);
}return ans;
}
struct node{
ll x, y;
bool operator < (const node & a){
if(y != a.y) return y > a.y;
return x < a.x;
}
}e[maxn];
int main()
{
memset(l,0x3f,sizeof l);
memset(r,0,sizeof r);
nx = ny = 0;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%I64d%I64d",&e[i].x, &e[i].y);
cx[++nx] = e[i].x;
cy[++ny] = e[i].y;
}
cx[++nx] = 0x3f3f3f3f;
sort(cx+1, cx+1+nx);
nx = unique(cx+1, cx+1+nx) - cx - 1;
sort(cy+1, cy+1+ny);
ny = unique(cy+1, cy+1+ny) - cy - 1;
ll ans = 0;
sort(e+1, e+1+n);
add(nx, 1);
int pre = -1;
for(int i = 1; i <= n; ++i){
int posx = lower_bound(cx+1, cx+nx+1, e[i].x) - cx;
if(query(posx) - query(posx-1) == 0) add(posx, 1);
if(e[i].y != e[i+1].y){
if(pre == -1){
int posx = lower_bound(cx+1, cx+nx+1, e[i].x) - cx;
ll L = query(posx);
ll R = query(nx) - L;
ans += L*R;
}
else{
ll res = 0;
ll A = query(nx);
for(int j = pre; j <= i; ++j){
int posx = lower_bound(cx+1, cx+nx+1, e[j].x) - cx;
ll L = query(posx) - query(res);
ans += L*(A - query(posx));
res = posx;
}
pre = -1;
}
}
else if(pre == -1) pre = i;
}
cout<<ans<<endl;
}