PAT 1009. Triple Inversions (35)

树状数组
A[0]数组用来求大于k的个数
A[1]数组用来求大于x的二维逆序对个数
没遍历一个数x,ans都加上到当前位置前方大于x的二维逆序对个数

#pragma warning(disable:4996)
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn = 100005;
//树状数组
LL A[2][maxn],N,x,ans;//A树状数组
LL lowbit(LL n) {   return n & -n;}
LL Get_sum(LL index,int f) {//1~index的和
    LL sum = 0;
    while (index)
        sum += A[f][index], index -= lowbit(index);
    return sum;
}
void Add(LL val, LL index,int f) {//index位置加上val
    while (index < maxn)
        A[f][index] += val, index += lowbit(index);
}
int main() {
#ifdef _DEBUG
    freopen("in", "r", stdin);
#endif // _DEBUG
    scanf("%d", &N);
    for (int i = 1; i <= N;++i) {
        scanf("%d", &x);
        Add(1, x, 0);
        Add(Get_sum(maxn-1,0)-Get_sum(x,0), x, 1);
        ans += Get_sum(maxn - 1, 1)-Get_sum(x, 1);
    }
    cout << ans << endl;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值