雅礼联考DAY02Magic

Posted leiyuanze

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了雅礼联考DAY02Magic相关的知识,希望对你有一定的参考价值。

#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);
}

以上是关于雅礼联考DAY02Magic的主要内容,如果未能解决你的问题,请参考以下文章

雅礼联考DAY01逃跑

雅礼联考DAY01数列

NOIP2016提高A组模拟8.17(雅礼联考day1)Binary

NOIP2016提高A组模拟8.17(雅礼联考day1)Matrix

NOIP2016提高A组模拟8.17(雅礼联考day1)Value

雅礼联考10-27 c 线段树