Codeforces 12 D Ball

Discription

N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of the i-th lady by Bi, her intellect by Ii and her richness by Ri. Then i-th lady is a probable self-murderer if there is some j-th lady that Bi < Bj, Ii < Ij, Ri < Rj. Find the number of probable self-murderers.


Input

The first line contains one integer N (1 ≤ N ≤ 500000). The second line contains N integer numbers Bi, separated by single spaces. The third and the fourth lines contain sequences Ii and Ri in the same format. It is guaranteed that 0 ≤ Bi, Ii, Ri ≤ 109.

Output

Output the answer to the problem.

Examples
Input
3
1 4 2
4 3 2
2 5 3
Output
1

先按B从大到小排序,消去一维。 然后我们依次尝试把点加入一个随着L单增R单减的二维图形上。显然这个图形可以用set维护。
当我们尝试加入一个点的时候,如果图形中第一个L比它大的点的R也比它大,那么这位女士显然就是要跳楼了;
否则我们就加入这个点,然后依次向图形的L小的一端扫,看能不能删去其他的点。

但是这么做有一个问题,就是如果B一样且L,R都满足小于的话,会被算进答案。
所以我们在之前排序的时候钦定B一样的时候按L升序排序,这样就可以避免这种情况。

#include<bits/stdc++.h>
#define ll long long
const int maxn=500005;
using namespace std;
struct node{
	int x,y,z;
	bool operator <(const node &u)const{
		return x==u.x?y<u.y:x>u.x;
	}
}a[maxn];
struct P{
	int x,y;
	bool operator <(const P &u)const{
		return x<u.x;
	}
};
set<P> s;
set<P> ::iterator it;
int n,ans=0;

inline void solve(){
	s.insert((P){-1,1e9+1});
	s.insert((P){1e9+1,-1});
	for(int i=1;i<=n;i++){
		P p=(P){a[i].y,a[i].z};
		it=s.upper_bound(p);
		
		if(it->y>p.y) ans++;
		else{
			--it;
			while(it->y<=p.y){
				s.erase(it);
				it=--s.upper_bound(p);
			}
			s.insert(p);
		}
	}
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i].x);
	for(int i=1;i<=n;i++) scanf("%d",&a[i].y);
	for(int i=1;i<=n;i++) scanf("%d",&a[i].z);
	sort(a+1,a+n+1);
	solve();
	printf("%d\n",ans);
	return 0;
}

 




posted @ 2018-04-11 16:17  蒟蒻JHY  阅读(323)  评论(0编辑  收藏  举报