码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj4430

时间:2017-12-03 14:49:14      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:原理   play   容斥   pos   pre   long   逆序对   就是   分享图片   

bit+容斥原理

我不会cdq分治只能用这个做法

考虑什么情况下不满足,至少有一个顺序不对就不行了,那么不满足的总有两对属性形成逆序对,那么我们用总方案数*2=n*(n-1)减去不符合的*2再/2就是答案

似乎进rank前200了

技术分享图片
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
namespace IO 
{
    const int Maxlen = N * 50;
    char buf[Maxlen], *C = buf;
    int Len;
    inline void read_in()
    {
        Len = fread(C, 1, Maxlen, stdin);
        buf[Len] = \0;
    }
    inline void fread(int &x) 
    {
        x = 0;
        int f = 1;
        while (*C < 0 || 9 < *C) { if(*C == -) f = -1; ++C; }
        while (0 <= *C && *C <= 9) x = (x << 1) + (x << 3) + *C - 0, ++C;
        x *= f;
    }
    inline void fread(long long &x) 
    {
        x = 0;
        long long f = 1;
        while (*C < 0 || 9 < *C) { if(*C == -) f = -1; ++C; }
        while (0 <= *C && *C <= 9) x = (x << 1) + (x << 3) + *C - 0, ++C;
        x *= f;
    }
    inline void read(int &x)
    {
        x = 0;
        int f = 1; char c = getchar();
        while(c < 0 || c > 9) { if(c == -) f = -1; c = getchar(); }
        while(c >= 0 && c <= 9) { x = (x << 1) + (x << 3) + c - 0; c = getchar(); }
        x *= f;
    }
    inline void read(long long &x)
    {
        x = 0;
        long long f = 1; char c = getchar();
        while(c < 0 || c > 9) { if(c == -) f = -1; c = getchar(); }
        while(c >= 0 && c <= 9) { x = (x << 1ll) + (x << 3ll) + c - 0; c = getchar(); }
        x *= f;
    } 
} using namespace IO;
int n;
int t[N], a[N], b[N], c[N], pos[N];
void update(int x, int d)
{
    for(; x <= n; x += x & -x) t[x] += d;    
} 
int query(int x)
{
    int ret = 0;
    for(; x; x -= x & -x) ret += t[x];
    return ret;
}
long long solve(int *a, int *b)
{
    long long ret = 0;
    for(int i = 1; i <= n; ++i) 
    {
        pos[a[i]] = i;
        t[i] = 0;
    }
    for(int i = n; i; --i) 
    {
        ret += query(pos[b[i]]);
        update(pos[b[i]], 1);
    }
    return ret;
}
int main()
{
    read_in();
    fread(n);
    for(int i = 1; i <= n; ++i) fread(a[i]);
    for(int i = 1; i <= n; ++i) fread(b[i]);
    for(int i = 1; i <= n; ++i) fread(c[i]);
    printf("%lld\n", ((long long)n * (long long)(n - 1) - solve(a, b) - solve(b, c) - solve(c, a)) >> 1); 
    return 0;
}
View Code

 

bzoj4430

标签:原理   play   容斥   pos   pre   long   逆序对   就是   分享图片   

原文地址:http://www.cnblogs.com/19992147orz/p/7965934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!