結果

問題 No.1300 Sum of Inversions
ユーザー kusaf_kusaf_
提出日時 2024-09-01 11:02:31
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,450 ms / 2,000 ms
コード長 6,955 bytes
コンパイル時間 4,251 ms
コンパイル使用メモリ 295,204 KB
実行使用メモリ 53,380 KB
最終ジャッジ日時 2024-09-01 11:03:10
合計ジャッジ時間 39,524 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1,116 ms
42,352 KB
testcase_04 AC 1,065 ms
41,272 KB
testcase_05 AC 877 ms
33,160 KB
testcase_06 AC 1,242 ms
46,368 KB
testcase_07 AC 1,192 ms
44,592 KB
testcase_08 AC 1,372 ms
47,968 KB
testcase_09 AC 1,334 ms
48,224 KB
testcase_10 AC 661 ms
28,308 KB
testcase_11 AC 710 ms
28,444 KB
testcase_12 AC 1,125 ms
40,756 KB
testcase_13 AC 1,106 ms
39,788 KB
testcase_14 AC 1,421 ms
52,764 KB
testcase_15 AC 1,387 ms
47,712 KB
testcase_16 AC 1,111 ms
42,480 KB
testcase_17 AC 681 ms
27,388 KB
testcase_18 AC 810 ms
30,660 KB
testcase_19 AC 971 ms
38,256 KB
testcase_20 AC 993 ms
37,524 KB
testcase_21 AC 984 ms
37,436 KB
testcase_22 AC 864 ms
33,032 KB
testcase_23 AC 1,234 ms
46,876 KB
testcase_24 AC 888 ms
33,872 KB
testcase_25 AC 752 ms
29,844 KB
testcase_26 AC 722 ms
29,376 KB
testcase_27 AC 830 ms
32,364 KB
testcase_28 AC 1,450 ms
49,024 KB
testcase_29 AC 959 ms
37,396 KB
testcase_30 AC 1,292 ms
48,892 KB
testcase_31 AC 876 ms
33,220 KB
testcase_32 AC 892 ms
34,120 KB
testcase_33 AC 363 ms
47,116 KB
testcase_34 AC 456 ms
46,992 KB
testcase_35 AC 738 ms
52,708 KB
testcase_36 AC 738 ms
53,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
  }
};

struct SuccinctIndexableDictionary {
  ll len;
  ll blk;
  vector<ll> bit, sum;
  SuccinctIndexableDictionary() = default;
  SuccinctIndexableDictionary(ll len): len(len), blk((len + 31) >> 5) {
    bit.assign(blk, 0LL);
    sum.assign(blk, 0LL);
  }
  void set(ll k) { bit[k >> 5] |= 1LL << (k & 31); }
  void build() {
    sum[0] = 0LL;
    for(ll i = 1; i < blk; i++) { sum[i] = sum[i - 1] + __builtin_popcountll(bit[i - 1]); }
  }
  bool operator[](ll k) { return (bool((bit[k >> 5] >> (k & 31)) & 1)); }
  ll rank(ll k) { return (sum[k >> 5] + __builtin_popcountll(bit[k >> 5] & ((1LL << (k & 31)) - 1))); }
  ll rank(bool val, ll k) { return (val ? rank(k) : k - rank(k)); }
};

