POJ-2299 Ultra-QuickSort(用树状数组求逆序对数)

题目链接

ac代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mp make_pair
#define pi acos(-1)
#define pii pair
#define pll pair
#define ll long long
#define ld long double
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
const int inf = 0x3f3f3f3f;
const int maxn = 500001;
using namespace std;
int n;
struct Intg
{
    int data;
    int pos;
    bool operator<(const Intg num) const
    {
        return data < num.data;
    }
}a[maxn];
int p[maxn], d[maxn];
int lowbit(int x)
{
    return x & (-x);
}
void update(int x)
{
    while(x <= maxn)
    {
        d[x]++;
        x += lowbit(x);
    }
}
int getsum(int x)
{
    int ans = 0;
    while(x)    //一定写成这种形式,否则写成for循环形式会导致超时
    {
        ans += d[x];
        x -= lowbit(x);
    }
    return ans;
}
int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    while(scanf("%d", &n) != EOF && n)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i].data);
            a[i].pos = i;
        }
        sort(a + 1, a + n + 1);
        p[a[1].pos] = 1;
        int idx = 1;
        for(int i = 2; i <= n ; i++)
        {
            if(a[i].data == a[i-1].data)
                p[a[i].pos] = idx;
            else
                p[a[i].pos] = ++idx;
        }
        ll ans = 0;
        memset(d, 0, sizeof(d));
        for(int i = 1; i <= n; i++)
        {
            update(p[i]);
            ans += i - getsum(p[i]);
        }
        printf("%lld\n", ans);
    }
}

你可能感兴趣的:(POJ-2299 Ultra-QuickSort(用树状数组求逆序对数))