結果

問題 No.1300 Sum of Inversions
ユーザー T1610T1610
提出日時 2020-11-30 15:41:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,572 bytes
コンパイル時間 5,175 ms
コンパイル使用メモリ 260,488 KB
実行使用メモリ 120,260 KB
最終ジャッジ日時 2023-10-11 02:55:43
合計ジャッジ時間 50,059 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 1,468 ms
93,536 KB
testcase_04 AC 1,426 ms
90,872 KB
testcase_05 AC 1,135 ms
76,200 KB
testcase_06 AC 1,737 ms
104,312 KB
testcase_07 AC 1,716 ms
99,920 KB
testcase_08 AC 1,924 ms
109,820 KB
testcase_09 AC 1,931 ms
109,544 KB
testcase_10 AC 925 ms
63,704 KB
testcase_11 AC 959 ms
64,524 KB
testcase_12 AC 1,515 ms
91,376 KB
testcase_13 AC 1,437 ms
89,336 KB
testcase_14 TLE -
testcase_15 AC 1,888 ms
108,592 KB
testcase_16 AC 1,603 ms
95,436 KB
testcase_17 AC 866 ms
61,812 KB
testcase_18 AC 965 ms
70,032 KB
testcase_19 AC 1,161 ms
81,692 KB
testcase_20 AC 1,157 ms
83,412 KB
testcase_21 AC 1,227 ms
83,112 KB
testcase_22 AC 1,092 ms
76,324 KB
testcase_23 AC 1,748 ms
104,060 KB
testcase_24 AC 1,208 ms
78,128 KB
testcase_25 AC 1,001 ms
67,864 KB
testcase_26 AC 952 ms
66,888 KB
testcase_27 AC 1,086 ms
73,948 KB
testcase_28 AC 1,903 ms
112,092 KB
testcase_29 AC 1,279 ms
82,404 KB
testcase_30 AC 1,850 ms
108,888 KB
testcase_31 AC 1,142 ms
76,860 KB
testcase_32 AC 1,196 ms
78,924 KB
testcase_33 AC 125 ms
6,224 KB
testcase_34 AC 179 ms
6,336 KB
testcase_35 AC 797 ms
120,260 KB
testcase_36 AC 814 ms
120,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " "
#define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) 
#define dumpL(x) 
#define LINE 
#define LINEL 
#define dumpV(v) 
#define dumpVL(v) 
#define STOP assert(false)
#endif
#define mp make_pair
 
namespace std {
template<class S, class T>
ostream &operator <<(ostream& out, const pair<S, T>& a) {
    out << '(' << a.fi << ", " << a.se << ')';
    return out;
}
}

template<typename T>
tuple<map<T, int>, vector<T>> zaatu(const vector<vector<T>>& v) {
    map<T, int> mp;
    for(auto&& u: v) for(auto&& x: u) mp[x] = 0;
    vector<T> r;
    for(auto& p: mp) {
        p.second = r.size();
        r.emplace_back(p.first);
    }
    return {mp, r};
}

template <typename T, typename U> T pow_fast(T x, U n) {
    T ret = (T)1;
    while(n) {
        if(n & (U)1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}

template <typename ModType, ModType MOD> struct ModInt {
    ModType val;
    ModInt() : val(0) {}
    ModInt(ModType x) : val(x % MOD) {
        if(val < 0) val += MOD;
    };
    operator ModType() const { return val; }
    ModInt &operator+=(const ModInt &m) {
        val += m.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    ModInt &operator-=(const ModInt &m) {
        val -= m.val;
        if(val < 0) val += MOD;
        return *this;
    }
    ModInt &operator*=(const ModInt &m) {
        (val *= m.val) %= MOD;
        return *this;
    }
    ModInt &operator/=(const ModInt &m) {
        *this *= m.inverse();
        return *this;
    }
    ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; }
    ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; }
    ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; }
    ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; }
    ModInt inverse() const { return pow_fast(*this, MOD - 2); }
    ModInt pow(ModType n) const {return pow_fast(*this, n); }

    friend std::ostream &operator<<(std::ostream &os, const ModInt &rhs) {
        os << rhs.val;
        return os;
    }
    friend std::istream &operator>>(std::istream &is, ModInt &rhs) {
        ModType value;
        is >> value;
        rhs = ModInt(value);
        return is;
    }
};
using mint = ModInt<ll, 998244353LL>;


