結果

問題 No.1300 Sum of Inversions
ユーザー kcz146kcz146
提出日時 2020-11-27 21:55:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,763 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 212,044 KB
実行使用メモリ 22,756 KB
最終ジャッジ日時 2023-10-01 05:31:33
合計ジャッジ時間 11,861 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 237 ms
21,912 KB
testcase_04 AC 236 ms
21,972 KB
testcase_05 AC 190 ms
13,252 KB
testcase_06 AC 275 ms
22,352 KB
testcase_07 AC 273 ms
21,964 KB
testcase_08 AC 300 ms
22,148 KB
testcase_09 AC 292 ms
22,112 KB
testcase_10 AC 155 ms
13,004 KB
testcase_11 AC 155 ms
12,956 KB
testcase_12 AC 255 ms
21,864 KB
testcase_13 AC 243 ms
21,712 KB
testcase_14 AC 321 ms
22,528 KB
testcase_15 AC 298 ms
22,256 KB
testcase_16 AC 251 ms
21,848 KB
testcase_17 AC 152 ms
13,008 KB
testcase_18 AC 184 ms
12,884 KB
testcase_19 AC 210 ms
21,700 KB
testcase_20 AC 226 ms
21,692 KB
testcase_21 AC 210 ms
21,708 KB
testcase_22 AC 197 ms
13,160 KB
testcase_23 AC 272 ms
22,120 KB
testcase_24 AC 199 ms
13,144 KB
testcase_25 AC 163 ms
12,988 KB
testcase_26 AC 159 ms
12,892 KB
testcase_27 AC 184 ms
13,176 KB
testcase_28 AC 311 ms
22,380 KB
testcase_29 AC 203 ms
21,596 KB
testcase_30 AC 295 ms
22,180 KB
testcase_31 AC 188 ms
13,284 KB
testcase_32 AC 201 ms
13,220 KB
testcase_33 AC 228 ms
22,744 KB
testcase_34 AC 241 ms
22,756 KB
testcase_35 AC 201 ms
22,708 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;
            st2.update(j, {(get<0>(st2[j]) + x+a[i]*y)%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