結果

問題 No.1300 Sum of Inversions
ユーザー kcz146kcz146
提出日時 2020-11-27 21:57:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,781 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 212,920 KB
実行使用メモリ 22,752 KB
最終ジャッジ日時 2023-10-01 05:38:48
合計ジャッジ時間 11,830 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 236 ms
22,040 KB
testcase_04 AC 232 ms
21,952 KB
testcase_05 AC 188 ms
13,140 KB
testcase_06 AC 272 ms
22,088 KB
testcase_07 AC 263 ms
22,016 KB
testcase_08 AC 294 ms
22,104 KB
testcase_09 AC 288 ms
22,112 KB
testcase_10 AC 150 ms
12,988 KB
testcase_11 AC 150 ms
12,956 KB
testcase_12 AC 249 ms
21,956 KB
testcase_13 AC 236 ms
21,692 KB
testcase_14 AC 313 ms
22,372 KB
testcase_15 AC 293 ms
22,364 KB
testcase_16 AC 244 ms
21,876 KB
testcase_17 AC 149 ms
13,016 KB
testcase_18 AC 178 ms
12,876 KB
testcase_19 AC 210 ms
21,560 KB
testcase_20 AC 231 ms
21,656 KB
testcase_21 AC 219 ms
21,608 KB
testcase_22 AC 193 ms
13,216 KB
testcase_23 AC 274 ms
22,388 KB
testcase_24 AC 200 ms
13,212 KB
testcase_25 AC 165 ms
12,904 KB
testcase_26 AC 162 ms
13,012 KB
testcase_27 AC 185 ms
13,132 KB
testcase_28 AC 306 ms
22,444 KB
testcase_29 AC 204 ms
21,656 KB
testcase_30 AC 297 ms
22,364 KB
testcase_31 AC 185 ms
13,132 KB
testcase_32 AC 198 ms
13,212 KB
testcase_33 AC 229 ms
22,684 KB
testcase_34 AC 242 ms
22,752 KB
testcase_35 AC 202 ms
22,752 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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));

        SegmentTree<pair<ll,ll>> st1(n, [&](auto a, auto b) -> pair<ll,ll>{
                auto [x, m] = a;
                auto [y, n] = b;
                return {(x+y)%MOD, m+n};
                }, {0ll, 0ll});
        SegmentTree<pair<ll,ll>> st2(n, [&](auto a, auto b) -> pair<ll,ll>{
                auto [x, m] = a;
                auto [y, n] = b;
                return {(x+y)%MOD, m+n};
                }, {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);
            auto [z, w] = st2.query(j+1, n);
            (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});
            st1.update(j, {(get<0>(st1[j]) + a[i])%MOD, get<1>(st1[j]) + 1});
            // cout << x << ' ' << y << ' ' << z << ' ' << w << ' ' << r << endl;
        }
        cout << r << endl;
    }

    return 0;
}
0