結果

問題 No.1300 Sum of Inversions
ユーザー muminmumin
提出日時 2020-11-27 23:31:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 5,710 bytes
コンパイル時間 2,142 ms
コンパイル使用メモリ 177,856 KB
実行使用メモリ 20,064 KB
最終ジャッジ日時 2023-10-10 21:47:36
合計ジャッジ時間 9,800 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 190 ms
19,016 KB
testcase_04 AC 175 ms
18,876 KB
testcase_05 AC 137 ms
12,244 KB
testcase_06 AC 206 ms
19,300 KB
testcase_07 AC 194 ms
19,128 KB
testcase_08 AC 219 ms
19,872 KB
testcase_09 AC 215 ms
19,616 KB
testcase_10 AC 112 ms
11,812 KB
testcase_11 AC 112 ms
11,716 KB
testcase_12 AC 174 ms
19,016 KB
testcase_13 AC 175 ms
18,964 KB
testcase_14 AC 239 ms
19,876 KB
testcase_15 AC 220 ms
19,732 KB
testcase_16 AC 188 ms
19,000 KB
testcase_17 AC 110 ms
11,756 KB
testcase_18 AC 127 ms
11,900 KB
testcase_19 AC 160 ms
18,408 KB
testcase_20 AC 163 ms
18,456 KB
testcase_21 AC 160 ms
18,528 KB
testcase_22 AC 138 ms
12,016 KB
testcase_23 AC 205 ms
19,336 KB
testcase_24 AC 143 ms
12,080 KB
testcase_25 AC 124 ms
11,812 KB
testcase_26 AC 123 ms
11,900 KB
testcase_27 AC 138 ms
11,956 KB
testcase_28 AC 231 ms
19,776 KB
testcase_29 AC 161 ms
18,380 KB
testcase_30 AC 224 ms
19,616 KB
testcase_31 AC 143 ms
12,076 KB
testcase_32 AC 147 ms
12,220 KB
testcase_33 AC 106 ms
19,284 KB
testcase_34 AC 118 ms
19,416 KB
testcase_35 AC 154 ms
20,064 KB
testcase_36 AC 158 ms
20,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

const 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;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        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;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

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

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(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 (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(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;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

using S1 = mint;

S1 op1(S1 a, S1 b){
    return a+b;
}

S1 e1(){
    return 0;
}

using S2 = int;

S2 op2(S2 a, S2 b){
    return a+b;
}

S2 e2(){
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n), wa(n);
    rep(i,n){
        cin >> a[i];
        wa[i] = a[i];
    }
    sort(wa.begin(), wa.end());
    vector<int> va;
    rep(i,n){
        if(i == 0 || wa[i] != wa[i-1]) va.push_back(wa[i]);
    }
    vector<int> b(n);
    rep(i,n){
        b[i] = lower_bound(va.begin(), va.end(), a[i]) - va.begin();
    }

    segtree<S1, op1, e1> seg1(n), seg2(n);
    segtree<S2, op2, e2> seg3(n), seg4(n);
    rep(i,n){
        int v = seg4.get(b[i]);
        seg4.set(b[i], v+1);
        mint t = seg2.get(b[i]);
        t += a[i];
        seg2.set(b[i], t);
    }
    mint ans = 0;
    rep(i,n){
        if(i > 0){
            int id = b[i-1];
            int v = seg3.get(id);
            seg3.set(id, v+1);
            mint t = seg1.get(id);
            t += a[i-1];
            seg1.set(id, t);
        }
        int id = b[i];
        int v = seg4.get(id);
        seg4.set(id, v-1);
        mint t = seg2.get(id);
        t -= a[i];
        seg2.set(id, t);

        mint tmp = a[i];

        int n1 = seg4.prod(0, b[i]);
        int n2 = seg3.prod(b[i]+1, n);
        mint t1 = seg2.prod(0, b[i]);
        mint t2 = seg1.prod(b[i]+1, n);
        tmp *= n1;
        tmp *= n2;
        t1 *= n2;
        t2 *= n1;
        tmp += t1;
        tmp += t2;
        ans += tmp;
        //cout << n1 << ' ' << n2 << ' ' << t1 << ' ' << t2 << ' ' << tmp << endl;
    }
    cout << ans << endl;
}
0