結果

問題 No.1300 Sum of Inversions
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-11-28 16:20:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 744 ms / 2,000 ms
コード長 9,124 bytes
コンパイル時間 1,963 ms
コンパイル使用メモリ 137,540 KB
実行使用メモリ 21,928 KB
最終ジャッジ日時 2023-10-10 00:38:40
合計ジャッジ時間 19,561 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 533 ms
19,552 KB
testcase_04 AC 512 ms
19,588 KB
testcase_05 AC 400 ms
13,944 KB
testcase_06 AC 626 ms
20,636 KB
testcase_07 AC 593 ms
20,352 KB
testcase_08 AC 666 ms
18,460 KB
testcase_09 AC 678 ms
21,124 KB
testcase_10 AC 322 ms
12,912 KB
testcase_11 AC 324 ms
11,612 KB
testcase_12 AC 535 ms
16,280 KB
testcase_13 AC 514 ms
19,200 KB
testcase_14 AC 744 ms
21,928 KB
testcase_15 AC 646 ms
21,120 KB
testcase_16 AC 540 ms
19,752 KB
testcase_17 AC 305 ms
12,628 KB
testcase_18 AC 353 ms
11,972 KB
testcase_19 AC 444 ms
18,428 KB
testcase_20 AC 467 ms
18,876 KB
testcase_21 AC 452 ms
18,872 KB
testcase_22 AC 407 ms
13,888 KB
testcase_23 AC 612 ms
20,608 KB
testcase_24 AC 421 ms
13,312 KB
testcase_25 AC 349 ms
13,028 KB
testcase_26 AC 350 ms
13,132 KB
testcase_27 AC 406 ms
13,868 KB
testcase_28 AC 696 ms
21,328 KB
testcase_29 AC 460 ms
18,868 KB
testcase_30 AC 677 ms
18,388 KB
testcase_31 AC 417 ms
14,680 KB
testcase_32 AC 437 ms
14,060 KB
testcase_33 AC 46 ms
7,740 KB
testcase_34 AC 98 ms
7,828 KB
testcase_35 AC 393 ms
15,756 KB
testcase_36 AC 413 ms
16,060 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int compression(std::vector<_Tp>&)’ 内:
main.cpp:285:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  285 |     for(auto [k,_] : mp) mp[k] = now++;
      |              ^

ソースコード

diff #

#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <stack>
#include <utility>
#include <array>
#include <random>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <assert.h>
using namespace std;
typedef long long ll;
#define dump(x) // // cerr << #x << " = " << (x) << " [" << __LINE__ << ":" << __FUNCTION__ << "] " << endl;
// vector出力
template < typename T >
ostream& operator<<(ostream& os, vector< T >& v) {
    os << "{";
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i < v.size() - 1 ? ", " : "");
    }
    os << "}";
    return os;
}
// pair出力
template < typename T, typename U >
ostream& operator<<(ostream& os, const pair< T, U >& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}
// map出力
template < typename T, typename U >
ostream& operator<<(ostream& os, const map< T, U >& map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// set 出力
template < typename T >
ostream& operator<<(ostream& os, const set< T >& set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// multiset 出力
template < typename T >
ostream& operator<<(ostream& os, const multiset< T >& set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#pragma endregion

/////////////////////////////////////////

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;

// verify : https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_A
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();
    }

    // val で埋める
    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();
        }
    }

    // 最後に 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]);
        }
    }

    // [l,r)
    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);
    }

    // [l,r)を満たすkでf(get_query(l,k+1))=trueとなる最小のk
    // 無ければ-1
    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<mint<MOD>> seg1(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0);
        SegmentTree<mint<MOD>> seg2(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0);
        for (int i = n-1; i >= 0; i--) {
            seg1.update_query(b[i],seg1[b[i]] + 1);
            seg2.update_query(b[i],seg2[b[i]]+seg1.get_query(0,b[i]));
            ans += seg2.get_query(0,b[i]) * a[i];
        }
    }
    // 右
    {
        SegmentTree<mint<MOD>> seg1(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0);
        SegmentTree<mint<MOD>> seg2(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0);
        for (int i = 0; i < n; i++) {
            seg1.update_query(b[i],seg1[b[i]] + 1);
            seg2.update_query(b[i],seg2[b[i]]+seg1.get_query(b[i]+1,max_e));
            ans += seg2.get_query(b[i]+1,max_e) * a[i];
        }
    }
    // 中
    {
        SegmentTree<mint<MOD>> seg1(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0);
        SegmentTree<mint<MOD>> seg2(max_e,0,[](mint<MOD> a, mint<MOD> b){return a+b;},0);
        vector<mint<MOD>> lef(n), rig(n);
        for (int i = 0; i < n; i++) {
            seg1.update_query(b[i],seg1[b[i]] + 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]] + 1);
            rig[i] = seg2.get_query(0,b[i]);
        }
        for (int i = 0; i < n; i++) {
            ans += lef[i] * rig[i] * a[i];
        }
    }
    cout << ans << endl;
    return 0;
}

/////////////////////////////////////////
0