結果

問題 No.1300 Sum of Inversions
ユーザー recososorecososo
提出日時 2020-11-27 22:18:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 2,000 ms
コード長 2,958 bytes
コンパイル時間 2,020 ms
コンパイル使用メモリ 178,160 KB
実行使用メモリ 31,364 KB
最終ジャッジ日時 2023-10-01 06:30:39
合計ジャッジ時間 11,187 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 226 ms
24,632 KB
testcase_04 AC 221 ms
24,236 KB
testcase_05 AC 168 ms
20,396 KB
testcase_06 AC 267 ms
27,340 KB
testcase_07 AC 250 ms
26,212 KB
testcase_08 AC 290 ms
28,724 KB
testcase_09 AC 285 ms
28,728 KB
testcase_10 AC 143 ms
17,496 KB
testcase_11 AC 144 ms
17,760 KB
testcase_12 AC 237 ms
24,084 KB
testcase_13 AC 229 ms
23,568 KB
testcase_14 AC 323 ms
30,644 KB
testcase_15 AC 310 ms
28,396 KB
testcase_16 AC 254 ms
25,220 KB
testcase_17 AC 141 ms
16,920 KB
testcase_18 AC 168 ms
19,024 KB
testcase_19 AC 213 ms
21,716 KB
testcase_20 AC 215 ms
22,392 KB
testcase_21 AC 214 ms
22,192 KB
testcase_22 AC 199 ms
20,480 KB
testcase_23 AC 285 ms
27,260 KB
testcase_24 AC 197 ms
20,872 KB
testcase_25 AC 163 ms
18,620 KB
testcase_26 AC 161 ms
18,340 KB
testcase_27 AC 186 ms
19,876 KB
testcase_28 AC 322 ms
29,256 KB
testcase_29 AC 215 ms
22,060 KB
testcase_30 AC 322 ms
28,328 KB
testcase_31 AC 204 ms
20,752 KB
testcase_32 AC 214 ms
21,392 KB
testcase_33 AC 44 ms
17,300 KB
testcase_34 AC 58 ms
17,176 KB
testcase_35 AC 143 ms
31,364 KB
testcase_36 AC 149 ms
31,324 KB
権限があれば一括ダウンロードができます

ソースコード

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)%mod;
        }
        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))%mod;
            (ans+=cc*rc[i]%mod*x.first%mod)%=mod;
            (ans+=(cc*rd[i]%mod+rc[i]*dd%mod)%mod)%=mod;
        }
    }
    cout << ans << endl;
}
0