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.
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 the answer to the problem.
3 1 4 2 4 3 2 2 5 3
1
解题思路:给定一些人,他们每个人用一个三元组(x,y,z)表示,若x1>x2&&y1>y2&&z1>z2,则1号完全优于2号,2号会去自杀,问总共有多少人自杀。首先对y进行离散化,然后按x从大到小排序,把他们的z值插入到线段树中y的位置,这样保证之前的人的x值都是大于当前的人的,即求y大于当前的人中有无z大于当前的人的,若有,当前人自杀。注意x可能会相同,所以对x相同的只询问不更新,等到所有询问结束后再将x相同的一并更新
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <bitset>
#include <cmath>
#include <utility>
#define Maxn 500005
#define Maxm 1000005
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define make_pair MP
#define LL long long
#define Inf (1LL<<62)
#define inf 0x3f3f3f3f
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
int tree[Maxn<<2];
struct P
{
int x,y,z;
int id;
}p[Maxn];
bool cmp1(P a,P b)
{
if(a.x!=b.x)
return a.x>b.x;
else if(a.y!=b.y)
return a.y>b.y;
else
return a.z>b.z;
}
bool cmp2(P a,P b)
{
return a.y<b.y;
}
void push_up(int rt)
{
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
void update(int pos,int x,int l,int r,int rt)
{
if(l==r)
{
tree[rt]=max(tree[rt],x);
return ;
}
int m=(l+r)>>1;
if(pos<=m)
update(pos,x,lson);
else
update(pos,x,rson);
push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
return tree[rt];
int m=(l+r)>>1;
int ans=0;
if(L<=m)
ans=max(ans,query(L,R,lson));
if(R>m)
ans=max(ans,query(L,R,rson));
return ans;
}
int main()
{
int i,j,n,ans,cnt,pre;
//re;wr;
while(~scanf("%d",&n))
{
ans=0;cnt=1;
memset(tree,0,sizeof(tree));
for(i=0;i<n;i++)
scanf("%d",&p[i].x),p[i].id=i;
for(i=0;i<n;i++)
scanf("%d",&p[i].y);
for(i=0;i<n;i++)
scanf("%d",&p[i].z);
sort(p,p+n,cmp2);
pre=p[0].y;
p[0].y=1;
for(i=1;i<n;i++)
if(p[i].y==pre)
{
pre=p[i].y;
p[i].y=cnt;
}
else
{
pre=p[i].y;
p[i].y=++cnt;
}
sort(p,p+n,cmp1);
for(i=0;i<n;)
{
for(j=i;j<n&&p[i].x==p[j].x;j++)
{
if(query(p[j].y+1,cnt,1,cnt,1)>p[j].z)
ans++;
}
for(;i<j;i++)
update(p[i].y,p[i].z,1,cnt,1);
}
printf("%d\n",ans);
}
return 0;
}