bzoj4430

bit+容斥原理

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

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

似乎进rank前200了

#include
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

 

转载于:https://www.cnblogs.com/19992147orz/p/7965934.html

你可能感兴趣的:(bzoj4430)