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

题意:给你一组人,每个人有三个数值a,b,c,如果存在一个人x的三个值均大于另一个人y的三个值,y将自杀,求最终有多少个人自杀?

思路:一开始以为是三维偏序……幸好并非如此.

如果只有两维的话,自然好解决,将一维从大到小排序,二维扔进一颗线段树,每次比较线段树中的最大值与当前要扔进的值,因为先放入的一维必然大,若此时二维又比线段树中已有二维小,这个数必然产生贡献.

那么如何拓展到三维呢?

刚刚的线段树还没有发挥到极致,比如区间求最小值.

看看数据emmmm500000

当然是离散化喽!

将第一个值离散化作为线段树中的插入的位置

将第二维从大到小排序这样可以忽略第二维的影响

因为逐个插入线段树的第二维一定是递减的

然后线段树中插入值的位置为第一维离散后的值

存储的数为第三维的值

对于每个新插入的数只要保证他插入的位置后面有一个值比他大的就产生了贡献(因为后放入所以y已经小了,插入的位置后面的数说明x也小了,若z再小说明此人跳楼自杀)

然后就好啦!

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define lson root<<1
#define rson root<<1|1
#define maxn 500005 int t[maxn<<],h[maxn],m,n;
struct node
{
int x,y,z;
}tr[maxn]; bool cmp(node a,node b)
{
return a.y>b.y;
} void push_up(int root)
{
t[root]=max(t[lson],t[rson]);
} void build(int l,int r,int root)
{
t[root]=;
if(l==r)
{
return ;
}
int mid=(l+r)>>;
build(l,mid,lson);
build(mid+,r,rson);
} void update(int l,int r,int root,int p,int x)
{
if(l==r)
{
t[root]=max(t[root],x);
return;
}
int mid=(l+r)>>;
if(p<=mid)
{
update(l,mid,lson,p,x);
}
else
{
update(mid+,r,rson,p,x);
}
push_up(root);
} int query(int l,int r,int root,int li,int ri)
{
if(li<=l&&r<=ri)
{
return t[root];
}
int mid=(l+r)>>;
int val=;
if(li<=mid)
{
val=max(query(l,mid,lson,li,ri),val);
}
if(r>mid)
{
val=max(query(mid+,r,rson,li,ri),val);
}
return val;
} void init()
{
sort(h,h+m);
m=unique(h,h+m)-h;
build(,m,);
for(int i=;i<n;i++)
{
tr[i].x=lower_bound(h,h+m,tr[i].x)-h+;
}
sort(tr,tr+m,cmp);
} int main()
{
int ans,i,j,k;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&tr[i].x);
h[i]=tr[i].x;
}
for(int i=;i<n;i++)
{
scanf("%d",&tr[i].y);
}
for(int i=;i<n;i++)
{
scanf("%d",&tr[i].z);
}
m=n;
ans=;
init();
for(i=;i<n;i=j)
{
j=i;
while(j<n&&tr[j].y==tr[i].y)
{
if(query(,m,,tr[j].x+,m)>tr[j].z)
{
ans++;
}
j++;
}
for(k=i;k<j;k++)
{
update(,m,,tr[k].x,tr[k].z);
}
}
printf("%d\n",ans);
return ;
}

Codeforces 12D Ball(线段树)的更多相关文章

  1. Ball CodeForces - 12D (线段树)

    题目链接:https://cn.vjudge.net/problem/CodeForces-12D 题目大意:给你一个人的三个信息,如果存在一个人比当前人的这三个信息都大,那么这个人就会退出,问你最终 ...

  2. codeforces 12D Ball

    codeforces 12D Ball 这道题有两种做法 一种用树状数组/线段树维护区间最值,一种用map维护折线,昨天我刚遇见了一道类似的用STL维护折线的题目: 392D Three Arrays ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  5. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  6. Codeforces 787D. Legacy 线段树建模+最短路

    D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  7. hdu 1556 Color the ball (线段树+代码详解)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  9. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

随机推荐

  1. 程序4-4 chmod函数实例

    //http://blog.chinaunix.net/uid-24549279-id-71355.html /* ========================================== ...

  2. 【转】Jmeter MySQL数据库性能测试

    1.首先准备M一SQL数据,新建一个数据库及测试用的表,插入1条数据 2.打开Jmeter,新建线程组,设置多少用户,循环几次随意 3.在线程组下新增JDBC配置元件,通过配置使得Jmeter能够连上 ...

  3. 模块的分类以及time与date time 模块 radom模块

    1.标准库,或者内置模块,python解释器自带的,比如sys,os模块 2.开源模块,或者叫第三方模块,python就强大在这里. 3.自定义模块. 标准库: 1.时间模块time与datetime ...

  4. 20181101_将WCF寄宿到控制台

    使用管理员权限打开VS2017 2. 创建以下代码进行测试: a)         创建一个空白解决方案 b)         创建三个类库文件 c)         IMathService代码如下 ...

  5. 单片机keil C中的data、bdata、idata、xdata、hdata、pdata、code解释

    从数据存储类型来说,8051系列有片内.片外程序存储器,片内.片外数据存储器,片内程序存储器还分直接寻址区和间接寻址类型,分别对应code.data.xdata.idata以及根据51系列特点而设定的 ...

  6. Access 数据库的数据类型

    今天开发数据库通用组件时,做C#数据库类型与Access数据库类型的相互转化时,发现Access中“text”类型的最大长度为255,当长度超过255时,需改用“Memo”类型,但需要注意备注(Mem ...

  7. js 正则匹配 小结

    JS的正则表达式 rge.test(str) 检验目标对象中是否包含匹配模式,并相应的返回true或false   rge.source str.search(rge) 将返回一个整数值,指明这个匹配 ...

  8. 「小程序JAVA实战」 小程序默认加载的页面和生命周期(八)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-08/ 小程序如何加载的呢?生命周期!源码:https://github.com/limingios ...

  9. Eclipse修改XML默认打开方式

    用Eclipse开发Android的时候 默认的XML是采用Android xml editor 打开,这个工具不够直观,如果想直接看文本的XML的话,可以通过如下方式修改 1.菜单:Window   ...

  10. 20-从零玩转JavaWeb-Super关键字与子类初始化过程

    配套详解视频  super关键字 继承内存分析 this与super对比 继承字段隐藏 继承Object根类 一.Super关键字的作用 this:当前对象,谁调用this所在的方法,this就是哪一 ...