D. Ball(树状数组三维排序,求是否存在三个值都比自己大的人)

题目链接:http://codeforces.com/problemset/problem/12/D

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
题意:给你N个女人的Beauty,Intelect,richness值,女人之间如果Bi<Bj&&Ii<Ij&&Ri<Rj,那么i女人就会去自杀.....0.0!问总共有多少个女会去自杀......
思路:思路在代码中
看代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define sc1(a) scanf("%lld",&a)
#define pf1(a) printf("%lld
",a)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const LL INF=1e18;
const ull base=2333;
const int maxn=5e5+50;
const int maxm=1e3+50;
const int maxv=1e6+5;
const int mod=1e9+7;
const int ba=3e5;
/**
首先根据x从小到大排序  给排序后的女人重新编号 这一维的大小我们就控制好了 id就是他们新的大小
接下来我们处理y 我们把y从大到小排序 遍历一遍y 此时y是从大往小来遍历的(这里保证了y小于后面的女人)
那么我们判断当前的会不会自杀 就是判断在当前的id后面的那些女人(id后面的女人保证了x小于这些女人)的z会不会也比当前的大(这里再控制z)
所以这样子就求出来了
*/
LL N,p;
LL c[maxn];
struct Node
{
    LL x,y,z,id;
}a[maxn];
bool cmp(const Node a1,const Node a2)
{
    return a1.x<a2.x;
}
bool cmp2(const Node a1,const Node a2)
{
    return a1.y>a2.y;
}
LL lowbit(LL x)
{
    return x&(-x);
}
LL Query(LL x)
{
    LL ma=-1;
    for(int i=x;i<=p;i+=lowbit(i))
    {
        ma=max(ma,c[i]);
    }
    return ma;
}
void Update(LL id,LL v)
{
    for(int i=id;i>0;i-=lowbit(i))
    {
        c[i]=max(c[i],v);
    }
}
int main()
{
    sc1(N);
    for(int i=1;i<=N;i++) sc1(a[i].x),c[i]=-1;
    for(int i=1;i<=N;i++) sc1(a[i].y);
    for(int i=1;i<=N;i++) sc1(a[i].z);
    sort(a+1,a+1+N,cmp);
    p=1;a[1].id=p;
    for(int i=2;i<=N;i++)
    {
        if(a[i].x==a[i-1].x) a[i].id=a[i-1].id;
        else a[i].id=++p;
    }
    sort(a+1,a+1+N,cmp2);
    LL ans=0;
    int i,j;
    for(i=1;i<=N;i=j)//保证y从大往小
    {
        for(j=i;j<=N&&a[j].y==a[i].y;j++)//这里y相等的要一起处理 不然的话就把下一个和当前的y相等的默认为大于下一个了
        {
            LL v=Query(a[j].id+1);//查找x比自己大的女人中是否存在y比自己大的
            if(v>a[j].z) ans++;//代表存在三个值都比自己大的
        }
        for(j=i;j<=N&&a[j].y==a[i].y;j++)
        {
            Update(a[j].id,a[j].z);
        }
    }
    pf1(ans);
    return 0;
}
/**

*/
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/12358149.html