結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー kk
提出日時 2020-10-01 10:31:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,500 ms
コード長 963 bytes
コンパイル時間 4,081 ms
コンパイル使用メモリ 198,800 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 06:47:09
合計ジャッジ時間 2,840 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

template <typename T>
class fenwick_tree {
  vector<T> data;
public:
  fenwick_tree() {}
  // manage data in [1, n]
  fenwick_tree(int n) : data(n + 1) {}
  
  void init() {
    fill(data.begin(), data.end(), 0);
  }
  
  // return sum in [1, i]
  T sum(int i){
    T s = 0;
    while(i > 0){
      s += data[i];
      i -= i & -i;
    }
    return s;
  }
  
  // return sum in [l, r]
  T sum(int l, int r) {
    return sum(r) - sum(l-1);
  }
  
  void add(int i, T x){
    while(i < (int)data.size()){
      data[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  int ret = 0;
  fenwick_tree<int> tree(30000);
  REP (i, n) {
    int m;
    cin >> m;
    tree.add(m, 1); 
    if (m == 1)
      ret += i;
    else
      ret += i - tree.sum(m-1);
  }
  
  cout << ret << endl;
  
  return 0;
}
0