求大佬,WA

P1908 逆序对

litterred @ 2024-10-01 21:56:03

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 5e5 + 10;

struct Node{
    ll num, val;
    bool operator< (const Node& no)
    {
        return no.val < val;
    }
}node[N];

int n;
ll f[N];

ll low_bit(ll x)
{
    return x & (-x);
}

void upd(ll pos, ll x)
{
    while(pos <= n)
    {
        f[pos] += x;
        pos += low_bit(pos);
    }
}

ll query(int pos)
{
    ll res = 0;
    while(pos > 0)
    {
        res += f[pos];
        pos -= low_bit(pos);
    }
    return res;
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> node[i].val, node[i].num = i;

    sort(node + 1, node + 1 + n);
    ll res = 0;
    for(int i = 1; i <= n; i++)
    {
        upd(node[i].num, 1);

        res += query(node[i].num - 1);

    }

    cout << res << endl;
    return 0;
}

|