結果

問題 No.694 square1001 and Permutation 3
コンテスト
ユーザー ei1333333
提出日時 2018-06-08 22:28:00
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 503 ms / 3,000 ms
コード長 1,025 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,434 ms
コンパイル使用メモリ 218,256 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2026-06-06 23:35:02
合計ジャッジ時間 7,230 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

template< class T >
struct BinaryIndexedTree {
  vector< T > data;

  BinaryIndexedTree(int sz) {
    data.assign(++sz, 0);
  }

  T sum(int k) {
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for(++k; k < data.size(); k += k & -k) data[k] += x;
  }
};

int main() {
  int N, A[500000];
  scanf("%d", &N);
  vector< int > vs(N);
  for(int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    vs[i] = A[i];
  }
  sort(begin(vs), end(vs));
  vs.erase(unique(begin(vs), end(vs)), end(vs));
  for(int i = 0; i < N; i++) {
    A[i] = lower_bound(begin(vs), end(vs), A[i]) - begin(vs);
  }
  BinaryIndexedTree< int > bit(vs.size());
  int64 ret = 0;
  for(int i = N - 1; i >= 0; i--) {
    ret += bit.sum(A[i] - 1);
    bit.add(A[i], 1);
  }

  cout << ret << endl;
  for(int i = 0; i < N - 1; i++) {
    ret -= bit.sum(A[i] - 1);
    ret += N - bit.sum(A[i]);
    cout << ret << endl;
  }
}
0