记录编号 357270 评测结果 AAAAAAAAAA
题目名称 [HZOI 2016] 偏序 最终得分 100
用户昵称 Gravatarsxysxy 是否通过 通过
代码语言 C++ 运行时间 7.214 s
提交时间 2016-12-10 07:33:09 内存使用 2.27 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <string>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define MAXN 50002
namespace IO
{
    char buf[1<<18], *fs, *ft;
    inline char readc()
    {
        return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin), fs==ft))?0:*fs++;
    }
    inline int fast_read()
    {
        char c = readc();
        int r = 0;
        bool sig = false;
        while(c > '9' || c < '0')
        {
            if(c == '-')sig = true;
            c = readc();
        }
        while(c >= '0' && c <= '9')
        {
            r = (r<<3)+(r<<1)+(c^0x30);
            c = readc();
        }
        return sig?-r:r;
    }
}using IO::fast_read;

typedef struct _AVLNode
{
    struct _AVLNode *left, *right;
    int data;   
    int height;   
    int size;     
}AVLNode, *pNode, *AVLTree;
#define HEIGHT(x) ((x)?(x)->height:-1)
#define SIZE(x) ((x)?(x)->size:0)
void pushup(pNode k)
{       
    if(!k)return;
    k -> height = std::max(HEIGHT(k->left), HEIGHT(k->right))+1;
    k -> size = 1 + SIZE(k -> left) + SIZE(k -> right);
}
pNode singleRotateLL(pNode k)
{
    pNode k1 = k -> left;
    k -> left = k1 -> right;
    k1 -> right = k;
    pushup(k);
    pushup(k1);
    return k1;
}
pNode singleRotateRR(pNode k)
{
    pNode k1 = k -> right;
    k -> right = k1 -> left;
    k1 -> left = k;
    pushup(k);
    pushup(k1);
    return k1;
}
pNode doubleRotateLR(pNode k)
{
    k -> left = singleRotateRR(k -> left);
    return singleRotateLL(k);
}
pNode doubleRotateRL(pNode k)
{
    k -> right = singleRotateLL(k -> right);
    return singleRotateRR(k);
}
pNode insert(AVLTree t, int x)
{ 
    if(!t)     
    {
        t = new AVLNode;
        t -> data = x;
        t -> size = 1;
        t -> left = t -> right = NULL;
    }else
    {
        if(x < t -> data)  
        {
            t -> left = insert(t -> left, x);
            if(HEIGHT(t -> left) - HEIGHT(t -> right) == 2)
            {
                if(x < t -> left -> data)
                    t = singleRotateLL(t); 
                else    
                    t = doubleRotateLR(t); 
            }
        }else
        {
            t -> right = insert(t -> right, x);
            if(HEIGHT(t -> right) - HEIGHT(t -> left) == 2)
            {
                if(x >= t -> right -> data)
                    t = singleRotateRR(t);
                else
                    t = doubleRotateRL(t);
            }
        }
    }
    pushup(t);
    return t;
}
pNode find(AVLTree t, int x)
{
    if(!t)
        return NULL;
    else if(t -> data <= x)
        return find(t -> right, x);
    else if(x < t -> data)
        return find(t -> left, x);
    else
        return t;
}
int rank(pNode x, int v)
{
    if(!x)return 0;
    if(v < x->data)
        return rank(x->left, v);
    else
        return rank(x->right, v)+SIZE(x->left)+1;
}
pNode delBalance(AVLTree t)
{
    if(HEIGHT(t -> left) - HEIGHT(t -> right) == 2)
    {
        if(t -> left)
        {
            if(HEIGHT(t -> left -> left) >= HEIGHT(t -> left -> right))
                t = singleRotateLL(t);
            else
                t = doubleRotateLR(t);
        }
    }
    if(HEIGHT(t -> right) - HEIGHT(t -> left) == 2)
    {
        if(t -> right)
        {   
            if(HEIGHT(t -> right -> right) >= HEIGHT(t -> right -> left))
                t = singleRotateRR(t);
            else
                t = doubleRotateRL(t);
        }
    }
    pushup(t);
    return t;
}
pNode delNode(AVLTree t, int x)
{
    if(!t)
        return t;
    if(x == t -> data)   
    {
        if(t -> right == NULL)
        {
            pNode tmp = t;
            t = t -> left;
            free(tmp);
        }else
        {
            pNode tmp = t -> right;
            while(tmp -> left)
                tmp = tmp -> left;
            t -> data = tmp -> data;
            t -> right = delNode(t -> right, t -> data);
        }
        pushup(t);
        return t;
    }else if(x < t -> data)
    {
        t -> left = delNode(t -> left, x);    
    }else
    {
        t -> right = delNode(t -> right, x);
    }
    if(t -> left)
        t -> left = delBalance(t -> left);
    if(t -> right)
        t -> right = delBalance(t -> right);
    if(t)
        t = delBalance(t);
    return t;
}
struct node
{
    int id, a, b, c;
    bool operator<(const node &x)const{return a < x.a;}
}a[MAXN], tmp[MAXN];
using namespace std;
typedef long long LL;
int n;
LL ans;
int root;
AVLTree ts[MAXN];
void add(int x, int v)
{
    for(; x <= n; x += x&-x)
        ts[x] = insert(ts[x], v);
}
void del(int x, int v)
{
    for(; x <= n; x += x&-x)
        ts[x] = delNode(ts[x], v);
}
int query(int x, int v)
{
    int r = 0;
    for(; x; x -= x&-x)
        r += rank(ts[x], v);
    return r;
}
void merge(int l, int r)
{
    if(l >= r)return;
    int m = (l+r)>>1;
    merge(l, m);
    merge(m+1, r);
    int j = l, k = m+1, p = l;
    while(j <= m && k <= r)
        if(a[j] < a[k])tmp[p++] = a[j++];
        else tmp[p++] = a[k++];
    while(j <= m)tmp[p++] = a[j++];
    while(k <= r)tmp[p++] = a[k++];
    for(int i = l; i <= r; i++)
    {
        a[i] = tmp[i];
        if(a[i].id <= m)add(a[i].b, a[i].c);
        else ans += query(a[i].b, a[i].c);
    }
    for(int i = l; i <= r; i++)
        if(a[i].id <= m)del(a[i].b, a[i].c);
}
int main()
{   
    freopen("partial_order.in", "r", stdin);
    freopen("partial_order.out", "w", stdout);
    n = fast_read();
    for(int i = 1; i <= n; i++)a[i].id = i;
    for(int i = 1; i <= n; i++)a[i].a = fast_read();
    for(int i = 1; i <= n; i++)a[i].b = fast_read();
    for(int i = 1; i <= n; i++)a[i].c = fast_read();
    merge(1, n);
    printf("%lld\n", ans);
    return 0;
}