코드 트리(Codeforces 12D Ball)

4007 단어
D. Ball
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Sample test(s)
input
3
1 4 2
4 3 2
2 5 3

output
1

제목:
n개의 삼원조(B, L, R)를 정하고 몇 개의 삼원조(B[i], L[i], R[i]), 존재(B[j], L[j], R[j])를 구하여 B[i]분석:
1차원마다 이산화한 다음에 1차원에서 큰 것부터 작은 것, 2차원에서 작은 것, 3차원에서 큰 것부터 작은 것까지 순서대로 이 3원조를 정렬하고 한 그루의 라인 트리를 세우며 2차원 수치가 3차원의 최대 값에 대응하는 것을 유지한다. 매번 매거할 때 구간 [L[i]+1, n]의 최대 값 k를 조회하고 k>R[i]이면ans++를 만들고 라인 트리에 있는 L[i]의 값을 업데이트한다.전술한 방법에 따라 순서대로 열거할 때 1차원이 줄지 않고 1차원과 동시에 2차원으로 점차 늘어나는 것을 주의하고 먼저 라인 트리를 삽입하는 값이 현재 문의에 영향을 미치지 않도록 한다.복잡도 O (nlogn).
코드:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=500005;
int b[MAXN],l[MAXN],r[MAXN],t[MAXN];
void modify(int s[],int h[],int n)
{
    for(int i=1;i<=n;i++)h[i]=s[i];
    sort(h+1,h+n+1);
    int cnt=unique(h+1,h+n+1)-(h+1);
    for(int i=1;i<=n;i++)
        s[i]=lower_bound(h+1,h+cnt+1,s[i])-h;
}
struct person
{
    int b,l,r;
    person(int b=0,int l=0,int r=0):b(b),l(l),r(r){}
    bool operator < (const person &q)const
    {
        return b==q.b ? (l==q.l ? r>q.r : l<q.l) : b>q.b;
    }
}p[MAXN];
struct node
{
    int l,r,m,v;
}s[MAXN<<2];
void push_up(int num)
{
    s[num].v=max(s[num<<1].v,s[num<<1|1].v);
}
void build(int l,int r,int num)
{
    int m=(l+r)>>1;
    s[num].l=l;
    s[num].r=r;
    s[num].m=m;
    if(r-l==1)s[num].v=0;
    else
    {
        build(l,m,num<<1);
        build(m,r,num<<1|1);
        push_up(num);
    }
}
void update(int p,int k,int num)
{
    if(s[num].l==p && s[num].r==p+1)
    {
        s[num].v=max(s[num].v,k);
        return;
    }
    if(p<s[num].m)update(p,k,num<<1);
    else update(p,k,num<<1|1);
    push_up(num);
}
int query(int l,int r,int num)
{
    if(s[num].l==l && s[num].r==r)return s[num].v;
    if(r<=s[num].m)return query(l,r,num<<1);
    if(l>=s[num].m)return query(l,r,num<<1|1);
    return max(query(l,s[num].m,num<<1),query(s[num].m,r,num<<1|1));
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=1;i<=n;i++)scanf("%d",&l[i]);
    for(int i=1;i<=n;i++)scanf("%d",&r[i]);
    modify(b,t,n);
    modify(l,t,n);
    modify(r,t,n);
    for(int i=1;i<=n;i++)
    {
        p[i]=person(b[i],l[i],r[i]);
    }
    sort(p+1,p+n+1);
    build(1,n+2,1);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(query(p[i].l+1,n+2,1)>p[i].r)ans++;
        update(p[i].l,p[i].r,1);
    }
    printf("%d
",ans); return 0; }

좋은 웹페이지 즐겨찾기