cogs 2479.[HZOI 2016] 偏序(求四维偏序)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baodream/article/details/82778387

题目链接:http://cogs.pro:8080/cogs/problem/problem.php?pid=2479

【题目描述】

给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数。

【输入格式】

第一行一个整数n,表示序列长度。

第二行n个整数,分别表示a1~an。

第三行n个整数,分别表示b1~bn。

第四行n个整数,分别表示c1~cn。

【输出格式】

一个整数,表示答案。

【样例输入】

5
1 5 3 4 2
2 5 3 4 1
1 2 5 3 4

【样例输出】

3

【样例解释】

满足条件的(i,j)共有以下三对:

(1,2)

(1,3)

(1,4)

【数据范围与约定】

对于30%的数据,n<=5000。

对于100%的数据,1<=n<=50000,保证所有的ai、bi、ci分别组成三个1~n的排列。

【来源】

HZOI 2016

题目思路:四维偏序裸题,CDQ套CDQ即a通过flag标记,在CDQ2的时候,b已经是有序的了,所以只用再c归并,d树状数组求和。

推荐这篇博客,看一下代码秒懂:http://www.cnblogs.com/candy99/p/6442434.html

我的AC代码:

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

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;

struct node{
    int a,b,c,d;
    bool flag;
}a[N],tmp[N],tmp1[N];
int n;
ll ans;


int tree[N]; //tree数组按二进制存,根据n的末尾0的个数存取,树状数组

int lowbit(int x){return x&(-x);}
int Query(int x){  //返回1到x的前缀和
    int res=0;
    while(x){
        res+=tree[x];
        x-=lowbit(x);
    }
    return res;
}
void Add(int x,int v){  //实现a[x]+v;
    while(x<=n){        //注意这里是小于等于k,不是n,k是数据范围
        tree[x]+=v;
        x+=lowbit(x);
    }
}
void clearr(int x){
    while(x<=n){
        if(tree[x]==0)
            break;
        tree[x]=0;
        x+=lowbit(x);
    }
}

void CDQ2(int l,int r){
    if(l>=r) return;
    int mid = l+r>>1;
    CDQ2(l,mid);
    CDQ2(mid+1,r);
    int p=l,q=mid+1,k=l;
    //这里的tmp数组已经是对b有序的一个数组,flag用于判断下标,flag=1代表在(l,mid),flag=0代表在(mid+1,r)区间
    while(p<=mid&&q<=r){
        if(tmp[p].c<tmp[q].c){
            if(tmp[p].flag) Add(tmp[p].d,1);
            tmp1[k++]=tmp[p++];
        }
        else{
            if(!tmp[q].flag)
                ans+=Query(tmp[q].d);
            tmp1[k++] = tmp[q++];
        }
    }
    while(p<=mid){
        tmp1[k++] = tmp[p++];
    }
    while(q<=r){
        if(!tmp[q].flag)
            ans+=Query(tmp[q].d);
        tmp1[k++] = tmp[q++];
    }
    for(int i=l;i<=r;i++) clearr(tmp[i].d),tmp[i] = tmp1[i];
}

void CDQ(int l,int r){
    if(l>=r) return;
    int mid = l+r>>1;
    CDQ(l,mid);
    CDQ(mid+1,r);
    int p=l,q=mid+1,k=l;
    while(p<=mid&&q<=r){
        if(a[p].b<a[q].b){
            tmp[k++] = a[p++];
            tmp[k-1].flag=1;
        }
        else{
            tmp[k++] = a[q++];
            tmp[k-1].flag=0;
        }
    }
    while(p<=mid){
        tmp[k++] = a[p++];
        tmp[k-1].flag = 1;
    }
    while(q<=r){
        tmp[k++] = a[q++];
        tmp[k-1].flag=0;
    }
    for(int i=l;i<=r;i++) a[i] = tmp[i];
    CDQ2(l,r);
}

int main()
{
    freopen("partial_order.in","r",stdin);
    freopen("partial_order.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i].b);
    for(int i=1;i<=n;i++) scanf("%d",&a[i].c);
    for(int i=1;i<=n;i++) scanf("%d",&a[i].d), a[i].a=i;
    ans=0;
    CDQ(1,n);
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/baodream/article/details/82778387