template<typename T, ll MAXLOG> struct WaveletMatrix {
  ll len;
  SuccinctIndexableDictionary mat[MAXLOG];
  ll mid[MAXLOG];
  WaveletMatrix() = default;
  WaveletMatrix(vector<T> v): len(v.size()) {
    vector<T> l(len), r(len);
    for(ll lev = MAXLOG - 1; lev >= 0; lev--) {
      mat[lev] = SuccinctIndexableDictionary(len + 1);
      ll left = 0, right = 0;
      for(ll i = 0; i < len; i++) {
        if(((v[i] >> lev) & 1)) {
          mat[lev].set(i);
          r[right++] = v[i];
        }
        else { l[left++] = v[i]; }
      }
      mid[lev] = left;
      mat[lev].build();
      v.swap(l);
      for(ll i = 0; i < right; i++) { v[left + i] = r[i]; }
    }
  }
  pair<ll, ll> succ(bool f, ll l, ll r, ll lev) { return {mat[lev].rank(f, l) + mid[lev] * f, mat[lev].rank(f, r) + mid[lev] * f}; }
  T access(ll k) {
    T ret = 0;
    for(ll lev = MAXLOG - 1; lev >= 0; lev--) {
      bool f = mat[lev][k];
      if(f) { ret |= T(1) << lev; }
      k = mat[lev].rank(f, k) + mid[lev] * f;
    }
    return ret;
  }
  T operator[](const ll &k) { return access(k); }
  ll rank(const T &x, ll r) {
    ll l = 0;
    for(ll lev = MAXLOG - 1; lev >= 0; lev--) { tie(l, r) = succ((x >> lev) & 1, l, r, lev); }
    return r - l;
  }
  T kth_smallest(ll l, ll r, ll k) {
    assert(0 <= k && k < r - l);
    T ret = 0;
    for(ll lev = MAXLOG - 1; lev >= 0; lev--) {
      ll cnt = mat[lev].rank(false, r) - mat[lev].rank(false, l);
      bool f = cnt <= k;
      if(f) {
        ret |= T(1) << lev;
        k -= cnt;
      }
      tie(l, r) = succ(f, l, r, lev);
    }
    return ret;
  }
  T kth_largest(ll l, ll r, ll k) { return kth_smallest(l, r, r - l - k - 1); }
  ll range_freq(ll l, ll r, T upper) {
    ll ret = 0;
    for(ll lev = MAXLOG - 1; lev >= 0; lev--) {
      bool f = ((upper >> lev) & 1);
      if(f) { ret += mat[lev].rank(false, r) - mat[lev].rank(false, l); }
      tie(l, r) = succ(f, l, r, lev);
    }
    return ret;
  }
  ll range_freq(ll l, ll r, T lower, T upper) { return range_freq(l, r, upper) - range_freq(l, r, lower); }
  T prev_value(ll l, ll r, T upper) {
    ll cnt = range_freq(l, r, upper);
    return cnt == 0 ? T(-1) : kth_smallest(l, r, cnt - 1);
  }
  T next_value(ll l, ll r, T lower) {
    ll cnt = range_freq(l, r, lower);
    return cnt == r - l ? T(-1) : kth_smallest(l, r, cnt);
  }
};

template<typename T = ll, ll MAXLOG = 20> struct CompressedWaveletMatrix {
  WaveletMatrix<ll, MAXLOG> mat;
  vector<T> ys;
  CompressedWaveletMatrix() {}
  CompressedWaveletMatrix(const vector<T> &v): ys(v) {
    ranges::sort(ys);
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    vector<ll> t(v.size());
    for(ll i = 0; i < (ll)v.size(); i++) { t[i] = get(v[i]); }
    mat = WaveletMatrix<ll, MAXLOG>(t);
  }
  inline ll get(const T &x) { return ranges::lower_bound(ys, x) - ys.begin(); }
  T access(ll k) { return ys[mat.access(k)]; }
  T operator[](const ll &k) { return access(k); }
  ll rank(const T &x, ll r) {
    auto pos = get(x);
    if(pos == (ll)ys.size() || ys[pos] != x) { return 0; }
    return mat.rank(pos, r);
  }
  ll count(ll l, ll r, T x) {
    if(l >= r) { return 0; }
    return rank(x, r) - rank(x, l);
  }
  T kth_smallest(ll l, ll r, ll k) { return ys[mat.kth_smallest(l, r, k)]; }
  T kth_largest(ll l, ll r, ll k) { return ys[mat.kth_largest(l, r, k)]; }
  ll range_freq(ll l, ll r, T upper) {
    if(l >= r) { return 0; }
    return mat.range_freq(l, r, get(upper));
  }
  ll range_freq(ll l, ll r, T lower, T upper) {
    if(l >= r || lower >= upper) { return 0; }
    return mat.range_freq(l, r, get(lower), get(upper));
  }
  T prev_value(ll l, ll r, T upper) {
    auto ret = mat.prev_value(l, r, get(upper));
    return ret == -1 ? T(-1) : ys[ret];
  }
  T next_value(ll l, ll r, T lower) {
    auto ret = mat.next_value(l, r, get(lower));
    return ret == -1 ? T(-1) : ys[ret];
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N;
  cin >> N;

  vector<ll> A(N);
  CompressedBIT2D<ll, mint> S;
  for(ll i = 0; i < N; i++) {
    cin >> A[i];
    S.use(i, A[i]);
  }

  CompressedWaveletMatrix<> C(A);
  S.build();
  for(ll i = 0; i < N; i++) { S.add(i, A[i], A[i]); }

  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.range_freq(0, i, A[i] + 1, 1e18), rc = C.range_freq(i + 1, N, 0, A[i]);
    ans += ls * rc + rs * lc + lc * rc * A[i];
  }

  cout << ans.val() << "\n";
}
0