結果

問題 No.694 square1001 and Permutation 3
ユーザー sugdxsugdx
提出日時 2018-06-08 22:57:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,908 ms / 3,000 ms
コード長 1,142 bytes
コンパイル時間 2,332 ms
コンパイル使用メモリ 184,500 KB
実行使用メモリ 50,004 KB
最終ジャッジ日時 2023-09-13 00:06:42
合計ジャッジ時間 12,953 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 829 ms
18,704 KB
testcase_08 AC 1,425 ms
24,728 KB
testcase_09 AC 1,862 ms
49,780 KB
testcase_10 AC 770 ms
18,852 KB
testcase_11 AC 1,897 ms
50,004 KB
testcase_12 AC 1,908 ms
49,972 KB
testcase_13 AC 2 ms
4,380 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);
  vector<long long> a(n);
  map<long long, int> ma;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first; p[i].second = i;
    a[i] = p[i].first;
    if (ma.find(p[i].first) != ma.end()) ma[p[i].first]++;
    else ma.insert(make_pair(p[i].first, 1));
  }
  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) - (ma[a[i]]-1);
    ma[a[i]] -= 2;
    cout << tmp << endl;
  }
  return 0;
}
0