結果

問題 No.1300 Sum of Inversions
ユーザー saxofone111saxofone111
提出日時 2021-03-12 20:40:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,791 bytes
コンパイル時間 2,640 ms
コンパイル使用メモリ 217,624 KB
実行使用メモリ 36,396 KB
最終ジャッジ日時 2024-10-14 10:42:44
合計ジャッジ時間 13,295 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 288 ms
34,816 KB
testcase_04 AC 280 ms
34,656 KB
testcase_05 AC 214 ms
20,596 KB
testcase_06 AC 321 ms
35,348 KB
testcase_07 AC 310 ms
35,148 KB
testcase_08 AC 337 ms
35,648 KB
testcase_09 AC 341 ms
35,728 KB
testcase_10 AC 168 ms
19,964 KB
testcase_11 AC 169 ms
20,072 KB
testcase_12 AC 280 ms
34,664 KB
testcase_13 AC 271 ms
34,640 KB
testcase_14 AC 374 ms
36,172 KB
testcase_15 AC 340 ms
35,632 KB
testcase_16 AC 298 ms
34,960 KB
testcase_17 AC 167 ms
19,780 KB
testcase_18 AC 188 ms
20,400 KB
testcase_19 AC 231 ms
34,148 KB
testcase_20 AC 248 ms
34,280 KB
testcase_21 AC 230 ms
34,360 KB
testcase_22 AC 207 ms
20,508 KB
testcase_23 AC 321 ms
35,344 KB
testcase_24 AC 216 ms
20,672 KB
testcase_25 AC 180 ms
20,144 KB
testcase_26 AC 176 ms
20,128 KB
testcase_27 AC 199 ms
20,488 KB
testcase_28 AC 343 ms
35,840 KB
testcase_29 AC 236 ms
34,252 KB
testcase_30 AC 337 ms
35,664 KB
testcase_31 AC 209 ms
20,652 KB
testcase_32 AC 218 ms
20,676 KB
testcase_33 AC 48 ms
19,816 KB
testcase_34 AC 101 ms
19,816 KB
testcase_35 AC 200 ms
36,324 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

#define MOD 998244353
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod){
  ll res=1;
  while(n>0){
    if(n&1) res=res*a%mod;
    a=a*a%mod;
    n>>=1;
  }
  return res;
}

ll modinv(ll a, ll mod){
  ll b=mod, u=1, v=0;
  while(b){
    ll t=a/b;
    a-=t*b; swap(a, b);
    u-=t*v; swap(u, v);
  }
  u%=mod;
  if(u<0)u+=mod;
  return u;
}

ll gcd(ll a, ll b){
  ll r = a%b;
  if(r==0) return b;
  else return gcd(b, a%b);
}

// @param b 1
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
std::pair<long long, long long> inv_gcd(long long a, long long b) {
  a = mod(a, b);
  if (a == 0) return {b, 0};

  // Contracts:
  // [1] s - m0 * a = 0 (mod b)
  // [2] t - m1 * a = 0 (mod b)
  // [3] s * |m1| + t * |m0| <= b
  long long s = b, t = a;
  long long m0 = 0, m1 = 1;

  while (t) {
    long long u = s / t;
    s -= t * u;
    m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

    // [3]:
    // (s - t * u) * |m1| + t * |m0 - m1 * u|
    // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
    // = s * |m1| + t * |m0| <= b

    auto tmp = s;
    s = t;
    t = tmp;
    tmp = m0;
    m0 = m1;
    m1 = tmp;
  }
  // by [3]: |m0| <= b/g
  // by g != b: |m0| < b/g
  if (m0 < 0) m0 += b / s;
  return {s, m0};
}

