結果
| 問題 | No.1300 Sum of Inversions | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2024-09-01 11:00:41 | 
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,758 ms / 2,000 ms | 
| コード長 | 2,411 bytes | 
| コンパイル時間 | 3,996 ms | 
| コンパイル使用メモリ | 288,520 KB | 
| 実行使用メモリ | 94,536 KB | 
| 最終ジャッジ日時 | 2024-09-01 11:01:29 | 
| 合計ジャッジ時間 | 47,590 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 34 | 
ソースコード
#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
#include <atcoder/modint>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
template<typename S, typename T> struct CompressedBIT2D {
 private:
  int N;
  vector<fenwick_tree<T>> bit;
  vector<vector<S>> ys;
  vector<pair<S, S>> ps;
  int id(S x) const {
    return ranges::lower_bound(ps, make_pair(x, S()), [](const pair<S, S> &a, const pair<S, S> &b) { return a.first < b.first; }) - ps.begin();
  }
  int id(int i, S y) const { return ranges::lower_bound(ys[i], y) - ys[i].begin(); }
 public:
  CompressedBIT2D() = default;
  CompressedBIT2D(int N) { ps.reserve(N); }
  void use(S x, S y) { ps.emplace_back(x, y); }
  void build() {
    ranges::sort(ps);
    ps.erase(unique(ps.begin(), ps.end()), ps.end());
    N = ps.size();
    bit.resize(N + 1);
    ys.resize(N + 1);
    for(int i = 0; i <= N; ++i) {
      for(int j = i + 1; j <= N; j += j & -j) { ys[j].emplace_back(ps[i].second); }
      ranges::sort(ys[i]);
      ys[i].erase(unique(ys[i].begin(), ys[i].end()), ys[i].end());
      bit[i] = fenwick_tree<T>(ys[i].size() + 1);
    }
  }
  void add(S x, S y, T a) {
    int i = ranges::lower_bound(ps, make_pair(x, y)) - ps.begin();
    assert(ps[i] == make_pair(x, y));
    for(++i; i <= N; i += i & -i) { bit[i].add(id(i, y), a); }
  }
  T sum(S x, S y) {
    T r = T();
    for(int a = id(x); a; a -= a & -a) { r += bit[a].sum(0, id(a, y)); }
    return r;
  }
  T sum(S lx, S rx, S ly, S ry) {
    T r = T();
    int a = id(lx), b = id(rx);
    while(a != b) {
      if(a < b) {
        r += bit[b].sum(id(b, ly), id(b, ry));
        b -= b & -b;
      }
      else {
        r -= bit[a].sum(id(a, ly), id(a, ry));
        a -= a & -a;
      }
    }
    return r;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vector<ll> A(N);
  CompressedBIT2D<ll, mint> S, C;
  for(ll i = 0; i < N; i++) {
    cin >> A[i];
    S.use(i, A[i]);
    C.use(i, A[i]);
  }
  S.build();
  C.build();
  for(ll i = 0; i < N; i++) {
    S.add(i, A[i], A[i]);
    C.add(i, A[i], 1);
  }
  mint ans = 0;
  for(ll i = 0; i < N; i++) {
    mint ls = S.sum(0, i, A[i] + 1, 1e18), rs = S.sum(i + 1, N, 0, A[i]);
    mint lc = C.sum(0, i, A[i] + 1, 1e18), rc = C.sum(i + 1, N, 0, A[i]);
    ans += ls * rc + rs * lc + lc * rc * A[i];
  }
  cout << ans.val() << "\n";
}
            
            
            
        