結果

問題 No.2250 Split Permutation
ユーザー k1suxuk1suxu
提出日時 2023-03-17 23:15:03
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 3,000 ms
コード長 12,119 bytes
コンパイル時間 3,315 ms
コンパイル使用メモリ 260,920 KB
実行使用メモリ 12,720 KB
最終ジャッジ日時 2023-10-18 16:11:05
合計ジャッジ時間 6,837 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 227 ms
12,720 KB
testcase_19 AC 199 ms
11,592 KB
testcase_20 AC 177 ms
10,820 KB
testcase_21 AC 105 ms
8,152 KB
testcase_22 AC 159 ms
10,076 KB
testcase_23 AC 27 ms
4,656 KB
testcase_24 AC 158 ms
10,084 KB
testcase_25 AC 232 ms
12,720 KB
testcase_26 AC 70 ms
6,648 KB
testcase_27 AC 19 ms
4,348 KB
testcase_28 AC 35 ms
5,056 KB
testcase_29 AC 232 ms
12,720 KB
testcase_30 AC 39 ms
5,104 KB
testcase_31 AC 10 ms
4,348 KB
testcase_32 AC 48 ms
5,792 KB
testcase_33 AC 118 ms
8,608 KB
testcase_34 AC 27 ms
4,664 KB
testcase_35 AC 76 ms
6,960 KB
testcase_36 AC 47 ms
5,504 KB
testcase_37 AC 232 ms
12,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

#define int long long

template<int MOD>
struct Modular_Int {
    int x;

    Modular_Int() = default;
    Modular_Int(int x_) : x(x_ >= 0? x_%MOD : (MOD-(-x_)%MOD)%MOD) {}

    int val() const {
        return (x%MOD+MOD)%MOD;
    }
    int get_mod() const {
        return MOD;
    }

    Modular_Int<MOD>& operator^=(int d)  {
        Modular_Int<MOD> ret(1);
        int nx = x;
        while(d) {
            if(d&1) ret *= nx;
            (nx *= nx) %= MOD;
            d >>= 1;
        }
        *this = ret;
        return *this;
    }
    Modular_Int<MOD> operator^(int d) const {return Modular_Int<MOD>(*this) ^= d;}
    Modular_Int<MOD> pow(int d) const {return Modular_Int<MOD>(*this) ^= d;}
    
    //use this basically
    Modular_Int<MOD> inv() const {
        return Modular_Int<MOD>(*this) ^ (MOD-2);
    }
    //only if the module number is not prime
    //Don't use. This is broken.
    // Modular_Int<MOD> inv() const {
    //     int a = (x%MOD+MOD)%MOD, b = MOD, u = 1, v = 0;
    //     while(b) {
    //         int t = a/b;
    //         a -= t*b, swap(a, b);
    //         u -= t*v, swap(u, v);
    //     }
    //     return Modular_Int<MOD>(u);
    // }

