結果

問題 No.1300 Sum of Inversions
ユーザー commycommy
提出日時 2020-11-28 16:31:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 392 ms / 2,000 ms
コード長 6,498 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 110,000 KB
実行使用メモリ 22,168 KB
最終ジャッジ日時 2024-09-12 23:12:26
合計ジャッジ時間 11,713 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 293 ms
21,568 KB
testcase_04 AC 279 ms
21,464 KB
testcase_05 AC 221 ms
12,980 KB
testcase_06 AC 329 ms
21,664 KB
testcase_07 AC 329 ms
21,708 KB
testcase_08 AC 364 ms
21,956 KB
testcase_09 AC 359 ms
21,832 KB
testcase_10 AC 176 ms
12,692 KB
testcase_11 AC 180 ms
12,824 KB
testcase_12 AC 287 ms
21,436 KB
testcase_13 AC 289 ms
21,356 KB
testcase_14 AC 392 ms
22,012 KB
testcase_15 AC 355 ms
22,008 KB
testcase_16 AC 290 ms
21,624 KB
testcase_17 AC 173 ms
12,660 KB
testcase_18 AC 208 ms
12,900 KB
testcase_19 AC 253 ms
21,380 KB
testcase_20 AC 258 ms
21,292 KB
testcase_21 AC 251 ms
21,344 KB
testcase_22 AC 228 ms
12,980 KB
testcase_23 AC 330 ms
21,736 KB
testcase_24 AC 228 ms
13,012 KB
testcase_25 AC 196 ms
12,872 KB
testcase_26 AC 192 ms
12,736 KB
testcase_27 AC 210 ms
13,056 KB
testcase_28 AC 369 ms
21,804 KB
testcase_29 AC 244 ms
21,240 KB
testcase_30 AC 355 ms
21,916 KB
testcase_31 AC 222 ms
12,860 KB
testcase_32 AC 229 ms
13,280 KB
testcase_33 AC 27 ms
6,944 KB
testcase_34 AC 41 ms
6,944 KB
testcase_35 AC 219 ms
21,956 KB
testcase_36 AC 225 ms
22,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

template<ll MOD = 1000000007>
class ModInt {
    ll n;
    ModInt constexpr inverse() const {
        return ModInt::pow(*this, MOD - 2);
    }

public:
    ModInt()
        : n(0) {}
    ModInt(ll _n)
        : n(((_n % MOD) + MOD) % MOD) {}
    ModInt operator+=(const ModInt &m) {
        n += m.n;
        if (n >= MOD) n -= MOD;
        return *this;
    }
    ModInt operator-=(const ModInt &m) {
        n -= m.n;
        if (n < 0) n += MOD;
        return *this;
    }
    ModInt operator*=(const ModInt &m) {
        n *= m.n;
        if (n >= MOD) n %= MOD;
        return *this;
    }
    ModInt operator/=(const ModInt &m) {
        (*this) *= m.inverse();
        return *this;
    }
    friend ModInt operator+(ModInt t, const ModInt &m) {
        return t += m;
    }
    friend ModInt operator-(ModInt t, const ModInt &m) {
        return t -= m;
    }
    friend ModInt operator*(ModInt t, const ModInt &m) {
        return t *= m;
    }
    friend ModInt operator/(ModInt t, const ModInt &m) {
        return t /= m;
    }
    ModInt operator=(const ll l) {
        n = l % MOD;
        if (n < 0) n += MOD;
        return *this;
    }
    friend ostream &operator<<(ostream &out, const ModInt &m) {
        out << m.n;
        return out;
    }
    friend istream &operator>>(istream &in, ModInt &m) {
        ll l;
        in >> l;
        m = l;
        return in;
    }
    static constexpr ModInt pow(const ModInt x, ll p) {
        ModInt<MOD> ans = 1;
        for (ModInt<MOD> m = x; p > 0; p /= 2, m *= m) {
            if (p % 2) ans *= m;
        }
        return ans;
    }
};
using mint = ModInt<998244353>;
mint operator"" _m(unsigned long long m) {
    return mint(m);
}

