[CF1190D] Tokitsukaze and Strange Rectangle

题目描述

There are $ n $ points on the plane, the $ i $ -th of which is at $ (x_i, y_i) $ . 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.

imageA point $ (x_i, y_i) $ is in the strange rectangular area if and only if $ l < x_i < r $ and $ y_i > a $ . For example, in the above figure, $ p_1 $ is in the area while $ p_2 $ 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.

输入格式

The first line contains a single integer $ n $ ( $ 1 \leq n \leq 2 \times 10^5 $ ) — the number of points on the plane.

The $ i $ -th of the next $ n $ lines contains two integers $ x_i $ , $ y_i $ ( $ 1 \leq x_i, y_i \leq 10^9 $ ) — the coordinates of the $ i $ -th point.

All points are distinct.

输出格式

Print a single integer — the number of different non-empty sets of points she can obtain.

样例 #1

样例输入 #1

3
1 1
1 2
1 3

样例输出 #1

3

样例 #2

样例输入 #2

3
1 1
2 1
3 1

样例输出 #2

6

样例 #3

样例输入 #3

4
2 1
2 2
3 1
3 2

样例输出 #3

6

提示

For the first example, there is exactly one set having $ k $ points for $ k = 1, 2, 3 $ , so the total number is $ 3 $ .

For the second example, the numbers of sets having $ k $ points for $ k = 1, 2, 3 $ are $ 3 $ , $ 2 $ , $ 1 $ respectively, and their sum is $ 6 $ .

For the third example, as the following figure shows, there are

  • $ 2 $ sets having one point;
  • $ 3 $ sets having two points;
  • $ 1 $ set having four points.

Therefore, the number of different non-empty sets in this example is $ 2 + 3 + 0 + 1 = 6 $ .

image

考虑按照 \(a\) 进行一个扫描。

为了保证数到的集合不重复,钦定枚举到的 \(a\) 是点击中 \(y\) 的最小值。同时把所有点进行离散化,明显是不影响答案的。把点按照 \(y\) 从大到小加入。

然后可以看有多少个符合要求的 \(l,r\)。这个其实很难算,但是可以考虑算出不符合要求的,在容斥一下。设 \(x_1,x_2\cdots x_k\) 为这次新加入的点的 \(x\) 坐标从小到大排序。如果 \((x_1,x_2)\) 中有 \(p\) 个不同的之前加入的 点的 \(x\) 坐标,那么这 \(p\) 个点形成的区间的 \(y\) 最小值都不是现在枚举的最小值的。注意 \(p\) 个点总共可以产生 \(\frac 12p(p+1)\) 个区间。

用树状数组维护前缀和,然后用总共的减去不合法的就行了。注意不合法的区间还有 \((0,x_1)\)\((x_n,\infin)\)

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
typedef long long LL;
LL ans,tr[N];
int lsh[N],n,mn,mx,k,cnt,f[N],tk;
struct node{
	int x,y;
	bool operator<(const node&n)const{
		if(y^n.y)
			return y>n.y; 
		return x<n.x;
	}
}p[N];
int cmp(node x,node y)
{
	return x.y>y.y;
}
void update(int x,int y)
{
	for(;x<=tk;x+=x&-x)
		tr[x]+=y;
}
LL query(int x)
{
	LL ret=0;
	for(;x;x-=x&-x)
		ret+=tr[x];
	return ret;
}
LL cl(int x)
{
	return x*(x+1LL)>>1;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y),lsh[i]=p[i].y;
	sort(lsh+1,lsh+n+1);
	k=unique(lsh+1,lsh+n+1)-lsh-1;
	for(int i=1;i<=n;i++)
		p[i].y=lower_bound(lsh+1,lsh+k+1,p[i].y)-lsh;
	for(int i=1;i<=n;i++)
		lsh[i]=p[i].x;
	sort(lsh+1,lsh+n+1);
	tk=unique(lsh+1,lsh+n+1)-lsh-1;
	for(int i=1;i<=n;i++)
		p[i].x=lower_bound(lsh+1,lsh+tk+1,p[i].x)-lsh;
	sort(p+1,p+n+1);
	int now=0;
//	for(int i=1;i<=n;i++)
//		printf("%d %d\n",p[i].x,p[i].y);
	for(int i=k;i>=1;i--)
	{
//		printf("crxzdq:%d\n",i); 
		int mn=200000000,mx=0;
		int t=now,lst=0;
		while(now^n&&p[now+1].y==i)
		{
//			printf("abc:%d\n",p[now].x);
			++now;
			ans-=cl(query(p[now].x-1)-query(lst));
			if(!f[p[now].x])
				update(p[now].x,f[p[now].x]=1);
			lst=p[now].x;
		}
		ans-=cl(query(tk)-query(lst));
		ans+=cl(query(tk));
//		printf("%lld\n",query(tk));
	}
	printf("%lld",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值