結果

問題 No.694 square1001 and Permutation 3
ユーザー どららどらら
提出日時 2018-10-05 23:44:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,108 ms / 3,000 ms
コード長 1,402 bytes
コンパイル時間 1,725 ms
コンパイル使用メモリ 175,324 KB
実行使用メモリ 62,404 KB
最終ジャッジ日時 2023-08-02 17:23:37
合計ジャッジ時間 14,614 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,372 KB
testcase_01 AC 5 ms
11,312 KB
testcase_02 AC 4 ms
11,388 KB
testcase_03 AC 6 ms
11,216 KB
testcase_04 AC 6 ms
11,300 KB
testcase_05 AC 6 ms
11,216 KB
testcase_06 AC 6 ms
11,220 KB
testcase_07 AC 825 ms
15,344 KB
testcase_08 AC 1,303 ms
24,540 KB
testcase_09 AC 2,108 ms
62,092 KB
testcase_10 AC 842 ms
15,236 KB
testcase_11 AC 2,084 ms
62,404 KB
testcase_12 AC 2,108 ms
62,288 KB
testcase_13 AC 5 ms
11,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct FenwickTree{
  static const int SIZE = 1<<21;
  int n;
  vector<int> tree;
public:
  FenwickTree(int n_):tree(SIZE+1){
    n = n_;
    for(int i=0; i<n+1; i++) tree[i] = 0;
  }
  void add(int i, int value){
    for(; i<n; i|=i+1) tree[i] += value;
  }
  int sum(int i){
    int s = 0;
    for(; i>=0; i=(i&i+1)-1) s += tree[i];
    return s;
  }
};

int main(){
  int N;
  cin >> N;
  vector<int> A;
  map<int,int> mm, mp;
  rep(i,N){
    int a;
    cin >> a;
    A.push_back(a);

    if(mm.count(a) == 0){
      mm[a] = 1;
    }
  }
  int cnt = 0;
  FOR(it,mm){
    mp[it->first] = cnt++;
  }
  
  vector<int> B;
  rep(i,N){
    B.push_back(mp[A[i]]);
  }
  
  FenwickTree ft(1<<21);

  ll sum = N * (ll)(N-1) / 2LL;
  ll ret = 0;
  rep(j,N){
    int tmp = ft.sum(B[j]);
    ret -= tmp;
    ft.add(B[j], 1);
  }

  cout << sum + ret << endl;
  
  rep(j,N-1){
    ft.add(B[j], -1);
    ll tmp1 = ft.sum(B[j]);
    ll tmp2 = ft.sum(N+5) - ft.sum(B[j]-1);
    //cout << tmp2 << " " << tmp1 << endl;
    ret += tmp2 - tmp1;
    cout << sum + ret << endl;
    ft.add(B[j], 1);
  }
  
  return 0;
}
0