1 条题解

  • 0
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #define int long long
    
    using namespace std;
    
    const int N = 2e5 + 10;
    
    int n;
    int a[N];
    int high[N], low[N];
    int tr[N];
    
    int lowbit(int x)
    {
        return x & -x;
    }
    
    void add(int x, int val)
    {
        for (int i = x; i <= n; i += lowbit(i)) tr[i] += val;
    }
    
    int sum(int x)
    {
        int res = 0;
        for (int i = x; i; i -= lowbit(i)) res += tr[i];
    
        return res;
    }
    
    signed main()
    {
        cin >> n;
        
        for (int i = 1; i <= n; i ++)
            cin >> a[i];
        
        int r1 = 0, r2 = 0;
        for (int i = 1; i <= n; i ++ )
        {
            high[i] = sum(n) - sum(a[i]);
            low[i] = sum(a[i] - 1);
            r1 += high[i] * (n - a[i] - high[i]);
            r2 += low[i] * (a[i] - 1 - low[i]);
            add(a[i], 1);
        }
        
        cout << r1 << " " << r2 << endl;
    }
    
    • 1

    信息

    ID
    152
    时间
    1000ms
    内存
    128MiB
    难度
    9
    标签
    递交数
    14
    已通过
    5
    上传者