結果

問題 No.1300 Sum of Inversions
ユーザー kcz146kcz146
提出日時 2020-11-27 22:02:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 366 ms / 2,000 ms
コード長 2,867 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 212,800 KB
実行使用メモリ 22,704 KB
最終ジャッジ日時 2023-10-01 05:53:59
合計ジャッジ時間 13,207 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 290 ms
21,840 KB
testcase_04 AC 270 ms
21,824 KB
testcase_05 AC 225 ms
13,068 KB
testcase_06 AC 328 ms
22,176 KB
testcase_07 AC 314 ms
21,828 KB
testcase_08 AC 358 ms
22,172 KB
testcase_09 AC 338 ms
22,108 KB
testcase_10 AC 181 ms
12,804 KB
testcase_11 AC 176 ms
12,884 KB
testcase_12 AC 268 ms
21,844 KB
testcase_13 AC 269 ms
21,644 KB
testcase_14 AC 366 ms
22,440 KB
testcase_15 AC 340 ms
22,172 KB
testcase_16 AC 289 ms
21,784 KB
testcase_17 AC 166 ms
12,892 KB
testcase_18 AC 202 ms
12,992 KB
testcase_19 AC 235 ms
21,576 KB
testcase_20 AC 238 ms
21,596 KB
testcase_21 AC 231 ms
21,580 KB
testcase_22 AC 217 ms
13,208 KB
testcase_23 AC 306 ms
22,176 KB
testcase_24 AC 230 ms
13,140 KB
testcase_25 AC 189 ms
12,896 KB
testcase_26 AC 188 ms
12,948 KB
testcase_27 AC 203 ms
13,068 KB
testcase_28 AC 359 ms
22,444 KB
testcase_29 AC 236 ms
21,652 KB
testcase_30 AC 336 ms
22,204 KB
testcase_31 AC 214 ms
13,300 KB
testcase_32 AC 221 ms
13,076 KB
testcase_33 AC 253 ms
22,632 KB
testcase_34 AC 267 ms
22,388 KB
testcase_35 AC 224 ms
22,396 KB
testcase_36 AC 229 ms
22,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// UTF−8

#include<bits/stdc++.h>
/* #include<atcoder/all> */
/* using namespace atcoder; */

using namespace std;
using ll = long long int;
using lc = complex<ll>;

template<class T>bool chmax(T &a, const T &b) { return (a<b ? (a=b,1) : 0); }
template<class T>bool chmin(T &a, const T &b) { return (a>b ? (a=b,1) : 0); }

template <typename T>
struct SegmentTree {
    using F = function<T(T, T)>;

    const T e;
    const F f;
    size_t sz;
    vector<T> tree;

    SegmentTree(size_t n, const F &f, const T &e = 0) : f(f), e(e) {
        sz = 1;
        while(sz < n) sz <<= 1;
        tree.assign(2*sz, e);
    }

    void set(typename vector<T>::iterator begin, typename vector<T>::iterator end) {
        copy(begin, end, tree.begin() + sz);
        for(size_t k=sz-1; k>0; k--)
            tree[k] = f(tree[2*k+0], tree[2*k+1]);
    }

    void update(size_t k, const T &x) {
        k += sz;
        tree[k] = x;
        while(k >>= 1)
            tree[k] = f(tree[2*k+0], tree[2*k+1]);
    }

    T query(size_t a, size_t b) const {
        b = min(b, sz);
        a = max((size_t)0, a);
        T l = e, r = e;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) l = f(l, tree[a++]);
            if(b & 1) r = f(tree[--b], r);
        }
        return f(l, r);
    }

    T operator[](const size_t k) const {
        return tree[sz + k];
    }
};


int main(void) {
    constexpr ll MOD = 0 ? 1e9+7 : 998244353;
    constexpr double PI = acos(-1);
    constexpr double eps = 1e-10;
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    if(1) {
        ll n;
        cin >> n;
        vector<ll> a(n);
        for(auto &e: a) cin >> e;
        vector<ll> b = a;
        sort(begin(b), end(b));
        b.erase(unique(begin(b),end(b)), end(b));

        SegmentTree<pair<ll,ll>> st1(n+10, [&](auto a, auto b) -> pair<ll,ll>{
                auto [x, m] = a;
                auto [y, n] = b;
                return {(x+y)%MOD, (m+n)%MOD};
                }, {0ll, 0ll});
        SegmentTree<pair<ll,ll>> st2(n+10, [&](auto a, auto b) -> pair<ll,ll>{
                auto [x, m] = a;
                auto [y, n] = b;
                return {(x+y)%MOD, (m+n)%MOD};
                }, {0ll, 0ll});

        ll r = 0;
        for(ll i=0; i<n; i++) {
            ll j = lower_bound(begin(b), end(b), a[i]) - begin(b);
            auto [x, y] = st1.query(j+1, n+5);
            auto [z, w] = st2.query(j+1, n+5);
            (r += (z + ((w*a[i])%MOD)) % MOD) %= MOD;
            st2.update(j, {(get<0>(st2[j]) + x+((a[i]*y) % MOD))%MOD, (get<1>(st2[j]) + y) %MOD});
            st1.update(j, {(get<0>(st1[j]) + a[i])%MOD, (get<1>(st1[j]) + 1) %MOD});
            // cout << x << ' ' << y << ' ' << z << ' ' << w << ' ' << r << endl;
        }
        cout << r << endl;
    }

    return 0;
}
0