結果

問題 No.1300 Sum of Inversions
ユーザー momoharamomohara
提出日時 2020-11-27 22:34:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 5,990 bytes
コンパイル時間 2,518 ms
コンパイル使用メモリ 218,192 KB
実行使用メモリ 25,340 KB
最終ジャッジ日時 2023-10-09 20:37:42
合計ジャッジ時間 10,630 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 232 ms
22,168 KB
testcase_04 AC 205 ms
21,872 KB
testcase_05 AC 157 ms
15,968 KB
testcase_06 AC 256 ms
23,492 KB
testcase_07 AC 231 ms
23,028 KB
testcase_08 AC 278 ms
24,016 KB
testcase_09 AC 286 ms
23,920 KB
testcase_10 AC 126 ms
14,384 KB
testcase_11 AC 127 ms
14,288 KB
testcase_12 AC 237 ms
21,912 KB
testcase_13 AC 214 ms
21,652 KB
testcase_14 AC 330 ms
25,128 KB
testcase_15 AC 261 ms
23,688 KB
testcase_16 AC 243 ms
22,368 KB
testcase_17 AC 132 ms
14,024 KB
testcase_18 AC 170 ms
15,180 KB
testcase_19 AC 222 ms
20,588 KB
testcase_20 AC 181 ms
20,908 KB
testcase_21 AC 181 ms
20,780 KB
testcase_22 AC 167 ms
15,944 KB
testcase_23 AC 250 ms
23,548 KB
testcase_24 AC 176 ms
16,376 KB
testcase_25 AC 138 ms
14,916 KB
testcase_26 AC 138 ms
14,828 KB
testcase_27 AC 156 ms
15,708 KB
testcase_28 AC 280 ms
24,244 KB
testcase_29 AC 179 ms
20,792 KB
testcase_30 AC 278 ms
24,068 KB
testcase_31 AC 162 ms
16,000 KB
testcase_32 AC 170 ms
16,232 KB
testcase_33 AC 18 ms
4,676 KB
testcase_34 AC 31 ms
4,520 KB
testcase_35 AC 161 ms
25,236 KB
testcase_36 AC 160 ms
25,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
/*
#include <atcoder/all>
using namespace atcoder;
*/
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;

constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
//constexpr long long MOD = (ll)1e9 + 7;
constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;

using Array = vector<ll>;
using Matrix = vector<Array>;

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

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    ll to, cap, rev;
    Edge(ll _to, ll _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

template <typename T, typename F>
class SegmentTree {
  private:
    T e;
    F op;
    ll n;
    vector<T> dat;

  public:
    SegmentTree(F f, T _e, vector<T> v) : op(f), e(_e) {
        int _n = v.size();
        n = 1;
        while(n < _n)
            n *= 2;
        dat.resize(2 * n - 1, e);
        REP(i, _n)
        dat[n + i - 1] = v[i];
        for(int i = n - 2; i >= 0; i--)
            dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
    SegmentTree(F f, T _e, int _n) : op(f), e(_e) {
        n = 1;
        while(n < _n)
            n *= 2;
        dat.resize(2 * n - 1, e);
        for(int i = n - 2; i >= 0; i--)
            dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
    void update(int i, T x) {
        i += n - 1;
        dat[i] = x;
        while(i > 0) {
            i = (i - 1) / 2;
            dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }
    T get(int i) {
        i += n - 1;
        return dat[i];
    }
    T query(int l, int r) {
        //[l,r)でのクエリ処理
        T left = e, right = e;
        l += n - 1;
        r += n - 1;
        while(l < r) {
            if((l & 1) == 0)
                left = op(left, dat[l]);
            if((r & 1) == 0)
                right = op(dat[r - 1], right);
            l = l / 2;
            r = (r - 1) / 2;
        }
        return op(left, right);
    }
};

template <int mod>
struct ModInt {
    int x;

    ModInt() : x(0) {}

    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod)
            x -= mod;
        return *this;
    }

    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod)
            x -= mod;
        return *this;
    }

    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }

    ModInt operator-() const { return ModInt(-x); }

    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

    bool operator==(const ModInt &p) const { return x == p.x; }

    bool operator!=(const ModInt &p) const { return x != p.x; }

    ModInt inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    ModInt pow(int64_t n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1)
                ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a) {
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = ModInt<MOD>;

void solve() {
    ll n;
    cin >> n;
    vec<ll> a(n);
    map<ll, ll> mp;
    REP(i, n) {
        cin >> a[i];
        mp[a[i]]++;
    }
    mp[INF]++;

    int id = 0;
    for(auto &[a, b] : mp) {
        b = id++;
    }

    using pm = pair<mint, mint>;
    auto op = [](pm a, pm b) { return make_pair(a.first + b.first, a.second + b.second); }; //個数,合計
    SegmentTree<pm, decltype(op)> seg1(op, make_pair(mint(0), mint(0)), id), seg2(op, make_pair(mint(0), mint(0)), id);

    mint ans = 0;
    REP(i, n) {
        int v = mp[a[i]];
        seg1.update(v, op(seg1.get(v), make_pair(1, mint(a[i]))));
        {
            //ai>aj
            auto [num, sum] = seg1.query(v + 1, id);
            seg2.update(v, op(seg2.get(v), make_pair(num, sum + mint(a[i]) * num)));
        }
        {
            //aj>ak
            auto [num, sum] = seg2.query(v + 1, id);
            ans += sum + mint(a[i]) * num;
        }
    }
    cout << ans << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*
    ll t;
    cin >> t;
    REP(i, t - 1) {
        solve();
    }*/

    solve();

    return 0;
}
0