結果

問題 No.1300 Sum of Inversions
ユーザー risujirohrisujiroh
提出日時 2020-11-27 21:31:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 5,513 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 213,460 KB
実行使用メモリ 7,960 KB
最終ジャッジ日時 2023-10-01 04:54:26
合計ジャッジ時間 11,476 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 243 ms
6,720 KB
testcase_04 AC 239 ms
6,424 KB
testcase_05 AC 193 ms
5,888 KB
testcase_06 AC 280 ms
6,904 KB
testcase_07 AC 263 ms
7,100 KB
testcase_08 AC 296 ms
7,276 KB
testcase_09 AC 291 ms
7,272 KB
testcase_10 AC 156 ms
5,396 KB
testcase_11 AC 157 ms
5,352 KB
testcase_12 AC 240 ms
6,452 KB
testcase_13 AC 232 ms
6,416 KB
testcase_14 AC 319 ms
7,716 KB
testcase_15 AC 290 ms
7,276 KB
testcase_16 AC 250 ms
6,672 KB
testcase_17 AC 151 ms
5,424 KB
testcase_18 AC 176 ms
5,656 KB
testcase_19 AC 206 ms
6,268 KB
testcase_20 AC 214 ms
6,132 KB
testcase_21 AC 213 ms
6,216 KB
testcase_22 AC 192 ms
5,952 KB
testcase_23 AC 272 ms
6,940 KB
testcase_24 AC 198 ms
5,924 KB
testcase_25 AC 166 ms
5,660 KB
testcase_26 AC 165 ms
5,676 KB
testcase_27 AC 185 ms
5,952 KB
testcase_28 AC 300 ms
7,484 KB
testcase_29 AC 210 ms
6,172 KB
testcase_30 AC 290 ms
7,276 KB
testcase_31 AC 192 ms
5,948 KB
testcase_32 AC 200 ms
6,124 KB
testcase_33 AC 85 ms
7,804 KB
testcase_34 AC 97 ms
7,960 KB
testcase_35 AC 175 ms
7,748 KB
testcase_36 AC 179 ms
7,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template <uint32_t Modulus>
class ModularInt {
  using M = ModularInt;

 public:
  static_assert(int(Modulus) >= 1, "Modulus must be in the range [1, 2^31)");
  static constexpr int modulus() { return Modulus; }
  static M raw(uint32_t v) { return *reinterpret_cast<M*>(&v); }

  ModularInt() : v_(0) {}
  ModularInt(int64_t v) : v_((v %= Modulus) < 0 ? v + Modulus : v) {}

  template <class T>
  explicit operator T() const {
    return v_;
  }
  M& operator++() { return v_ = ++v_ == Modulus ? 0 : v_, *this; }
  M& operator--() { return --(v_ ? v_ : v_ = Modulus), *this; }
  M operator+() const { return *this; }
  M operator-() const { return raw(v_ ? Modulus - v_ : 0); }
  M& operator*=(M o) { return v_ = uint64_t(v_) * o.v_ % Modulus, *this; }
  M& operator/=(M o) {
    auto [inv, gcd] = extgcd(o.v_, Modulus);
    assert(gcd == 1);
    return *this *= inv;
  }
  M& operator+=(M o) {
    return v_ = int(v_ += o.v_ - Modulus) < 0 ? v_ + Modulus : v_, *this;
  }
  M& operator-=(M o) {
    return v_ = int(v_ -= o.v_) < 0 ? v_ + Modulus : v_, *this;
  }

  friend M operator++(M& a, int) { return std::exchange(a, ++M(a)); }
  friend M operator--(M& a, int) { return std::exchange(a, --M(a)); }
  friend M operator*(M a, M b) { return a *= b; }
  friend M operator/(M a, M b) { return a /= b; }
  friend M operator+(M a, M b) { return a += b; }
  friend M operator-(M a, M b) { return a -= b; }
  friend std::istream& operator>>(std::istream& is, M& x) {
    int64_t v;
    return is >> v, x = v, is;
  }
  friend std::ostream& operator<<(std::ostream& os, M x) { return os << x.v_; }
  friend bool operator==(M a, M b) { return a.v_ == b.v_; }
  friend bool operator!=(M a, M b) { return a.v_ != b.v_; }

