Dimensional partial ordering

Meaning of the questions:

Given \ (n-\) elements, each element has four attributes \ (A, B, C, D \) , seeking a sequence that satisfies \ (a_i <a_j \) and \ (b_i <b_j \) and \ (c_i <c_j \) and \ (d_i <d_j \) number of \ ((i, j) \ ) number.

For \ (100% \) data, \ (. 1 <= n-<= 50000 \) , to ensure that all \ (ai, bi, ci, di \) are grouped into four \ (. 1 \) ~ \ (n-\ ) arrangement.

Ideas:

The first dimension sort.
Partition cdq second dimension, the second dimension while maintaining ordered, the order of recording a first dimension (i.e., whether this element can be updated or whether other elements may be updated)
in the third dimension continue cdq partition, a third dimension has maintained order of the
fourth dimension in the above premise, with Fenwick tree maintenance statistics answer

Precautions:

Beware hand disabled

code:

#include<bits/stdc++.h>
using namespace std;
const int N=50005;
int n,ans;
struct node{int a,b,c,d,tp;}q[N],tmp1[N],tmp2[N];
inline bool cmp1(node x,node y){return x.a<y.a;}
inline int read()
{
    int s=0,w=1; char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-')w=-1;
    for(;isdigit(ch);ch=getchar())s=(s<<1)+(s<<3)+(ch^48);
    return s*w;
}
struct tree{
    int cc[N];
    inline int lowbit(int x){return x&(-x);}
    inline void add(int x,int v)
    {
        for(;x<=n;x+=lowbit(x))cc[x]+=v;
    }
    inline int query(int x)
    {
        int anss=0;
        for(;x;x-=lowbit(x))anss+=cc[x];
        return anss;
    }
}T;
void cdq3d(int l,int r)
{
    if(l==r) return;
    int mid=l+r>>1;
    cdq3d(l,mid);cdq3d(mid+1,r);
    int i=l,j=mid+1,cnt2=l;
    while(j<=r)
    {
        while(tmp1[i].c<tmp1[j].c&&i<=mid)
        {
            if(tmp1[i].tp==1) T.add(tmp1[i].d,1);
            tmp2[cnt2++]=tmp1[i++];
        }
        if(tmp1[j].tp==2) ans+=T.query(tmp1[j].d);
        tmp2[cnt2++]=tmp1[j++];
    }
    for(int e=l;e<i;++e)
        if(tmp1[e].tp==1) T.add(tmp1[e].d,-1);
    for(int e=i;e<=mid;++e) tmp2[cnt2++]=tmp1[e];
    for(int e=l;e<=r;++e) tmp1[e]=tmp2[e];
}
void cdq2d(int l,int r)
{
    if(l==r) return;
    int mid=l+r>>1;
    cdq2d(l,mid);cdq2d(mid+1,r);
    int i=l,j=mid+1,cnt1=l;
    while(j<=r)
    {
        while(q[i].b<q[j].b&&i<=mid) q[i].tp=1,tmp1[cnt1++]=q[i++];
        q[j].tp=2,tmp1[cnt1++]=q[j++];
    }
    for(int e=i;e<=mid;++e) q[e].tp=1,tmp1[cnt1++]=q[e];
    for(int e=l;e<=r;++e) q[e]=tmp1[e];
    cdq3d(l,r);
}
int main()
{
    n=read();
    for(int i=1;i<=n;++i)q[i].a=read();
    for(int i=1;i<=n;++i)q[i].b=read();
    for(int i=1;i<=n;++i)q[i].c=read();
    for(int i=1;i<=n;++i)q[i].d=read();
    sort(q+1,q+n+1,cmp1);
    cdq2d(1,n);
    cout<<ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zmyzmy/p/12041304.html