    Modular_Int<MOD>& operator+=(const Modular_Int<MOD> other) {
        if((x += other.x) >= MOD) x -= MOD;
        return *this;
    }
    Modular_Int<MOD>& operator-=(const Modular_Int<MOD> other) {
        if((x -= other.x) < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator*=(const Modular_Int<MOD> other) {
        int z = x;
        z *= other.x;
        z %= MOD;
        x = z;
        if(x < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator/=(const Modular_Int<MOD> other) {
        return *this = *this * other.inv();
    }
    Modular_Int<MOD>& operator++() {
        x++;
        if (x == MOD) x = 0;
        return *this;
    }
    Modular_Int<MOD>& operator--() {
        if (x == 0) x = MOD;
        x--;
        return *this;
    }
    
    Modular_Int<MOD> operator+(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) /= other;}
    
    Modular_Int<MOD>& operator+=(const int other) {Modular_Int<MOD> other_(other); *this += other_; return *this;}
    Modular_Int<MOD>& operator-=(const int other) {Modular_Int<MOD> other_(other); *this -= other_; return *this;}
    Modular_Int<MOD>& operator*=(const int other) {Modular_Int<MOD> other_(other); *this *= other_; return *this;}
    Modular_Int<MOD>& operator/=(const int other) {Modular_Int<MOD> other_(other); *this /= other_; return *this;}
    Modular_Int<MOD> operator+(const int other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const int other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const int other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const int other) const {return Modular_Int<MOD>(*this) /= other;}

    bool operator==(const Modular_Int<MOD> other) const {return (*this).val() == other.val();}
    bool operator!=(const Modular_Int<MOD> other) const {return (*this).val() != other.val();}
    bool operator==(const int other) const {return (*this).val() == other;}
    bool operator!=(const int other) const {return (*this).val() != other;}

    Modular_Int<MOD> operator-() const {return Modular_Int<MOD>(0LL)-Modular_Int<MOD>(*this);}

    //入れ子にしたい
    // friend constexpr istream& operator>>(istream& is, mint& x) noexcept {
    //     int X;
    //     is >> X;
    //     x = X;
    //     return is;
    // }
    // friend constexpr ostream& operator<<(ostream& os, mint& x) {
    //     os << x.val();
    //     return os;
    // }
};

// const int MOD_VAL = 1e9+7;
const int MOD_VAL = 998244353;
using mint = Modular_Int<MOD_VAL>;

istream& operator>>(istream& is, mint& x) {
    int X;
    is >> X;
    x = X;
    return is;
}
ostream& operator<<(ostream& os, mint& x) {
    os << x.val();
    return os;
}

// istream& operator<<(istream& is, mint &a) {
//     int x;
//     is >> x;
//     a = mint(x);
//     return is;
// }
// ostream& operator<<(ostream& os, mint a) {
//     os << a.val();
//     return os;
// }

// vector<mint> f = {1}, rf = {1};
// void init(int n) {
//     f.resize(n, 0);
//     rf.resize(n, 0);
//     f[0] = 1;
//     repi(i, 1, n) f[i] = (f[i - 1] * i);
//     repi(i, 0, n) rf[i] = f[i].inv();
// }
// mint P(int n, int k) {
//     assert(n>=k);
//     while(n > f.size()-1) {
//         f.push_back(f.back() * f.size());
//         rf.push_back(f.back().inv());
//     }
//     return f[n] * f[n-k];
// }
// mint C(int n, int k) {
//     assert(n>=k);
//     while(n > f.size()-1) {
//         f.push_back(f.back() * f.size());
//         rf.push_back(f.back().inv());
//     }
//     return f[n]*rf[n-k]*rf[k];
// }
// mint H(int n, int k) {
//     assert(n>=1);
//     return C(n+k-1, k);
// }
// mint Cat(int n) {
//     return C(2*n, n)-C(2*n, n-1);
// }

struct RSQ {
    int n;
    vector<int> dat;

    RSQ(int n_) : n(), dat(n_ * 4, 0) {
        int x = 1;
        while(n_ > x) {
            x *= 2;
        }
        n = x;
    }

    void update(int i, int x) {
        i += n - 1;
        dat[i] = x;
        while(i) {
            i = (i - 1) / 2;
            dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
        }
    }
    
    void add(int i, int x) {
        i += n - 1;
        dat[i] += x;
        while(i) {
            i = (i - 1) / 2;
            dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
        }
    }

    // set(i, a) = add(i, a - get(i))
    int get(int i) {
        return dat[i + n - 1];
    }

    // [a, b) 蟾ヲ蜊企幕蛹コ髢�
    int query(int a, int b) {return query_sub(a, b, 0, 0, n);}
    int query_sub(int a, int b, int k, int l, int r) {
        if(r <= a || b <= l) {
            return 0;
        }else if(a <= l && r <= b) {
            return dat[k];
        }else {
            int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return vl + vr;
        }
    }
};

template <typename T>
vector<T> compress(vector<T> &X) {
    vector<T> vals = X;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    for (int i = 0; i < (int)X.size(); i++) {
        X[i] = lower_bound(vals.begin(), vals.end(), X[i]) - vals.begin();
    }
    //vals[X[i]] = original X[i]
    return vals;
}

int the_number_of_inversions(vector<int> a) {
    int n = (int)a.size();
    compress(a);

    RSQ tree(n);
    int inversion = 0;

    FOR(n) {
        inversion += tree.query(a[i]+1, n+10);
        tree.add(a[i], 1);
    }

    return inversion;
}

namespace internal {
    // @param n `0 <= n`
    // @return minimum non-negative `x` s.t. `n <= 2**x`
    int ceil_pow2(int n) {
        int x = 0;
        while ((1U << x) < (unsigned int)(n)) x++;
        return x;
    }

    // @param n `1 <= n`
    // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
    constexpr int bsf_constexpr(unsigned int n) {
        int x = 0;
        while (!(n & (1 << x))) x++;
        return x;
    }

    // @param n `1 <= n`
    // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
    int bsf(unsigned int n) {
    #ifdef _MSC_VER
        unsigned long index;
        _BitScanForward(&index, n);
        return index;
    #else
        return __builtin_ctz(n);
    #endif
    }
}  // namespace internal

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
    explicit segtree(const std::vector<S>& v) : _n((int)v.size()) {
        log = internal::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) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) const {
        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() const { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) const {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
        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) const {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
        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 S = mint;
S op(S x, S y) {
    return x + y;
}
S e() {
    return 0;
}

void solve() {
    int n;
    cin >> n;
    vi p(n);
    FOR(n) {
        cin >> p[i];
        --p[i];
    }

    mint ans = mint(the_number_of_inversions(p)) * mint(2).pow(n-1);
    segtree<S, op, e> seg(n);

    rep(i, n) {
        mint sum = seg.prod(p[i], n);
        ans -= sum * mint(2).pow(n-1-i);
        seg.set(p[i], mint(2).pow(i));
    }

    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0