首页 > 代码库 > CodeForces 12D Ball 多级排序 + 离散 + 线段树

CodeForces 12D Ball 多级排序 + 离散 + 线段树

给出B,I,R,对于Pi,若存在Pj满足 Bi < Bj && Ii < Ij && Ri < Rj ,则称Pi为 probable self-murderers。问存在多少个Pi。

首先对其升序排序,优先级为B > I > R。

然后发现对于i < j ,必有Bi <= Bj,所以这一层基本可以忽略。

然后由于数据范围较大,对 I 进行离散,建立线段树记录大于I的区间内最大的R是多少,当然此时要从后往前扫描,边更新边计数。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <ctime>
#include <iomanip>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-6)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 1000000007

using namespace std;

struct N
{
    int b,s,r;
} sta[500010];

bool cmp(N n1,N n2)
{
    if(n1.b != n2.b)
        return n1.b < n2.b;
    if(n1.s != n2.s)
        return n1.s < n2.s;
    return n1.r < n2.r;
}

int num[500010];

int DelSame(int *num,int Top)
{
    int i,j;

    for(i = 2,j = 1; i <= Top; ++i)
        if(num[i] != num[j])
            num[++j] = num[i];
    return j;
}

int BS(int *num,int l,int r,int x)
{
    int mid;

    while(l <= r)
    {
        mid = (l+r)>>1;

        if(num[mid] == x)
            return mid;
        if(num[mid] < x)
            l = mid+1;
        else
            r = mid-1;
    }
    return -1;
}

int st[2000010];

int Updata(int site,int l,int r,int x,int d)
{
    if(l == r && r == x)
        return st[site] = max(st[site],d);

    int mid = (l+r)>>1;

    if(x <= mid)
        return st[site] = max(st[site],Updata(site<<1,l,mid,x,d));
    return st[site] = max(st[site],Updata(site<<1|1,mid+1,r,x,d));
}

int Query(int site,int L,int R,int l,int r)
{
    if(L == l && R == r)
        return st[site];
    int mid = (L+R)>>1;

    if(r <= mid)
        return Query(site<<1,L,mid,l,r);
    if(mid < l)
        return Query(site<<1|1,mid+1,R,l,r);
    return max(  Query(site<<1,L,mid,l,mid), Query(site<<1|1,mid+1,R,mid+1,r) );
}

int main()
{
    int n,i,j;

    scanf("%d",&n);

    for(i = 0; i < n; ++i)
        scanf("%d",&sta[i].b);
    for(i = 0; i < n; ++i)
        scanf("%d",&sta[i].r);
    for(i = 0; i < n; ++i)
        scanf("%d",&sta[i].s);

    sort(sta,sta+n,cmp);

    int Top = 0;

    for(i = 0; i < n; ++i)
        num[++Top] = sta[i].r;

    sort(num+1,num+Top+1);

    Top = DelSame(num,Top);

//    for(i = 1;i <= Top; ++i)
//        printf("%d %d\n",i,num[i]);

//    for(i = 0; i < n; ++i)
//        printf("i = %d %d %d %d\n",i,sta[i].b,sta[i].r,sta[i].s);

    memset(st,0,sizeof(st));

    int site,ans = 0,tmp;

    int R = n-1,val = sta[n-1].b;

    for(i = n-1; i >= 0; --i)
    {
        if(sta[i].b == val)
            continue;

        for(j = i+1; j <= R; ++j)
        {
            site = BS(num,1,Top,sta[j].r);
            if(site != Top && (tmp = Query(1,1,Top,site+1,Top)) > sta[j].s)
               ans++;
        }
        for(j = i+1; j <= R; ++j)
        {
            site = BS(num,1,Top,sta[j].r);
            Updata(1,1,Top,site,sta[j].s);
        }
        R = i;
        val = sta[i].b;
    }
    for(j = i+1; j <= R; ++j)
    {
        site = BS(num,1,Top,sta[j].r);
        if(site != Top && (tmp = Query(1,1,Top,site+1,Top)) > sta[j].s)
            ans++;
    }

    printf("%d\n",ans);

    return 0;
}

CodeForces 12D Ball 多级排序 + 离散 + 线段树