Newcoder 16 A.Laptop(逆序对-BIT)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/V5ZSQ/article/details/83591667

Description

F S T FST 是一名可怜的小朋友,他很强,但是经常 f s t fst ,所以 r a t i n g rating 一直低迷。

但是重点在于,他非常适合 A C M ACM !并在最近的区域赛中获得了不错的成绩。

拿到奖金后 F S T FST 决定买一台新笔记本,但是 F S T FST 发现,在价格能承受的范围内,笔记本的内存和速度是不可兼得的。

可是,有一些笔记本是被另外一些“完虐”的,也就是内存和速度都不高于另外某一个笔记本,现在 F S T FST 想统计一下有多少笔记本被“完虐”。

Input

第一行一个正整数 n n ,表示笔记本的数量。接下来 n n 行,每行两个正整数 M i S i M_i,S_i 表示这款笔记本的内存和速度。

( n 1 0 5 , M i , S i 1 0 9 ) (n\le 10^5,M_i,S_i\le 10^9)

Output

一行,一个正整数,表示被完虐的笔记本数。

Sample Input

4
100 700
200 500
50 100
300 400

Sample Output

1

Solution

正序对,把所有笔记本按第一维降序排序后,以此将其第二维插入树状数组中,对于第 i i 个笔记本,之前考虑的 i 1 i-1 个笔记本第一维比它第一维要大,如果次数树状数组中它第二维所在位置之前没有 i 1 i-1 个位置有值,说明之前至少有一个笔记本第二维也比它大,那么这个笔记本就被完虐,以此统计答案即可,时间复杂度 O ( n l o g n ) O(nlogn)

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int,int>P;
const int maxn=100005;
struct BIT 
{
	#define lowbit(x) (x&(-x))
	int b[maxn],n;
	void init(int _n)
	{
		n=_n;
		for(int i=1;i<=n;i++)b[i]=0;
	}
	void update(int x,int v)
	{
		while(x<=n)
		{
			b[x]+=v;
			x+=lowbit(x);
		}
	}
	int query(int x)
	{
		int ans=0;
		while(x)
		{
			ans+=b[x];
			x-=lowbit(x);
		}
		return ans;
	}
}bit;
P a[maxn];
int n,h[maxn],vis[maxn]; 
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].first,&a[i].second);
		a[i].first*=-1;
		h[i]=a[i].second;
	}
	sort(a+1,a+n+1);
	sort(h+1,h+n+1);
	bit.init(n);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int t=lower_bound(h+1,h+n+1,a[i].second)-h;
		if(bit.query(t)!=i-1)ans++;
		bit.update(t,1);
	} 
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/V5ZSQ/article/details/83591667