結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー mamekinmamekin
提出日時 2015-01-20 23:00:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,866 ms / 5,000 ms
コード長 1,929 bytes
コンパイル時間 866 ms
コンパイル使用メモリ 100,500 KB
実行使用メモリ 61,644 KB
最終ジャッジ日時 2023-09-11 04:01:35
合計ジャッジ時間 7,548 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
5,476 KB
testcase_01 AC 78 ms
6,524 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 522 ms
7,928 KB
testcase_04 AC 2,866 ms
61,644 KB
testcase_05 AC 488 ms
7,924 KB
testcase_06 AC 416 ms
6,956 KB
testcase_07 AC 472 ms
7,052 KB
testcase_08 AC 533 ms
7,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class BinaryIndexedTree
{
    int n;
    vector<int> data;
public:
    BinaryIndexedTree(int n){ // コンストラクタ
        this->n = n;
        data.assign(n+1, 0);
    }
    void add(int k, int x){ // k番目の要素にxを加算する
        ++ k;
        while(k <= n){
            data[k] += x;
            k += k & -k;
        }
    }
    int sum(int k){ // 区間[0,k]の総和を返す
        ++ k;
        int ret = 0;
        while(k > 0){
            ret += data[k];
            k -= k & -k;
        }
        return ret;
    }
    int sum(int a, int b){ // 区間[a,b]の総和を返す
        return sum(b) - sum(a-1);
    }
};

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    map<int, int> index;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        index[a[i]];
    }

    int m = 0;
    for(auto& p : index){
        p.second = m;
        ++ m;
    }
    for(int i=0; i<n; ++i)
        a[i] = index[a[i]];

    BinaryIndexedTree left(m), right(m);
    for(int i=0; i<n; ++i)
        right.add(a[i], 1);

    long long same = 0;
    long long ret = 0;
    for(int i=0; i<n; ++i){
        right.add(a[i], -1);
        same -= left.sum(a[i], a[i]);

        ret += (long long)left.sum(0, a[i] - 1) * right.sum(0, a[i] - 1) +
               (long long)left.sum(a[i] + 1, m - 1) * right.sum(a[i] + 1, m - 1) +
               (long long)left.sum(a[i], a[i]) * right.sum(a[i], a[i]) - same;

        left.add(a[i], 1);
        same += right.sum(a[i], a[i]);
    }
    cout << ret << endl;

    return 0;
}
0