// (rem, mod)
std::pair<long long, long long> crt(const std::vector<long long>& r,
                                    const std::vector<long long>& m) {
  assert(r.size() == m.size());
  int n = int(r.size());
  // Contracts: 0 <= r0 < m0
  long long r0 = 0, m0 = 1;
  for (int i = 0; i < n; i++) {
    assert(1 <= m[i]);
    long long r1 = mod(r[i], m[i]), m1 = m[i];
    if (m0 < m1) {
      std::swap(r0, r1);
      std::swap(m0, m1);
    }
    if (m0 % m1 == 0) {
      if (r0 % m1 != r1) return {0, 0};
      continue;
    }
    // assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1)

    // (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1));
    // r2 % m0 = r0
    // r2 % m1 = r1
    // -> (r0 + x*m0) % m1 = r1
    // -> x*u0*g % (u1*g) = (r1 - r0) (u0*g = m0, u1*g = m1)
    // -> x = (r1 - r0) / g * inv(u0) (mod u1)

    // im = inv(u0) (mod u1) (0 <= im < u1)
    long long g, im;
    std::tie(g, im) = inv_gcd(m0, m1);

    long long u1 = (m1 / g);
    // |r1 - r0| < (m0 + m1) <= lcm(m0, m1)
    if ((r1 - r0) % g) return {0, 0};

    // u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1)
    long long x = (r1 - r0) / g % u1 * im % u1;

    // |r0| + |m0 * x|
    // < m0 + m0 * (u1 - 1)
    // = m0 + m0 * m1 / g - m0
    // = lcm(m0, m1)
    r0 += x * m0;
    m0 *= u1;  // -> lcm(m0, m1)
    if (r0 < 0) r0 += m0;
  }
  return {r0, m0};
}


bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
public:
  int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
  }
  lazy_segtree() : lazy_segtree(0) {}
  lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
  lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
    log = ceil_pow2(_n);
    size = 1 << log;
    d = std::vector<S>(2 * size, e());
    lz = std::vector<F>(size, id());
    for (int i = 0; i < _n; i++) d[size + i] = v[i];
    for (int i = size - 1; i >= 1; i--) {
      update(i);
    }
  }

  void set(int p, S x) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    d[p] = x;
    for (int i = 1; i <= log; i++) update(p >> i);
  }

  S get(int p) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    return d[p];
  }

  S prod(int l, int r) {
    assert(0 <= l && l <= r && r <= _n);
    if (l == r) return e();

    l += size;
    r += size;

    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l) push(l >> i);
      if (((r >> i) << i) != r) push(r >> i);
    }

    S sml = e(), smr = e();
    while (l < r) {
      if (l & 1) sml = op(sml, d[l++]);
      if (r & 1) smr = op(d[--r], smr);
      l >>= 1;
      r >>= 1;
    }

    return op(sml, smr);
  }

  S all_prod() { return d[1]; }

  void apply(int p, F f) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    d[p] = mapping(f, d[p]);
    for (int i = 1; i <= log; i++) update(p >> i);
  }

  void apply(int l, int r, F f) {
    assert(0 <= l && l <= r && r <= _n);
    if (l == r) return;

    l += size;
    r += size;

    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l) push(l >> i);
      if (((r >> i) << i) != r) push((r - 1) >> i);
    }

    {
      int l2 = l, r2 = r;
      while (l < r) {
        if (l & 1) all_apply(l++, f);
        if (r & 1) all_apply(--r, f);
        l >>= 1;
        r >>= 1;
      }
      l = l2;
      r = r2;
    }

    for (int i = 1; i <= log; i++) {
      if (((l >> i) << i) != l) update(l >> i);
      if (((r >> i) << i) != r) update((r - 1) >> i);
    }
  }

  template <bool (*g)(S)> int max_right(int l) {
    return max_right(l, [](S x) { return g(x); });
  }
  template <class G> int max_right(int l, G g) {
    assert(0 <= l && l <= _n);
    assert(g(e()));
    if (l == _n) return _n;
    l += size;
    for (int i = log; i >= 1; i--) push(l >> i);
    S sm = e();
    do {
      while (l % 2 == 0) l >>= 1;
      if (!g(op(sm, d[l]))) {
        while (l < size) {
          push(l);
          l = (2 * l);
          if (g(op(sm, d[l]))) {
            sm = op(sm, d[l]);
            l++;
          }
        }
        return l - size;
      }
      sm = op(sm, d[l]);
      l++;
    } while ((l & -l) != l);
    return _n;
  }

  template <bool (*g)(S)> int min_left(int r) {
    return min_left(r, [](S x) { return g(x); });
  }
  template <class G> int min_left(int r, G g) {
    assert(0 <= r && r <= _n);
    assert(g(e()));
    if (r == 0) return 0;
    r += size;
    for (int i = log; i >= 1; i--) push((r - 1) >> i);
    S sm = e();
    do {
      r--;
      while (r > 1 && (r % 2)) r >>= 1;
      if (!g(op(d[r], sm))) {
        while (r < size) {
          push(r);
          r = (2 * r + 1);
          if (g(op(d[r], sm))) {
            sm = op(d[r], sm);
            r--;
          }
        }
        return r + 1 - size;
      }
      sm = op(d[r], sm);
    } while ((r & -r) != r);
    return 0;
  }

