n个女性中,如果有一个女性的三维比这个女性的三维都大,这个女性就要自杀。问要自杀多少个。
思路分析:
先按照第一维排序。
然后离散化第二维,用第二维的下标建树,树上的值是第三维,更新的时候取最大值。
注意是按照第一维度从大到小进入线段树。
而且还要严格递增。
所以处理第一维度比较大小的时候要分开处理,要把相同的先判断,再更新入树。
那么如何判断这个女性是否自杀。
首先,你知道第一维度是从大到小的,所以先进树了的节点的第一维度一定更大。
再次,我们考虑第二维度,我们去树上第二维度下标大的节点区去寻找第三维度。
如果有一个比这个的第三维度大,那么就满足。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define lson num<<1,s,mid
#define rson num<<1|1,mid+1,e
#define maxn 500005
using namespace std;
struct node
{
int a,b,c;
bool operator < (const node &cmp)const
{
if(a!=cmp.a)return a<cmp.a;
if(b!=cmp.b)return b<cmp.b;
return c<cmp.c;
}
}wm[maxn];
int res[maxn<<2];
int x[maxn];
void pushup(int num)
{
res[num]=max(res[num<<1],res[num<<1|1]);
}
void build(int num,int s,int e)
{
res[num]=-1;
if(s==e)return ;
int mid=(s+e)>>1;
build(lson);build(rson);
}
void update(int num,int s,int e,int pos,int val)
{
if(s==e)
{
res[num]=max(res[num],val);
return;
}
int mid=(s+e)>>1;
if(pos<=mid)update(lson,pos,val);
else update(rson,pos,val);
pushup(num);
}
int query(int num,int s,int e,int l,int r)
{
if(l<=s && r>=e)return res[num];
int mid=(s+e)>>1;
if(r<=mid)return query(lson,l,r);
else if(l>mid)return query(rson,l,r);
else return max(query(lson,l,mid),query(rson,mid+1,r));
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&wm[i].a);
for(int i=1;i<=n;i++)
{
scanf("%d",&wm[i].b);
x[i]=wm[i].b;
}
for(int i=1;i<=n;i++)
scanf("%d",&wm[i].c);
sort(wm+1,wm+1+n);
sort(x+1,x+1+n);
int m=unique(x+1,x+1+n)-x;
m--;
int last=wm[n].a;
int r=n;
int l=n;
int ans=0;
wm[0].a=0x3f3f3f3f;
for(int i=n;i>=1;)
{
while(wm[l].a==last)
{
l--;
}
int c=r;
while(c>l)
{
int pos=lower_bound(x+1,x+m+1,wm[c].b)-x;
if(pos+1<=m && query(1,1,m,pos+1,m)>wm[c].c)ans++;
c--;
}
c=r;
while(c>l)
{
int pos=lower_bound(x+1,x+m+1,wm[c].b)-x;
update(1,1,m,pos,wm[c].c);
c--;
}
i=l;r=l;last=wm[i].a;
}
printf("%d\n",ans);
return 0;
}
Codeforces Beta Round #12 D. Ball (线段树),布布扣,bubuko.com
Codeforces Beta Round #12 D. Ball (线段树)
原文地址:http://blog.csdn.net/u010709592/article/details/38052823