結果

問題 No.1300 Sum of Inversions
ユーザー CleyLCleyL
提出日時 2022-10-22 00:51:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,278 bytes
コンパイル時間 1,391 ms
コンパイル使用メモリ 96,396 KB
実行使用メモリ 22,008 KB
最終ジャッジ日時 2023-09-14 00:44:16
合計ジャッジ時間 20,777 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 41 ms
6,160 KB
testcase_34 AC 94 ms
6,200 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
//B
template<typename T>
struct BIT{//1_Indexed
    int n;
    vector<T> bit;
    BIT(int n_):n(n_+1),bit(n,0){}

    T sum(int a){
        int ret = 0;
        for(int i = a; i > 0; i -= i & -i) ret += bit[i];
        return ret;
    }

    void add(int a,int w){
        for(int i = a; i <= n; i += i & -i)bit[i] += w;
    }

    T query(int r,int l){
        return sum(l-1)-sum(r-1);
    }

};
//E
int main(){
  int n;cin>>n;
  vector<long long> A(n),B(n);
  for(int i = 0; n > i; i++)cin>>A[i],B[i] = A[i];
  sort(B.begin(),B.end());
  B.erase(unique(B.begin(),B.end()),B.end());

  map<int,int> C;
  for(int i = 0; B.size() > i; i++){
    C[B[i]] = i+1;
  }
  BIT<long long> X(B.size()+1),Y(B.size()+1),Z(B.size()+1),W(B.size()+1);
  for(int i = 0; n > i; i++){
    Y.add(C[A[i]],1);
    W.add(C[A[i]],A[i]);
  }
  long long ans = 0;
  for(int i = 0; n > i; i++){
    auto x = X.query(C[A[i]]+1,B.size()+1);
    auto y = Y.query(1,C[A[i]]);
    auto z = Z.query(C[A[i]]+1,B.size()+1);
    auto w = W.query(1,C[A[i]]);
    ans += z*y + A[i]*x*y + w*x;
    X.add(C[A[i]],1);
    Y.add(C[A[i]],-1);
    Z.add(C[A[i]],A[i]);
    W.add(C[A[i]],-A[i]);
  }
  cout << ans << endl;
}

0