美文网首页
(树状数组) AcWing 241. 楼兰图腾

(树状数组) AcWing 241. 楼兰图腾

作者: 来到了没有知识的荒原 | 来源:发表于2021-03-15 16:38 被阅读0次

AcWing 241. 楼兰图腾

树状数组

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 2000010;
int n;
int a[N], tr[N];
int Lower[N], Greater[N];


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

void add(int x, int v) {
    for (int i = x; i < N; i += lowbit(i))tr[i] += v;
}

int get(int x) {
    int res = 0;
    for (int i = x; i; i -= lowbit(i))res += tr[i];
    return res;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++)cin >> a[i];

    for (int i = 1; i <= n; i++) {
        int y = a[i];
        Lower[i] = get(y - 1);
        Greater[i] = get(n) - get(y);
        add(y, 1);
    }

    memset(tr, 0, sizeof tr);
    LL res1 = 0, res2 = 0;
    for (int i = n; i >= 1; i--) {
        int y = a[i];
        res1 += Greater[i] * 1ll * (get(n) - get(y));
        res2 += Lower[i] * 1ll * get(y - 1);
        add(y, 1);
    }

    cout << res1 << " " << res2;

    return 0;
}

相关文章

  • (树状数组) AcWing 241. 楼兰图腾

    AcWing 241. 楼兰图腾[https://www.acwing.com/activity/content/...

  • 数据结构(二)

    树状数组 POJ 1990: MooFest关于x坐标建立树状数组。先按照v值升序排序,逐个添加到树状数组里。每次...

  • 三种一维树状数组

    单点修改+区间查询 最基本的树状数组 树状数组入门 模板(洛谷P3374 【模板】树状数组1) 区间修改+单点查询...

  • 树状结构转一维数组

    树状结构转数组方法 声明树状对象

  • 树状数组

    复习一下树状数组 树状数组 一种用于处理单点修改和区间查询的数据结构。树状数组C的定义: C[x] = Sum ...

  • 树状数组模板复习

    树状数组模板复习

  • 树状数组求逆序对原理

    归并排序和树状数组都可以用nlogn的算法做到求出逆序对.但这里着重讲树状数组的原理与求法.树状数组最常用的方面就...

  • 「树状数组」

    题目传送门 poj1990题意: 农夫的N (N∈[1, 20,000])头牛参加了"MooFest"之后有了不...

  • 树状数组

    参见: https://www.cnblogs.com/hsd-/p/6139376.htmlhttps://bl...

  • 树状数组

    created by Dejavu*[完结] 简介树状数组(Binary Indexed Tree(BIT), F...

网友评论

      本文标题:(树状数组) AcWing 241. 楼兰图腾

      本文链接:https://www.haomeiwen.com/subject/azvkcltx.html