Codeforces Beta Round #12 D. Ball (线段树)

题目大意:

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 (线段树)

时间: 2024-08-27 11:15:40

Codeforces Beta Round #12 D. Ball (线段树)的相关文章

codeforces Beta Round #19 D. Point (线段树 + set)

题目大意: 对平面上的点进行操作. add x y 在 (x,y )上加一个点. remove x y 移除 (x,y)上的点. find x y 求出在(x,y)右上角离他最近的点,优先级是靠左,靠下. 思路分析: find 操作 比较麻烦. 要保证x大的同时还要确保x最小,而且该x上还要有点. 这样要找大的时候要小的,就是在线段树上选择性的进入左子树还是右子树. 所以核心就是,用set维护叶子节点. 然后查找的时候去叶子节点找,如果这个叶子节点有蛮子的 x y  就输出,否则回溯去另外一个子

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 t

Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值

http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b[1], b[2], b[2]; 首先,先把b[1]值离散化,离散成一个个id,那么只能是在id比较大的地方找了.然后把b[2]排序,倒序查询,也就是先查询最大的,当然它是没可能自杀的,因为它已经最大了,然后查询完后,把它压进bit后,以后在bit查询,就不需要管b[2]了,因为在bit里面的b[2

Codeforces Beta Round #12 (Div 2 Only) D. Ball sort/map

D. Ball Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/12/D Description 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

Codeforces Beta Round #10 B. Cinema Cashier (树状数组)

题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 1000005 #define lowbit(x) (x&(-x)) using namespace std; struct BIT { int sum

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #6 (Div. 2 Only)

Codeforces Beta Round #6 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 typedef long long ll; 8 /*#ifndef

Codeforces Beta Round #35 (Div. 2)

Codeforces Beta Round #35 (Div. 2) http://codeforces.com/contest/35 A 这场的输入输出是到文件中的,不是标准的输入输出...没注意看,RE了好久... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x

Codeforces Beta Round #75 (Div. 2 Only)

Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A 1 #include<iostream> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 100005 7 typedef l