結果

問題 No.1300 Sum of Inversions
ユーザー recososorecososo
提出日時 2020-11-27 22:14:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,935 bytes
コンパイル時間 3,276 ms
コンパイル使用メモリ 178,460 KB
実行使用メモリ 31,312 KB
最終ジャッジ日時 2023-10-01 06:22:29
合計ジャッジ時間 11,272 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 231 ms
24,676 KB
testcase_04 AC 237 ms
24,240 KB
testcase_05 AC 196 ms
20,400 KB
testcase_06 AC 299 ms
27,260 KB
testcase_07 AC 267 ms
26,176 KB
testcase_08 AC 316 ms
28,732 KB
testcase_09 AC 290 ms
28,596 KB
testcase_10 AC 140 ms
17,508 KB
testcase_11 AC 147 ms
17,764 KB
testcase_12 AC 224 ms
24,100 KB
testcase_13 AC 222 ms
23,884 KB
testcase_14 AC 320 ms
30,808 KB
testcase_15 AC 286 ms
28,368 KB
testcase_16 AC 238 ms
25,240 KB
testcase_17 AC 133 ms
17,048 KB
testcase_18 AC 158 ms
19,228 KB
testcase_19 AC 194 ms
21,808 KB
testcase_20 AC 201 ms
22,256 KB
testcase_21 AC 197 ms
22,308 KB
testcase_22 AC 177 ms
20,400 KB
testcase_23 AC 271 ms
27,320 KB
testcase_24 AC 189 ms
21,000 KB
testcase_25 AC 151 ms
18,524 KB
testcase_26 AC 152 ms
18,348 KB
testcase_27 AC 173 ms
20,192 KB
testcase_28 AC 292 ms
29,108 KB
testcase_29 AC 195 ms
22,124 KB
testcase_30 AC 277 ms
28,400 KB
testcase_31 AC 181 ms
20,668 KB
testcase_32 AC 185 ms
21,236 KB
testcase_33 AC 42 ms
17,128 KB
testcase_34 AC 57 ms
17,228 KB
testcase_35 AC 145 ms
31,300 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=998244353;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
template< typename T >
struct BIT {
  int N; int max_2beki;

  vector< T > data;
  // 初期化 1-indexedでデータを管理する 0で初期化
  BIT(int size){
      N = ++size;
      data.assign(N, 0);
      max_2beki = 1;
      while(max_2beki * 2 <= N) max_2beki *= 2;
  }

  // [0,k](閉区間)の総和 閉区間に注意!
  T sum(int k) {
    if(k < 0) return 0; // k<0のとき0を返す
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  // [l,r](閉区間)の総和
  inline T sum(int l,int r){
    return sum(r) - sum(l-1);
  }

  // 一点取得 更新はできないことに注意
  inline T operator[](int k){
    return sum(k) - sum(k-1);
  }

  // data[k] += x;
  void add(int k, T x) {
    for(++k; k < N; k += k & -k) data[k] += x;
  }

  // imos法 [l,r]にxを加算
  void imos(int l,int r,T x){
    add(l , x); add(r + 1 , -x);
  }

  // lower_bound sum(i)がval以上となる最小のi
  int lower_bound(T w){
    if(w <= 0) return 0;
    int x = 0;
    for(int k = max_2beki; k > 0; k /= 2){
      if(x+k <= N - 1 && data[x + k] < w){
        w -= data[x + k];
        x += k;
      }
    }
    return x;
  }

  // upper_bound sum(i)がvalより大きくなる最小のi
  int upper_bound(T w){
    if(w < 0) return 0;
    int x = 0;
    for(int k = max_2beki; k > 0; k /= 2){
      if(x+k <= N - 1 && data[x + k] <= w){
        w -= data[x + k];
        x += k;
      }
    }
    return x;
  }

};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;cin >> n;
    vector<ll> a(n),sm(n+1);
    map<ll,vector<ll>> mp;
    REP(i,n){
        cin >> a[i];
        mp[a[i]].push_back(i);
        sm[i+1]=sm[i]+a[i];
    }
    BIT<ll> c(n),d(n);
    ll ans=0;
    for(auto x:mp){
        vector<ll> tmp=x.second;
        int ts=tmp.size();
        vector<ll> rc(ts),rd(ts);
        REP(i,ts){
            rc[i]=c.sum(tmp[i],n-1);
            rd[i]=d.sum(tmp[i],n-1);
        }
        REP(i,ts){
            c.add(tmp[i],1);
            d.add(tmp[i],x.first);
        }
        REP(i,ts){
            ll cc=tmp[i]-c.sum(tmp[i]-1);
            ll dd=sm[tmp[i]]-d.sum(tmp[i]-1);
            (ans+=cc*rc[i]*x.first%mod)%=mod;
            (ans+=(cc*rd[i]+rc[i]*dd)%mod)%=mod;
        }
    }
    cout << ans << endl;
}
0