結果

問題 No.694 square1001 and Permutation 3
ユーザー ojisan_ITojisan_IT
提出日時 2018-06-08 23:24:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,758 ms / 3,000 ms
コード長 1,931 bytes
コンパイル時間 2,462 ms
コンパイル使用メモリ 177,680 KB
実行使用メモリ 50,100 KB
最終ジャッジ日時 2023-09-13 00:18:42
合計ジャッジ時間 12,298 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,000 KB
testcase_01 AC 4 ms
6,992 KB
testcase_02 AC 3 ms
6,852 KB
testcase_03 AC 5 ms
7,060 KB
testcase_04 AC 4 ms
7,000 KB
testcase_05 AC 5 ms
6,884 KB
testcase_06 AC 5 ms
6,912 KB
testcase_07 AC 822 ms
18,748 KB
testcase_08 AC 1,410 ms
25,028 KB
testcase_09 AC 1,754 ms
50,100 KB
testcase_10 AC 790 ms
18,640 KB
testcase_11 AC 1,758 ms
49,848 KB
testcase_12 AC 1,737 ms
49,840 KB
testcase_13 AC 3 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vt = vector<T>;
template<class T> using vvt = vector<vt<T>>;
template<class T> using ttt = tuple<T,T>;
using tii = tuple<int,int>;
using vi = vector<int>;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(),(a).end()
#define FST first
#define SEC second
#define DEB cerr<<"!"<<endl
#define SHOW(a,b) cerr<<(a)<<" "<<(b)<<endl
#define DIV int(1e9+7)
const int INF = (INT_MAX/2);
const ll LLINF = (LLONG_MAX/2);
const double eps = 1e-8;
//const double PI = M_PI;  
inline ll pow(ll x,ll n,ll m){ll r=1;while(n>0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;}
inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;}

/*Coding Space*/
template<class T>
struct BIT{
    vector<T> d;
    BIT(size_t n):d(n+1){}
    T sum(size_t i){T s{};for(++i;i>0;i-=i&-i)s+=d[i];return s;}
    T sum(size_t i,size_t j){return i?sum(j)-sum(i-1):sum(j);}
    void add(size_t i,T x){for(++i;i<d.size();i+=i&-i)d[i]+=x;}
};

ll inversion(vt<ll> a){ // n = a.size  O(n log n)
  BIT<ll> bit(500050);
  ll ret = 0;
  rep(i,a.size()){
    ret += i - bit.sum(a[i]);
    bit.add(a[i],1);
  }
  return ret;
}
int main(){
  int n;  cin >> n;
  if(n > 500000){cout << "Error" << endl; return 0;}
  vt<ll> in(n); rep(i,n)cin >> in[i];

  vt<ll> t(n);
  rep(i,n) t[i] = in[i];
  sort(ALL(t));
  map<ll,ll> zatsu;
  rep(i,n) zatsu[t[i]] = i;
  //DEB;
  rep(i,n){
    in[i] = zatsu[in[i]];
    //cerr << in[i] << endl;
  }
  
  ll ans = inversion(in);
  rep(i,n) t[i] = zatsu[t[i]];

  rep(i,n){
    if(i == 0)cout << ans << endl;
    else{
      int lower = distance(t.begin(), lower_bound(ALL(t),in[i-1]));
      int upper = distance(upper_bound(ALL(t), in[i-1]) , t.end());
      //cerr << i << " " << lower << " " << upper << endl;
      ans = ans - lower + upper;
      cout << ans << endl;
    }
  }
}
0