結果

問題 No.1300 Sum of Inversions
ユーザー kcz146kcz146
提出日時 2020-11-27 22:00:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,841 bytes
コンパイル時間 3,696 ms
コンパイル使用メモリ 213,388 KB
実行使用メモリ 22,756 KB
最終ジャッジ日時 2023-10-01 05:45:04
合計ジャッジ時間 12,049 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 263 ms
21,944 KB
testcase_04 AC 236 ms
22,008 KB
testcase_05 AC 197 ms
13,176 KB
testcase_06 AC 285 ms
22,172 KB
testcase_07 AC 272 ms
21,824 KB
testcase_08 AC 305 ms
22,080 KB
testcase_09 AC 296 ms
22,356 KB
testcase_10 AC 158 ms
12,868 KB
testcase_11 AC 157 ms
12,912 KB
testcase_12 AC 237 ms
21,848 KB
testcase_13 AC 246 ms
21,708 KB
testcase_14 AC 328 ms
22,372 KB
testcase_15 AC 295 ms
22,392 KB
testcase_16 AC 253 ms
21,968 KB
testcase_17 AC 147 ms
12,872 KB
testcase_18 AC 186 ms
12,996 KB
testcase_19 AC 215 ms
21,708 KB
testcase_20 AC 217 ms
21,704 KB
testcase_21 AC 211 ms
21,712 KB
testcase_22 AC 199 ms
13,140 KB
testcase_23 AC 277 ms
22,216 KB
testcase_24 AC 204 ms
13,124 KB
testcase_25 AC 173 ms
12,864 KB
testcase_26 AC 168 ms
12,976 KB
testcase_27 AC 178 ms
13,252 KB
testcase_28 AC 314 ms
22,420 KB
testcase_29 AC 211 ms
21,704 KB
testcase_30 AC 290 ms
22,356 KB
testcase_31 AC 190 ms
13,240 KB
testcase_32 AC 202 ms
13,132 KB
testcase_33 AC 233 ms
22,756 KB
testcase_34 AC 245 ms
22,732 KB
testcase_35 AC 205 ms
22,356 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));
        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};
                }, {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};
                }, {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});
            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