結果

問題 No.1300 Sum of Inversions
ユーザー umezoumezo
提出日時 2020-11-28 04:37:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,558 bytes
コンパイル時間 2,349 ms
コンパイル使用メモリ 207,996 KB
実行使用メモリ 11,088 KB
最終ジャッジ日時 2023-10-09 23:27:48
合計ジャッジ時間 8,502 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 90 ms
10,776 KB
testcase_34 AC 146 ms
10,808 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const int MOD=998244353;

template <typename T>
vector<T> compress(vector<T> &X){
  vector<T> vals=X;
  sort(vals.begin(),vals.end());
  vals.erase(unique(vals.begin(), vals.end()),vals.end());
  for(int i=0;i<(int)X.size(); i++){
    X[i]=lower_bound(vals.begin(),vals.end(),X[i])-vals.begin()+1;
  }
  return vals;
}

template<typename T>
struct BIT{
private:
  vector<T> A;
  const int n;
  
public:
  BIT(int _n) : A(_n+1,0), n(_n){}

  T sum(int i){
    T s=0;
    while(i>0){
      s=(s+A[i])%MOD;
      i-=i&-i;
    }
    return s;
  }
  
  T sum(int i,int j){
    return (sum(j)-sum(i-1)+MOD)%MOD;
  }
  
  void add(int i,T x){
    while(i<=n){
      A[i]=(A[i]+x)%MOD;
      i+=i&-i;
    }
  } 
};

int main(){
  int n;
  cin>>n;
  
  vector<int> A(n),B(n);
  rep(i,n) cin>>A[i];
  B=A;
  
  compress(B);
  BIT<int> bitlcnt(n),bitlsum(n),bitrcnt(n),bitrsum(n);  
  vector<int> Lcnt(n),Rcnt(n),Lsum(n),Rsum(n);
  rep(i,n){
    Lcnt[i]=bitlcnt.sum(B[i]+1,n);
    bitlcnt.add(B[i],1);
    Lsum[i]=bitlsum.sum(B[i]+1,n);
    bitlsum.add(B[i],A[i]);
  }
  for(int i=n-1;i>=0;i--){
    Rcnt[i]=bitrcnt.sum(B[i]-1);
    bitrcnt.add(B[i],1);
    Rsum[i]=bitrsum.sum(B[i]-1);
    bitrsum.add(B[i],A[i]);
  }
  
  int ans=0;
  rep(i,n){
    ans=(ans+Rcnt[i]*Lcnt[i]%MOD*A[i]%MOD)%MOD;
    ans=(ans+Rsum[i]*Lcnt[i]%MOD)%MOD;
    ans=(ans+Lsum[i]*Rcnt[i]%MOD)%MOD;
  }
  cout<<ans<<endl;
  
  return 0;
}
0