树状数组求逆序对

逆序对定义

在计算机科学和离散数学中,逆序对是指序列中的两个元素,它们的顺序与它们在原始序列中的顺序相反。更具体地说,对于一个包含 n n n 个元素的序列,逆序对是指满足 a i > a j , i < j a_i>a_j,i<j ai>aj,i<j 的索引对 ( i , j ) (i, j) (i,j) 的数量,其中 a i a_i ai a j a_j aj 分别表示序列中的第 i i i 和第 j j j 个元素。

思路

树状数组表征值域,我们只需要对包含第 i i i 个数的数组 + 1 +1 +1,每次统计一下 i + 1 → N i+1 \to N i+1N 出现的次数即可。但是面对这种问题一般需要对数据进行离散化。

离散化定义

在离散数学和计算机科学中,离散化是指将连续的数据或变量转换为离散的值或类别的过程。这个过程通常涉及将连续的范围划分为若干个间隔或区间,并为每个区间分配一个离散的值或类别标签。离散化的目的通常是简化数据处理或分析,以及减少数据的复杂性。例如,在数据挖掘中,离散化可以用来将连续的数值特征转换为分类特征,以便更好地应用分类算法。

代码

	cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> b(all(a));
    sort(all(b));
    b.erase(unique(all(b)), b.end());
    int m = b.size();

在我们查询时,只需要查询 a i a_i ai 在离散化数组中的位置。

int pos = lower_bound(all(b), a[i]) - b.begin() + 1;

代码

#include <bits/stdc++.h>
using namespace std;

#define all(x) x.begin(), x.end()
#define bit1(x) __builtin_popcount(x)
#define Pqueue priority_queue
#define lc p << 1
#define rc p << 1 | 1
#define IOS ios::sync_with_stdio(false), cin.tie(0);
#define fi first
#define se second
#define lowbit(x) (x & -x)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> PII;

const ll mod = 1000000007;
const ll N = 1e6 + 10;
const ld eps = 1e-9;
const ll inf = 1e18;
const ll P = 131;
const ll dir[8][2] = {1, 0, 0, 1, -1, 0, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1};

int n, tr[N];
int t, r;

void change(int x, int k)
{
    while (x <= n)
    {
        tr[x] += k;
        x += lowbit(x);
    }
}
int query(int x)
{
    int ans = 0;
    while (x)
    {
        ans += tr[x];
        x -= lowbit(x);
    }
    return ans;
}

void solve()
{
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> b(all(a));
    sort(all(b));
    b.erase(unique(all(b)), b.end());
    int m = b.size();
    // cout << m << "\n";
    // for (auto i : b)
    //     cout << i << "\n";
    ll ans = 0;
    // cout << a.size();
    for (int i = 0; i < n; i++)
    {
        int pos = lower_bound(all(b), a[i]) - b.begin() + 1;
        ans += query(n) - query(pos);
        change(pos, 1);
    }
    cout << ans << "\n";
}

int main()
{
    int T = 1;
    // cin>>T;
    while (T--)
        solve();
    return 0;
}

/*
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
x                                                                                      o
o       _/_/_/_/                                                              _/       x
x      _/                                                                              o
o     _/_/_/_/ _/  _/_/   _/_/   _/_/_/ _/_/   _/_/_/     _/_/    _/_/_/    _/ _/   _/ x
x    _/       _/_/     _/    _/ _/   _/   _/  _/    _/ _/    _/  _/    _/  _/   _/ _/  o
o   _/       _/       _/    _/ _/   _/   _/  _/    _/ _/    _/  _/    _/  _/    _/_/   x
x  _/       _/         _/_/   _/   _/   _/  _/_/_/     _/_/ _/ _/    _/  _/      _/    o
o                                          _/                           _/      _/     x
x                                         _/                        _/_/       _/      o
o                                                                                      x
xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo
*/```

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

panjyash

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值