UVA4329 Pingpong ——树状数组+思维

蓝书P197

不是很显然的树状数组题

需要对于每个i,分别统计其左右两侧小于a[i]的人数数量。

注意开long long

注意人数上限2e4但是人的能力上限1e5, 更新C数组的时候要一直更新到1e5

// Decline is inevitable,
// Romance will last forever.
//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <vector>
using namespace std;
#define mst(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
//#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define int long long
const int maxn = 2e4 + 10;
const int maxm = 1e5;
const int P = 1e9 + 7;
int C[maxm], n, m;
int p[maxn], s[maxn];
int lb(int x) {return x & -x;}
void update(int x, int d) {
    while(x <= maxm) {  //此处注意
        C[x] += d;
        x += lb(x);
    }
}
int query(int x) {
    int res = 0;
    while(x) {
        res += C[x];
        x -= lb(x);
    }
    return res;
}
int a[maxn];
void solve() {
    cin >> n;
    mst(C, 0);
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        update(a[i], 1);
        p[i] = query(a[i]-1);
    }
    mst(C, 0);
    for(int i = n; i > 0; i--) {
        update(a[i], 1);
        s[i] = query(a[i]-1);
    }
    for(int i = 1; i <= n; i++)
        ans += p[i]*(n-i-s[i]) + s[i]*(i-1-p[i]);
    cout << ans << '\n';
}
signed main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//    int T; scanf("%d", &T); while(T--)
    int T; cin >> T; while(T--)
    solve();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值