UVALive 4329 Ping pong(树状数组求逆序数+顺序数)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2330

题意:对于给定的一个长度为n序列a,对于每个位置i,若左边存在一个数a[l],右边存在一个数a[r],满足a[l] < a[i] < a[r]或a[l] > a[i] > a[r],则(l, r)构成一对,求总共有多少对。

思路:虽然先学的是线段树,但是这类问题也可以用树状数组解决。考虑2个数组c和d,对于位置i,c[i]表示位置i左边有c[i]个数比它小,d[i]表示位置i右边有d[i]个数比它小。则最后的结果就是

i=1nc[i](nid[i])+(i1c[i])d[i]

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

const int N = 2e4 + 10;
const int M = 1e5 + 10;

int s[M << 2];
int c[M], d[M];
int a[N];
int n;

int lowbit(int x) {
    return x & (-x);
}

void add(int x) {
    while (x <= M) {
        s[x]++;
        x += lowbit(x);
    }
}

int sum(int x) {
    int res = 0;
    while (x > 0) {
        res += s[x];
        x -= lowbit(x);
    }
    return res;
}

int main() {    
    int t_case;
    scanf("%d", &t_case);
    for (int i_case = 1; i_case <= t_case; i_case++) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);

        memset(s, 0, sizeof(s));
        for (int i = 1; i <= n; i++) {
            c[i] = sum(a[i] - 1);
            add(a[i]);
        }

        memset(s, 0, sizeof(s));
        for (int i = n; i >= 1; i--) {
            d[i] = sum(a[i] - 1);
            add(a[i]);
        }

        long long res = 0;
        for (int i = 2; i < n; i++)
            res += 1LL * c[i] * (n - i - d[i]) + 1LL * (i - 1 - c[i]) * d[i];
        printf("%lld\n", res);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值