結果

問題 No.1300 Sum of Inversions
ユーザー iqueue02iqueue02
提出日時 2020-12-19 18:31:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 401 ms / 2,000 ms
コード長 3,413 bytes
コンパイル時間 2,633 ms
コンパイル使用メモリ 217,500 KB
実行使用メモリ 15,760 KB
最終ジャッジ日時 2023-10-21 09:26:15
合計ジャッジ時間 13,202 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 301 ms
14,380 KB
testcase_04 AC 297 ms
14,192 KB
testcase_05 AC 243 ms
10,300 KB
testcase_06 AC 356 ms
14,912 KB
testcase_07 AC 352 ms
14,672 KB
testcase_08 AC 377 ms
15,192 KB
testcase_09 AC 371 ms
15,172 KB
testcase_10 AC 194 ms
9,696 KB
testcase_11 AC 206 ms
9,696 KB
testcase_12 AC 304 ms
14,224 KB
testcase_13 AC 291 ms
14,116 KB
testcase_14 AC 401 ms
15,700 KB
testcase_15 AC 357 ms
15,172 KB
testcase_16 AC 314 ms
14,448 KB
testcase_17 AC 185 ms
9,696 KB
testcase_18 AC 231 ms
9,980 KB
testcase_19 AC 258 ms
13,688 KB
testcase_20 AC 261 ms
13,852 KB
testcase_21 AC 269 ms
13,852 KB
testcase_22 AC 232 ms
10,292 KB
testcase_23 AC 339 ms
14,912 KB
testcase_24 AC 237 ms
10,488 KB
testcase_25 AC 209 ms
9,960 KB
testcase_26 AC 205 ms
9,960 KB
testcase_27 AC 231 ms
10,224 KB
testcase_28 AC 381 ms
15,436 KB
testcase_29 AC 269 ms
13,852 KB
testcase_30 AC 359 ms
15,172 KB
testcase_31 AC 237 ms
10,488 KB
testcase_32 AC 248 ms
10,488 KB
testcase_33 AC 43 ms
9,696 KB
testcase_34 AC 96 ms
9,696 KB
testcase_35 AC 241 ms
15,760 KB
testcase_36 AC 254 ms
15,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
constexpr int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};

template <typename T> 
struct SegmentTree {
  
  int n;
  vector<T> node;
  T e;
 
  T f(T a, T b) {return (a + b);}
  void g(T &a, T b) {a += b;}
 
  SegmentTree (int sz, T init = 0) {
    n = 1; e = init; while (n < sz) n <<= 1;
    node.assign(2*n, init);
    for (int i = n - 2; i >= 0; i--) node[i] = f(node[2*i+1], node[2*i+2]);
  }
 
  void update(int k, T x) {
    k += (n - 1);
    g(node[k], x);
    while (k > 0) {
      k = (k - 1) >> 1;
      node[k] = f(node[2*k+1], node[2*k+2]);
    } 
  }
 
  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return e;
    if (a <= l && r <= b) return node[k];
    T vl = query(a, b, 2*k+1, l, (l+r)/2);
    T vr = query(a, b, 2*k+2, (l+r)/2, r);
    return f(vl, vr);
  }
 
  T getvalue(int k) {
    return query(k,k+1);
  }
  
  template <typename F> 
  int find (int a, int b, T x, F &ok, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (!ok(node[k]) || r <= a || b <= l) return b;
    if (k >= n - 1) return k - n + 1;
    int vl = find(a, b, x, ok, 2*k+1, l, (l + r)/2);
    if (vl != b) return vl;
    return find(a, b, x, ok, 2*k+2, (l + r)/2, r);
  }
 
};

int main() {  
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  auto b = a;
  sort(b.begin(), b.end());
  b.erase(unique(b.begin(), b.end()), b.end());
  int m = b.size();

  vector<mint> lsum(n, 0), rsum(n, 0);
  vector<int> l(n), r(n);

  {
    SegmentTree<int> cnt(m, 0);
    SegmentTree<mint> sum(m, 0);

    for ( int i = 0; i < n; i++) {
      int j = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
      sum.update(j, a[i]);
      cnt.update(j, 1);
      lsum[i] = sum.query(j + 1, m);
      l[i] = cnt.query(j + 1, m);
    }
  }

  {
    SegmentTree<int> cnt(m, 0);
    SegmentTree<mint> sum(m, 0);

    for (int i = n - 1; i >= 0; i--) {
      int j = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
      sum.update(j, a[i]);
      cnt.update(j, 1);
      rsum[i] = sum.query(0, j);
      r[i] = cnt.query(0, j);
    }
  }

  mint res = 0;

  for (int i = 0; i < n; i++) {
    if (!l[i] * r[i]) continue;
    int j = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    mint aa = a[i];
    res += lsum[i] * r[i] + rsum[i] * l[i] + aa * l[i] * r[i];
  }
  cout << res.x << endl;
  return 0;
}
0