結果

問題 No.1300 Sum of Inversions
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-11-28 16:30:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 537 ms / 2,000 ms
コード長 6,288 bytes
コンパイル時間 1,983 ms
コンパイル使用メモリ 186,240 KB
実行使用メモリ 36,348 KB
最終ジャッジ日時 2023-10-10 00:39:39
合計ジャッジ時間 15,421 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 396 ms
30,356 KB
testcase_04 AC 388 ms
32,156 KB
testcase_05 AC 300 ms
21,912 KB
testcase_06 AC 455 ms
31,844 KB
testcase_07 AC 418 ms
33,556 KB
testcase_08 AC 478 ms
32,124 KB
testcase_09 AC 477 ms
35,136 KB
testcase_10 AC 235 ms
18,480 KB
testcase_11 AC 226 ms
18,532 KB
testcase_12 AC 373 ms
30,104 KB
testcase_13 AC 366 ms
29,832 KB
testcase_14 AC 537 ms
36,348 KB
testcase_15 AC 463 ms
32,044 KB
testcase_16 AC 395 ms
30,716 KB
testcase_17 AC 220 ms
19,756 KB
testcase_18 AC 267 ms
19,264 KB
testcase_19 AC 328 ms
30,824 KB
testcase_20 AC 339 ms
28,956 KB
testcase_21 AC 342 ms
29,124 KB
testcase_22 AC 293 ms
21,976 KB
testcase_23 AC 444 ms
34,400 KB
testcase_24 AC 307 ms
20,340 KB
testcase_25 AC 250 ms
20,852 KB
testcase_26 AC 247 ms
20,516 KB
testcase_27 AC 281 ms
21,608 KB
testcase_28 AC 489 ms
35,304 KB
testcase_29 AC 328 ms
31,236 KB
testcase_30 AC 473 ms
32,128 KB
testcase_31 AC 295 ms
21,880 KB
testcase_32 AC 310 ms
22,540 KB
testcase_33 AC 41 ms
11,128 KB
testcase_34 AC 93 ms
10,840 KB
testcase_35 AC 278 ms
28,368 KB
testcase_36 AC 299 ms
28,576 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int compression(std::vector<_Tp>&)’ 内:
main.cpp:193:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  193 |     for(auto [k,_] : mp) mp[k] = now++;
      |              ^

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.11.28 16:30:15
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

template< int MOD >
struct mint {
public:
    long long x;
    mint(long long x = 0) :x((x%MOD+MOD)%MOD) {}
    mint(std::string &s) {
        long long z = 0;
        for (int i = 0; i < s.size(); i++) {
            z *= 10;
            z += s[i] - '0';
            z %= MOD;
        }
        this->x = z;
    }
    mint& operator+=(const mint &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint &a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint &a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint& operator/=(const mint &a) {
        long long n = MOD - 2;
        mint u = 1, b = a;
        while (n > 0) {
            if (n & 1) {
                u *= b;
            }
            b *= b;
            n >>= 1;
        }
        return *this *= u;
    }
    mint operator+(const mint &a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-() const {return mint() -= *this; }
    mint operator-(const mint &a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint &a) const {
        mint res(*this);
        return res *= a;
    }
    mint operator/(const mint &a) const {
        mint res(*this);
        return res /= a;
    }
    friend std::ostream& operator<<(std::ostream &os, const mint &n) {
        return os << n.x;
    }
    friend std::istream &operator>>(std::istream &is, mint &n) {
        long long x;
        is >> x;
        n = mint(x);
        return is;
    }
    bool operator==(const mint &a) const {
        return this->x == a.x;
    }
    bool operator!=(const mint &a) const {
        return this->x != a.x;
    }
    mint pow(long long k) const {
        mint ret = 1;
        mint p = this->x;
        while (k > 0) {
            if (k & 1) {
                ret *= p;
            }
            p *= p;
            k >>= 1;
        }
        return ret;
    }
};
constexpr int MOD = 998244353;

template < class Monoid >
struct SegmentTree {
private:
    using Func = std::function< Monoid(Monoid, Monoid) >;
    Func F;
    Monoid UNITY;
    int n;
    std::vector< Monoid > node;
    int _binary_search(int a, int b, const std::function<bool(Monoid)> &f, Monoid &s, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return n;
        if (a <= l && r <= b && !f(F(s,node[k]))) {
            s = F(s,node[k]);
            return n;
        }
        if (l == r - 1) {s = F(s,node[k]); return l;}
        int ret = _binary_search(a, b, f, s, 2 * k + 1, l, (r - l) / 2 + l);
        return ret != n ? ret : _binary_search(a, b, f, s, 2 * k + 2, (r - l) / 2 + l, r);
    }
public:
    SegmentTree() {}
    SegmentTree(const std::vector< Monoid > &v, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        int sz = v.size();
        n = 1;
        while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        build();
    }
    
    SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        n = 1;
        while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        if (val != UNITY) {
            for (int i = 0; i < m; i++) node[i + n - 1] = val;
            build();
        }
    }
    
    void set(int k, const Monoid &x) {
        node[n + k - 1] = x;
    }
    void build() {
        for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }
    void update_query(int x, const Monoid &val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    
    Monoid get_query(int l, int r) {
        Monoid L = UNITY, R = UNITY;
        if (l < 0) l = 0;
        if (r < 0) return UNITY;
        if (l >= n) return UNITY;
        if (r >= n) r = n;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = F(L, node[l++ - 1]);
            if (r & 1) R = F(node[--r - 1], R);
        }
        return F(L, R);
    }
    
    
    int binary_search(int l, int r, const std::function<bool(Monoid)> &f) {
        Monoid s = UNITY;
        int ret = _binary_search(l,r,f,s);
        return ret != n ? ret : -1;
    }
    Monoid operator[](int x) const {
        return node[n + x - 1];
    }
    int size() {
        return n;
    }
    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i << "\t: " << node[n + i - 1] << std::endl;
        }
    }
};

template<typename T>
int compression(vector<T> &v) {
    map<T,T> mp;
    for (auto &val : v) mp[val] = 0;
    int now = 0;
    for(auto [k,_] : mp) mp[k] = now++;
    for (auto &val : v) val = mp[val];
    return now;
}
int main() {
    int n;cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    auto b = a;
    int max_e = compression(b);
    mint<MOD> ans = 0;
    
    {
        SegmentTree<pair<mint<MOD>,int>> seg1(max_e,{0,0},[](pair<mint<MOD>,int> a, pair<mint<MOD>,int> b){return make_pair(a.first+b.first,a.second+b.second);},{0,0});
        SegmentTree<pair<mint<MOD>,int>> seg2(max_e,{0,0},[](pair<mint<MOD>,int> a, pair<mint<MOD>,int> b){return make_pair(a.first+b.first,a.second+b.second);},{0,0});
        vector<pair<mint<MOD>,mint<MOD>>> lef(n), rig(n);
        for (int i = 0; i < n; i++) {
            seg1.update_query(b[i],{seg1[b[i]].first + a[i],seg1[b[i]].second + 1});
            lef[i] = seg1.get_query(b[i]+1,max_e);
        }
        for (int i = n-1; i >= 0; i--) {
            seg2.update_query(b[i],{seg2[b[i]].first + a[i],seg2[b[i]].second + 1});
            rig[i] = seg2.get_query(0,b[i]);
        }
        for (int i = 0; i < n; i++) {
            ans += lef[i].first * rig[i].second + rig[i].first * lef[i].second + lef[i].second * rig[i].second * a[i];
        }
    }
    cout << ans << endl;
    return 0;
}
0