Codeforces Round #573 (Div. 2) F. Tokitsukaze and Strange Rectangle

F. Tokitsukaze and Strange Rectangle

time limit per test:3 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

There are n n n points on the plane, the i i i-th of which is at ( x x xi, y y 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 x x= l l l, y y y= a a a and x x x= r r r, as its left side, its bottom side and its right side respectively, where l l l, r r r and a a a can be any real numbers satisfying that l &lt; r l&lt;r l<r. The upper side of the area is boundless, which you can regard as a line parallel to the x x x-axis at infinity. The following figure shows a strange rectangular area.
在这里插入图片描述
A point ( x (x (xi, y y yi ) ) ) is in the strange rectangular area if and only if l &lt; x l&lt;x l<xi &lt; r &lt;r <r and y y yi &gt; a &gt;a >a. For example, in the above figure, p p p1 is in the area while p p 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 n n(1≤ n n n≤2×105) — the number of points on the plane.
The i i i-th of the next n n n lines contains two integers x x xi, y y yi (1≤ x x xi, y y yi≤109) — the coordinates of the i i i-th point.
All points are distinct.
Output
Print a single integer — the number of different non-empty sets of points she can obtain.
题意
二维平面上有 n n n个点,第i个点的坐标以 ( x (x xi y y yi ) ) 的形式给出,保证每个点的坐标都不同且均在区间 [ 1 , 10 [1,10 [1,109 ] ] ]内。画一个由 x = l , x = r , y = a x=l,x=r,y=a x=l,x=r,y=a,上不封顶的矩形(其中至少包含一个点),求能够画出的包含的点的集合不同的矩形个数(若至少有一个点存在于集合A而不存在于集合B则认为集合A与集合B不同)
思路
显然,由于这个矩形上不封顶的性质,若有两个点A,B的x坐标相同,那么是无法做到画出的矩形中包含y坐标较小的点且不包含y坐标较大的点的。故先将点按x坐标排序,用map离散化得出x轴坐标不同的点的个数(设其为 c n t cnt cnt),再将点按y坐标排序,y坐标相同时按x坐标排序。每次处理当前所有y坐标最小的点。
对于仅有一个点的情况,设该点左边有 l l l个点,右边有 r r r个点,故对于左边r个点,可以选取 0 0 0~ r r r ( r + 1 ) (r+1) r+1种方案,同理,右边有 ( l + 1 ) (l+1) l+1种方案,共 ( l + 1 ) ∗ ( r + 1 ) (l+1)*(r+1) l+1r+1种方案。
对于多个点的情况,处理第二个点时就不能直接计算为 ( l + 1 ) ∗ ( r + 1 ) (l+1)*(r+1) l+1r+1了:假设第一个点前有 a a a x x x坐标不同的点,第二个点与第一个点间有 b b b x x x坐标不同的点,第二个点后有 c c c x x x坐标不同的点,若此时选取前 a a a个点之一作为起点,后 c c c个点之一作为终点,那么就与处理第一个点时的结果重复了(因为处理第一个点时也能分别以这两个点为起点和终点)。
故令 m [ k ] m[k] m[k]表示 x = k x=k x=k是第 m [ k ] m[k] m[k]小的、至少包含一个点满足 x = k x=k x=k x x x坐标。令 s u m sum sum_ t ( i ) t(i) t(i)表示有多少个满足 x &lt; = i x&lt;=i x<=i,且至少包含一个未被处理/正在被处理的点的 x x x坐标。则处理任意一个点 m i m_i mi时我们可以用 ( s u m (sum (sum_ t ( m i ) t(m_i) t(mi)- s u m sum sum_ t ( m t(m t(mi-1 ) ) ∗ ( ))*( ))( s u m sum sum_ t ( c n t ) t(cnt) t(cnt)- s u m sum sum_ t ( m i ) ) t(m_i)) t(mi))来表示该点且不选取任何一个与该点 y y y坐标相同且x坐标小于该点的点的情况下能得到的矩形数。
将这些点处理完后,我们需要从 s u m sum sum_ t ( i ) t(i) t(i)中删去这些点(因为每次被处理的点都是当前剩余点中 y y y坐标最小的所有点)。
重复以上步骤直到没有剩余的点。
AC代码

#include<stdio.h>
#include<map>
#include<queue>
#include<iostream>
#include<algorithm>
#include<vector>
#define MOD 10000000007
#define N 200005
using namespace std;
map<int,int> m;
typedef struct node{
	int x,y;
}node;
node data[N];
int ct1[N],ct2[N];//ct1用于记录每个x坐标剩余的点的数量,ct2用于记录每个x坐标是否存在点 
int q[N];
int n,x,y;
long long temp,cot,ans,cnt;
inline int lowbit(int x){return (x&(-x));}//树状数组
inline long long sum_t(int n)
{
	long long result=0;
	while(n)
	{
		result+=ct2[n];n-=lowbit(n);
	}
	return result;
}//树状数组
inline void update(int pos,int val)
{
	do
	{
		ct2[pos]+=val;
		pos+=lowbit(pos);
	}while (pos<=n);
	return;
}//树状数组 
int cmp1(node a,node b)
{
	return a.x<b.x;
}
int cmp2(node a,node b)
{
	return a.y<b.y||a.y==b.y&&a.x<b.x;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&x,&y);
		data[i].x=x;data[i].y=y;
	}
	sort(data+1,data+n+1,cmp1);//按x坐标排序 
	for(int i=1;i<=n;i++)
	{
		if(m.find(data[i].x)==m.end()) 
		{
			m[data[i].x]=++cnt;//使用map实现离散化 
		}
		ct1[m[data[i].x]]++;
	}
	for(int i=1;i<=cnt;i++) update(i,1);
	sort(data+1,data+n+1,cmp2);//按y坐标优先,x坐标其次排序 
	int i=0,j=1;
	while(i<n&&j<=n)
	{
		i=j;int te=1;
		for(j=i;data[j].y==data[i].y;j++,te++)
		{
			q[te]=data[j].x;//寻找当前所有可行点 
		}
		for(int k=1;k<te;k++)
		ans+=(sum_t(m[q[k]])-sum_t(m[q[k-1]]))*(sum_t(cnt)-sum_t(m[q[k]])+1);//计算每个点的可行方案数 
		for(int k=1;k<te;k++)
		{
			ct1[m[q[k]]]--;//删去这个点 
			if(ct1[m[q[k]]]==0) update(m[q[k]],-1);//删去不存在点的x坐标 
		}
	}
	printf("%I64d",ans);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值