結果

問題 No.694 square1001 and Permutation 3
ユーザー sugdxsugdx
提出日時 2018-06-08 22:38:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 931 bytes
コンパイル時間 1,791 ms
コンパイル使用メモリ 175,328 KB
実行使用メモリ 14,988 KB
最終ジャッジ日時 2023-09-12 23:59:41
合計ジャッジ時間 8,406 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// one-based numbering
struct Fenwick {
    vector<int> bit; int N;
    Fenwick(int n) {
      N = n;
      bit.resize(n+1, 0);
    }
    void add(int a, int w) {
      for (int x = a; x <= N; x += x & -x) bit[x] += w;
    }
    int sum(int a) {
      int ret = 0;
      for (int x = a; x > 0; x -= x & -x) ret += bit[x];
      return ret;
    }
};


int main(){
  int n; cin >> n;
  vector<int> v(n); vector< pair<long long, int> > p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].first; p[i].second = i;
  }
  sort(p.begin(), p.end());
  for (int i = 0; i < n; i++) {
    v[p[i].second] = i+1;
  }
  long long tmp = 0;
  Fenwick f = Fenwick(n);
  for (int i = 0; i < n; i++) {
    tmp += (long long) (i - f.sum(v[i]));
    f.add(v[i], 1);
  }
  cout << tmp << endl;
  for (int i = 0; i < n-1; i++) {
    tmp += (n - v[i]) - (v[i]-1);
    cout << tmp << endl;
  }
  return 0;
}
0