結果

問題 No.694 square1001 and Permutation 3
ユーザー ei1333333
提出日時 2018-06-08 22:28:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,020 ms / 3,000 ms
コード長 1,025 bytes
コンパイル時間 2,402 ms
コンパイル使用メモリ 201,784 KB
最終ジャッジ日時 2025-01-05 10:50:06
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:28:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |   scanf("%d", &N);
      |   ~~~~~^~~~~~~~~~
main.cpp:31:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |     scanf("%d", &A[i]);
      |     ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

#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