結果

問題 No.1300 Sum of Inversions
ユーザー keijakkeijak
提出日時 2020-11-28 08:03:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 8,878 bytes
コンパイル時間 2,585 ms
コンパイル使用メモリ 213,256 KB
実行使用メモリ 24,084 KB
最終ジャッジ日時 2023-10-09 23:36:40
合計ジャッジ時間 8,850 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 164 ms
22,984 KB
testcase_04 AC 160 ms
22,880 KB
testcase_05 AC 128 ms
13,964 KB
testcase_06 AC 182 ms
23,408 KB
testcase_07 AC 169 ms
23,260 KB
testcase_08 AC 194 ms
23,708 KB
testcase_09 AC 194 ms
23,712 KB
testcase_10 AC 101 ms
13,628 KB
testcase_11 AC 103 ms
13,632 KB
testcase_12 AC 157 ms
22,956 KB
testcase_13 AC 153 ms
22,944 KB
testcase_14 AC 215 ms
23,964 KB
testcase_15 AC 194 ms
23,784 KB
testcase_16 AC 164 ms
23,028 KB
testcase_17 AC 99 ms
13,608 KB
testcase_18 AC 107 ms
13,896 KB
testcase_19 AC 136 ms
22,476 KB
testcase_20 AC 138 ms
22,512 KB
testcase_21 AC 137 ms
22,500 KB
testcase_22 AC 123 ms
13,984 KB
testcase_23 AC 177 ms
23,320 KB
testcase_24 AC 123 ms
14,364 KB
testcase_25 AC 107 ms
13,956 KB
testcase_26 AC 105 ms
13,852 KB
testcase_27 AC 118 ms
13,940 KB
testcase_28 AC 194 ms
23,712 KB
testcase_29 AC 136 ms
22,512 KB
testcase_30 AC 188 ms
23,660 KB
testcase_31 AC 122 ms
14,324 KB
testcase_32 AC 124 ms
14,520 KB
testcase_33 AC 23 ms
7,664 KB
testcase_34 AC 35 ms
7,692 KB
testcase_35 AC 101 ms
24,084 KB
testcase_36 AC 106 ms
23,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, REP_N_ = (n); i < REP_N_; ++i)
#define ALL(x) std::begin(x), std::end(x)
using i64 = long long;
using u64 = unsigned long long;

template <class T>
inline int ssize(const T &a) {
  return (int)std::size(a);
}
template <class T>
inline bool chmax(T &a, T b) {
  return a < b and ((a = std::move(b)), true);
}
template <class T>
inline bool chmin(T &a, T b) {
  return a > b and ((a = std::move(b)), true);
}

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
  for (auto &x : a) is >> x;
  return is;
}
template <typename Container>
std::ostream &pprint(const Container &a, std::string_view sep = " ",
                     std::string_view ends = "\n", std::ostream *os = nullptr) {
  if (os == nullptr) os = &std::cout;
  auto b = std::begin(a), e = std::end(a);
  for (auto it = std::begin(a); it != e; ++it) {
    if (it != b) *os << sep;
    *os << *it;
  }
  return *os << ends;
}
template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
                                  decltype(std::end(std::declval<T>()))>>
    : std::true_type {};

template <typename T, typename = std::enable_if_t<
                          is_iterable<T>::value &&
                          !std::is_same<T, std::string_view>::value &&
                          !std::is_same<T, std::string>::value>>
std::ostream &operator<<(std::ostream &os, const T &a) {
  return pprint(a, ", ", "", &(os << "{")) << "}";
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &a) {
  return os << "(" << a.first << ", " << a.second << ")";
}