 private:
  static std::array<int, 2> extgcd(int a, int b) {
    std::array x{1, 0};
    while (b) std::swap(x[0] -= a / b * x[1], x[1]), std::swap(a %= b, b);
    return {x[0], a};
  }

  uint32_t v_;
};

template <class T>
class Fenwick {
 public:
  Fenwick() {}
  template <class Generator>
  Fenwick(int n, Generator gen) : tree(n) {
    for (int i = 0; i < n; ++i) tree[i] = gen();
    for (int i = 0; i < n; ++i)
      if (int j = i | (i + 1); j < n) tree[j] += tree[i];
  }

  int size() const { return std::size(tree); }
  void add(int i, const T& a) {
    assert(0 <= i), assert(i < size());
    for (; i < size(); i |= i + 1) tree[i] += a;
  }
  T sum(int i) const {
    assert(0 <= i), assert(i <= size());
    T res{};
    for (; i; i &= i - 1) res += tree[i - 1];
    return res;
  }
  T sum(int l, int r) const {
    assert(0 <= l), assert(l <= r), assert(r <= size());
    return sum(r) - sum(l);
  }
  int kth(T k) const {
    static_assert(std::is_integral_v<T> and not std::is_same_v<T, bool>);
    assert(k >= 0);
    int i = 0;
    for (int w = 1 << std::__lg(size()); w; w >>= 1)
      if (i + w <= size() and tree[i + w - 1] <= k) k -= tree[(i += w) - 1];
    return i;
  }

 private:
  std::vector<T> tree;
};

#pragma region my_template

struct Rep {
  struct I {
    int i;
    void operator++() { ++i; }
    int operator*() const { return i; }
    bool operator!=(I o) const { return i < *o; }
  };
  const int l_, r_;
  Rep(int l, int r) : l_(l), r_(r) {}
  Rep(int n) : Rep(0, n) {}
  I begin() const { return {l_}; }
  I end() const { return {r_}; }
};
struct Per {
  struct I {
    int i;
    void operator++() { --i; }
    int operator*() const { return i; }
    bool operator!=(I o) const { return i > *o; }
  };
  const int l_, r_;
  Per(int l, int r) : l_(l), r_(r) {}
  Per(int n) : Per(0, n) {}
  I begin() const { return {r_ - 1}; }
  I end() const { return {l_ - 1}; }
};

template <class F>
struct Fix : private F {
  Fix(F f) : F(f) {}
  template <class... Args>
  decltype(auto) operator()(Args&&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class T = int>
T scan() {
  T res;
  std::cin >> res;
  return res;
}

template <class T, class U = T>
bool chmin(T& a, U&& b) {
  return b < a ? a = std::forward<U>(b), true : false;
}
template <class T, class U = T>
bool chmax(T& a, U&& b) {
  return a < b ? a = std::forward<U>(b), true : false;
}

#ifndef LOCAL
#define DUMP(...) void(0)
template <int OnlineJudge, int Local>
constexpr int OjLocal = OnlineJudge;
#endif

using namespace std;

#define ALL(c) begin(c), end(c)

#pragma endregion

using Mint = ModularInt<998244353>;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n = scan();
  vector<int> a(n);
  generate(ALL(a), scan<>);
  auto v = a;
  sort(ALL(v));
  v.erase(unique(ALL(v)), end(v));
  auto vi = [&](int e) { return lower_bound(ALL(v), e) - begin(v); };

  Mint ans;

  Fenwick<int> fi(n, [] { return 0; });
  Fenwick<int> gi(n, [] { return 0; });
  Fenwick<Mint> fm(n, [] { return 0; });
  Fenwick<Mint> gm(n, [] { return 0; });
  for (int i : Rep(n)) {
    gi.add(vi(a[i]), +1);
    gm.add(vi(a[i]), +a[i]);
  }

  for (int i : Rep(n)) {
    gi.add(vi(a[i]), -1);
    gm.add(vi(a[i]), -a[i]);
    ans += fm.sum(vi(a[i]) + 1, n) * gi.sum(vi(a[i]));
    ans += gm.sum(vi(a[i])) * fi.sum(vi(a[i]) + 1, n);
    ans += Mint(a[i]) * fi.sum(vi(a[i]) + 1, n) * gi.sum(vi(a[i]));
    fi.add(vi(a[i]), +1);
    fm.add(vi(a[i]), +a[i]);
  }

  cout << ans << '\n';
}
0