template<typename T, typename E>
void compress_insert(vector<T> &inserter, pair<vector<T> &, vector<E> &> pr) {
    for (T &val : pr.first) {
        inserter.push_back(val);
    }
}
template<typename T, typename Head, typename... Args>
void compress_insert(vector<T> &inserter, Head head, Args... args) {
    compress_insert(inserter, head);
    compress_insert(inserter, args...);
}
template<typename T, typename E>
void compress_update(vector<T> &inserter, pair<vector<T> &, vector<E> &> pr) {
    for (unsigned int i = 0; i < pr.first.size(); i++) {
        pr.second[i] = static_cast<int>(lower_bound(inserter.begin(), inserter.end(), pr.first[i]) - inserter.begin());
    }
}
template<typename T, typename Head, typename... Args>
void compress_update(vector<T> &inserter, Head head, Args... args) {
    compress_update(inserter, head);
    compress_update(inserter, args...);
}
template<typename T, typename... Args>
size_t compress(vector<T> &inserter, Args... args) {
    compress_insert(inserter, args...);
    sort(inserter.begin(), inserter.end());
    inserter.erase(unique(inserter.begin(), inserter.end()), inserter.end());
    compress_update(inserter, args...);
    return inserter.size();
}
template<typename T, typename E>
auto refer(vector<T> &a, vector<E> &b) {
    return make_pair(ref(a), ref(b));
}

#include <functional>
template<typename T>
class SegmentTreeOneAll {
    using Func = function<T(T, T)>;

public:
    vector<T> data;
    int n;
    T init;
    Func update_func;
    Func query_func;
    SegmentTreeOneAll(int _n, T _init, Func up, Func qu) {
        init = _init;
        update_func = up;
        query_func = qu;
        for (n = 1; n < _n; n *= 2)
            ;
        data.resize(2 * n - 1, init);
    }
    void update(int pos, T val) {
        pos += n - 1;
        data[pos] = update_func(data[pos], val);
        while (pos > 0) {
            pos = (pos - 1) / 2;
            data[pos] = query_func(data[2 * pos + 1], data[2 * pos + 2]);
        }
    }
    T query(int l, int r) {
        T resL = init, resR = init;
        for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) {
            if (!(l & 1)) {
                resL = query_func(resL, data[l]);
            }
            if (!(r & 1)) {
                resR = query_func(data[r - 1], resR);
            }
        }
        return query_func(resL, resR);
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin, n;
    vector<int> a(n);
    rep(i, 0, n) {
        cin, a[i];
    }
    vector<int> tmp, b(n);
    int sz = compress(tmp, refer(a, b));
    auto SUM1 = [](ll s, ll t) {
        return s + t;
    };
    auto SUM2 = [](mint s, mint t) {
        return s + t;
    };
    SegmentTreeOneAll<ll> seg1(sz, 0, SUM1, SUM1), seg2(sz, 0, SUM1, SUM1);
    SegmentTreeOneAll<mint> seg1m(sz, 0, SUM2, SUM2), seg2m(sz, 0, SUM2, SUM2);
    rep(i, 0, n) {
        int t = b[i];
        seg2.update(t, 1);
        seg2m.update(t, a[i]);
    }
    mint ans = 0;
    rep(i, 0, n) {
        int t = b[i];
        seg2.update(t, -1);
        seg2m.update(t, -a[i]);
        ll c1 = seg1.query(t + 1, sz);
        ll c2 = seg2.query(0, t);
        mint sum1 = seg1m.query(t + 1, sz);
        mint sum2 = seg2m.query(0, t);
        ans += 1_m * c1 * c2 * a[i];
        ans += 1_m * c1 * sum2;
        ans += 1_m * c2 * sum1;
        seg1.update(t, 1);
        seg1m.update(t, a[i]);
        dump(a[i], b[i], c1, c2, sum1, sum2, ans);
    }
    print(ans);
    return 0;
}
0