private:
  int _n, size, log;
  std::vector<S> d;
  std::vector<F> lz;

  void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
  void all_apply(int k, F f) {
    d[k] = mapping(f, d[k]);
    if (k < size) lz[k] = composition(f, lz[k]);
  }
  void push(int k) {
    all_apply(2 * k, lz[k]);
    all_apply(2 * k + 1, lz[k]);
    lz[k] = id();
  }
};

struct S{
  ll x, d;
};

using F = ll;

S op(S a, S b){
  return {mod(a.x+b.x, MOD), a.d + b.d};
}

S mapping(F a, S b){
  return {mod(b.x + b.d*a, MOD), b.d};
}

F composition(F b, F a){
  return mod(a+b, MOD);
}

S e(){
  return S{0, 0};
}

F id(){
  return 0;
}

//usage lazy_segtree<S, op, e, F, mapping, composition, id>

struct segment_tree{
  ll N;
  llvec v;
  ll init=0;//initial value
  ll f(ll a, ll b){ //function
    return a+b;
  }
  segment_tree(ll n){
    N=1;
    while(N<n){
      N*=2;
    }
    v = llvec(2*N-1, init);
  }
  
  void set(ll i, ll val){
    i += N-1;
    v[i] = val;
    while(i>0){
      i = (i-1)/2;
      v[i] = f(v[i*2+1], v[i*2+2]);
    }
  }
  void add(ll i, ll val){
    i += N-1;
    v[i] += val;
    while(i>0){
      i = (i-1)/2;
      v[i] = f(v[i*2+1], v[i*2+2]);
    }
  }

  ll get(ll L, ll R){// L <= i < R
    L += N-1;
    R += N-1;
    ll vl = init;
    ll vr = init;
    while(L<R){
      if(L%2==0){
        vl = f(vl, v[L]);
        L++;
      }
      if(R%2==0){
        vr = f(vr, v[R-1]);
        R--;
      }
      R=(R-1)/2;
      L=(L-1)/2;
    }
    return f(vl, vr);
  }

  ll operator[](ll i){
    return v[i+N-1];
  }
  
};

/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N;
  cin >> N;
  llvec A(N);
  llvec v;
  rep(i, N){
    cin >> A[i];
    v.push_back(A[i]);
  }
  sort(ALL(v));
  v.erase(unique(ALL(v)), v.end());
  ll M = v.size();
  vector<S> a(N, {0, 1});
  lazy_segtree<S, op, e, F, mapping, composition, id> lz(a);
  segment_tree sg(M);
  segment_tree sg2(M);
  segment_tree sg3(M);
  segment_tree sg4(M);
  rep(i, N){
    ll ind = lower_bound(ALL(v), A[i]) - v.begin();
    sg2.add(ind, 1);
    sg4.add(ind, A[i]);
  }
  ll ans = 0;
  rep(i, N){
    ll ind = lower_bound(ALL(v), A[i]) - v.begin();
    ll ind2 = upper_bound(ALL(v), A[i]) - v.begin();
    ll L = sg2.get(0, ind);
    ll R = sg.get(ind2, M);
    ans = mod(ans + L * sg3.get(ind2, M), MOD);
    ans = mod(ans + R * sg4.get(0, ind), MOD);
    ans = mod(ans + mod(R * L, MOD)*A[i], MOD);
    sg.add(ind, 1);
    sg3.add(ind, A[i]);
    sg2.add(ind, -1);
    sg4.add(ind, -A[i]);
  }
  


  cout << ans << endl;
  
  return 0;
}
0