#ifdef ENABLE_DEBUG
template <typename T>
void pdebug(const T &value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void pdebug(const T &value, const Ts &...args) {
  pdebug(value);
  std::cerr << ", ";
  pdebug(args...);
}
#define DEBUG(...)                                   \
  do {                                               \
    std::cerr << " \033[33m (L" << __LINE__ << ") "; \
    std::cerr << #__VA_ARGS__ << ":\033[0m ";        \
    pdebug(__VA_ARGS__);                             \
    std::cerr << std::endl;                          \
  } while (0)
#else
#define pdebug(...)
#define DEBUG(...)
#endif

using namespace std;

template <unsigned int M>
struct ModInt {
  constexpr ModInt(long long val = 0) : _v(0) {
    if (val < 0) {
      long long k = (abs(val) + M - 1) / M;
      val += k * M;
    }
    assert(val >= 0);
    _v = val % M;
  }

  static constexpr int mod() { return M; }
  static constexpr unsigned int umod() { return M; }
  inline unsigned int val() const { return _v; }

  ModInt &operator++() {
    _v++;
    if (_v == umod()) _v = 0;
    return *this;
  }
  ModInt &operator--() {
    if (_v == 0) _v = umod();
    _v--;
    return *this;
  }
  ModInt operator++(int) {
    auto result = *this;
    ++*this;
    return result;
  }
  ModInt operator--(int) {
    auto result = *this;
    --*this;
    return result;
  }

  constexpr ModInt operator-() const { return ModInt(-_v); }
  constexpr ModInt &operator+=(const ModInt &a) {
    if ((_v += a._v) >= M) _v -= M;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &a) {
    if ((_v += M - a._v) >= M) _v -= M;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &a) {
    _v = ((unsigned long long)(_v)*a._v) % M;
    return *this;
  }
  constexpr ModInt pow(unsigned long long t) const {
    ModInt base = *this;
    ModInt res = 1;
    while (t) {
      if (t & 1) res *= base;
      base *= base;
      t >>= 1;
    }
    return res;
  }

  constexpr ModInt inv() const {
    // Inverse by Extended Euclidean algorithm.
    // M doesn't need to be prime, but x and M must be coprime.
    assert(_v != 0);
    auto [g, x, y] = ext_gcd(_v, M);
    assert(g == 1LL);  // The GCD must be 1.
    return x;

    // Inverse by Fermat's little theorem.
    // M must be prime. It's often faster.
    //
    //     return pow(M - 2);
  }
  constexpr ModInt &operator/=(const ModInt &a) { return *this *= a.inv(); }

  friend constexpr ModInt operator+(const ModInt &a, const ModInt &b) {
    return ModInt(a) += b;
  }
  friend constexpr ModInt operator-(const ModInt &a, const ModInt &b) {
    return ModInt(a) -= b;
  }
  friend constexpr ModInt operator*(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= b;
  }
  friend constexpr ModInt operator/(const ModInt &a, const ModInt &b) {
    return ModInt(a) /= b;
  }
  friend constexpr bool operator==(const ModInt &a, const ModInt &b) {
    return a._v == b._v;
  }
  friend constexpr bool operator!=(const ModInt &a, const ModInt &b) {
    return a._v != b._v;
  }
  friend std::istream &operator>>(std::istream &is, ModInt &a) {
    return is >> a._v;
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) {
    return os << a._v;
  }

 private:
  // Extended Euclidean algorithm
  // Returns (gcd(a,b), x, y) where `a*x + b*y == gcd(a,b)`.
  static std::tuple<int, int, int> ext_gcd(int a, int b) {
    int ax = 1, ay = 0, bx = 0, by = 1;
    for (;;) {
      if (b == 0) break;
      auto d = std::div(a, b);
      a = b;
      b = d.rem;
      ax -= bx * d.quot;
      std::swap(ax, bx);
      ay -= by * d.quot;
      std::swap(ay, by);
    }
    return {a, ax, ay};
  }

  unsigned int _v;  // raw value
};
const unsigned int MOD = 998244353;
using Mint = ModInt<MOD>;

template <typename Monoid>
struct SegTree {
  using T = typename Monoid::T;

  inline int n() const { return n_; }
  inline int offset() const { return offset_; }

  explicit SegTree(int n) : n_(n) {
    offset_ = 1;
    while (offset_ < n_) offset_ <<= 1;
    data_.assign(2 * offset_, Monoid::id());
  }

  explicit SegTree(const std::vector<T> &leaves) : n_(leaves.size()) {
    offset_ = 1;
    while (offset_ < n_) offset_ <<= 1;
    data_.assign(2 * offset_, Monoid::id());
    for (int i = 0; i < n_; ++i) {
      data_[offset_ + i] = leaves[i];
    }
    for (int i = offset_ - 1; i > 0; --i) {
      data_[i] = Monoid::op(data_[i * 2], data_[i * 2 + 1]);
    }
  }

  // Sets i-th value (0-indexed) to x.
  void set(int i, const T &x) {
    int k = offset_ + i;
    data_[k] = x;
    // Update its ancestors.
    while (k > 1) {
      k >>= 1;
      data_[k] = Monoid::op(data_[k * 2], data_[k * 2 + 1]);
    }
  }

  // Queries by [l,r) range (0-indexed, half-open interval).
  T fold(int l, int r) const {
    l = std::max(l, 0) + offset_;
    r = std::min(r, offset_) + offset_;
    T vleft = Monoid::id(), vright = Monoid::id();
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vleft = Monoid::op(vleft, data_[l++]);
      if (r & 1) vright = Monoid::op(data_[--r], vright);
    }
    return Monoid::op(vleft, vright);
  }

  T fold_all() const { return data_[1]; }

  // Returns i-th value (0-indexed).
  T operator[](int i) const { return data_[offset_ + i]; }

  friend std::ostream &operator<<(std::ostream &os, const SegTree &st) {
    os << "[";
    for (int i = 0; i < st.n(); ++i) {
      if (i != 0) os << ", ";
      const auto &x = st[i];
      os << x;
    }
    return os << "]";
  }

 private:
  int n_;                // number of valid leaves.
  int offset_;           // where leaves start
  std::vector<T> data_;  // data size: 2*offset_
};

struct Sum {
  struct T {
    Mint sum;
    i64 count;
  };
  static T op(const T &x, const T &y) {
    return {x.sum + y.sum, x.count + y.count};
  }
  static constexpr T id() { return {0, 0}; }
};

template <typename T>
struct Compress {
  std::vector<T> vec;

  explicit Compress(std::vector<T> v) : vec(v) {
    std::sort(vec.begin(), vec.end());
    vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  }

  int size() const { return vec.size(); }

  int index(T x) const {
    return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
  }

  const T &value(int i) const { return vec[i]; }
};

Mint solve() {
  int N;
  cin >> N;
  vector<int> A(N);
  cin >> A;
  Compress<int> ca(A);
  SegTree<Sum> segl(ca.size()), segr(ca.size());

  vector<Sum::T> left(N);
  REP(i, N) {
    int j = ca.index(A[i]);
    auto r = segl.fold(j + 1, ca.size());
    left[i] = r;
    {
      auto qj = segl[j];
      qj.sum += A[i];
      qj.count++;
      segl.set(j, qj);
    }
  }
  Mint ans = 0;
  REP(i, N) {
    int ri = N - 1 - i;
    int j = ca.index(A[ri]);
    auto r = segr.fold(0, j);
    ans += left[ri].sum * r.count;
    ans += r.sum * left[ri].count;
    ans += Mint(A[ri]) * left[ri].count * r.count;
    {
      auto qj = segr[j];
      qj.sum += A[ri];
      qj.count++;
      segr.set(j, qj);
    }
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << solve() << endl;
}
0