using ull = unsigned long long;

template <typename T>
struct Node {
    static inline const T initVal = 0;     //TODO: fix
    static inline const T initLazy = 0;    //TODO: fix
    T val;
    T lazy;
    ull l, r;
    unique_ptr<Node<T>> l_node, r_node;
    Node(ull l, ull r, const T& val = initVal) : l(l), r(r), val(val), lazy(initLazy) {}
    T getVal() {
        return val + lazy * T(r - l); //TODO: fix
    }
    T f1(const T& a, const T& b) {
        return a+b;               //TODO: fix
    }
    T f2(const T& a, const T& b) {
        return a+b;                       //TODO: fix
    }
    void push() {
        if(r - l > 1) {
            auto m = (l+r)/2;
            {
                if(!l_node) l_node = make_unique<Node>(l, m);
                if(lazy != initLazy) l_node->lazy = f2(l_node->lazy, this->lazy);
            }
            {
                if(!r_node) r_node = make_unique<Node>(m, r);
                if(lazy != initLazy) r_node->lazy = f2(r_node->lazy, this->lazy); 
            }
        }
        val = getVal();
        lazy = initLazy;
    }
    void fix() {
        if(r - l == 1) return;
        val = f1(l_node->getVal(), r_node->getVal());
    }
    T query(ull qL, ull qR) {
        if(qR <= l || r <= qL) return initVal;
        if(qL <= l && r <= qR) return getVal();
        if(r - l == 1)  return getVal();

        push();
        T l_val = l_node->query(qL, qR), r_val = r_node->query(qL, qR);
        return f1(l_val, r_val);
    }
    void update(ull uL, ull uR, const T& uVal) {
        if(uR <= l || r <= uL) return;
        if(uL <= l && r <= uR) lazy = f2(lazy, uVal);
        else {
            push();
            l_node->update(uL, uR, uVal);
            r_node->update(uL, uR, uVal);
            fix();
        }
    }
};

template <typename T>
struct DynamicLazySegTree {
    Node<T> root;
    DynamicLazySegTree(ull l, ull r, const T& val) : root(l, r, val) {}
    DynamicLazySegTree(ull r): root(0, r) {}
    T query(ull l, ull r) {
        return root.query(l, r);
    }
    void update(ull l, ull r, const T& val) {
        root.update(l, r, val);
    }
    void output() {
        auto l = root.l;
        auto r = root.r;
        REP(i, l, r) {
            cout << " " << query(i, i+1);
        }
        cout << '\n';
    }
};


int main(){
    int n;
    cin >> n;
    vl a(n);
    rep(i, n) cin >> a[i];
    vector<vl> aa(1, a);
    auto [mp, v] = zaatu(aa);
    
    DynamicLazySegTree<mint> seg1(n+10), seg2(n+10);
    DynamicLazySegTree<ll> cnt1(n+10), cnt2(n+10);
    
    mint ans = 0;

    repr(i, n) {
        int cur = mp[a[i]];
        // dump(i);dump(cur);dumpL(a[i]);
        seg1.update(cur, cur+1, a[i]);
        cnt1.update(cur, cur+1, 1);
        if(cur > 0) {
            seg2.update(cur, cur+1, seg1.query(0, cur)+mint(cnt1.query(0, cur))*mint(a[i]));
            cnt2.update(cur, cur+1, cnt1.query(0, cur));
        }
        if(cur > 0) {
            // auto tmp = seg2.query(0, cur) + mint(cnt2.query(0, cur) * a[i]);
            // dumpL(tmp);
            ans += seg2.query(0, cur) + mint(cnt2.query(0, cur)) * mint(a[i]);
        }
        // seg1.output();
        // cnt1.output();
        // seg2.output();
        // cnt2.output();
    }
    cout << ans << '\n';
    return 0;
}
0