【雅礼联考DAY02】Magic
阅读原文时间:2023年07月08日阅读:1
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;

const int N = 1e5;
int n , lim , x[N + 5] , y[N + 5] , z[N + 5] , sum;
int bl[(N << 1) + 5] , c[(N << 1) + 5] , p[(N << 1) + 5];
struct point{
    int l , r , bl;
}a[N + 5];
LL ans;

inline int read()
{
    char ch = getchar();
    int res = 0;
    for(; ch < '0' || ch > '9'; ch = getchar());
    for(; ch >= '0' && ch <= '9'; ch = getchar()) res = (res << 3) + (res << 1) + ch - '0';
    return res;
}
inline int lowbit(int x) {return x & (-x);}
inline bool cmp(point x , point y){return x.r < y.r;}

inline void add(int x , int v)
{
    for(; x <= lim; x += lowbit(x)) c[x] += v;
}

inline int query(int x)
{
    int res = 0;
    for(; x; x -= lowbit(x)) res += c[x];
    return res;
}

int main()
{
//    freopen("Magic.in" , "r" , stdin);
    scanf("%d" , &n);
    for(register int i = 1; i <= n; i++)
    {
        a[i].l = read() , a[i].r = read();
        if (a[i].l > a[i].r) swap(a[i].l , a[i].r);
        a[i].bl = bl[a[i].l] = bl[a[i].r] = i;
        p[a[i].l] = 1 , p[a[i].r] = 2;
    }
    lim = n << 1;

    sum = 0;
    for(register int i = 1; i <= lim; i++)
    {
        if (p[i] == 1) x[bl[i]] += sum;
        else sum++;
    }

    sum = 0;
    for(register int i = lim; i; i--)
    {
        if (p[i] == 2) x[bl[i]] += sum;
        else sum++;
    }

    sort(a + 1 , a + n + 1 , cmp);
    for(register int i = n; i; i--)
    {
        x[a[i].bl] += query(a[i].l);
        add(a[i].l , 1);
    }
    memset(c , 0 , sizeof c);
    for(register int i = 1; i <= n; i++)
    {
        y[a[i].bl] = query(a[i].r) - query(a[i].l);
        add(a[i].l , 1);
    }

    for(register int i = 1; i <= n; i++) z[i] = n - x[i] - y[i] - 1;
    for(register int i = 1; i <= n; i++)
        ans += 1LL * x[i] * y[i] + 1LL * (x[i] + y[i]) * z[i] / 2;

    ans = 1LL * n * (n - 1) / 2 * (n - 2) / 3 - ans;
    printf("%lld" , ans);
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章