結果

問題 No.1300 Sum of Inversions
ユーザー tsuyu93tsuyu93
提出日時 2020-11-28 12:18:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 6,328 bytes
コンパイル時間 2,304 ms
コンパイル使用メモリ 187,548 KB
実行使用メモリ 14,192 KB
最終ジャッジ日時 2023-10-09 23:51:04
合計ジャッジ時間 7,746 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 124 ms
11,496 KB
testcase_04 AC 120 ms
11,172 KB
testcase_05 AC 96 ms
9,892 KB
testcase_06 AC 138 ms
12,584 KB
testcase_07 AC 136 ms
12,072 KB
testcase_08 AC 153 ms
13,008 KB
testcase_09 AC 149 ms
13,124 KB
testcase_10 AC 75 ms
8,812 KB
testcase_11 AC 79 ms
8,880 KB
testcase_12 AC 117 ms
11,292 KB
testcase_13 AC 117 ms
11,212 KB
testcase_14 AC 167 ms
13,900 KB
testcase_15 AC 150 ms
12,760 KB
testcase_16 AC 122 ms
11,660 KB
testcase_17 AC 73 ms
8,524 KB
testcase_18 AC 88 ms
9,432 KB
testcase_19 AC 104 ms
10,500 KB
testcase_20 AC 107 ms
10,352 KB
testcase_21 AC 107 ms
10,460 KB
testcase_22 AC 97 ms
9,944 KB
testcase_23 AC 137 ms
12,492 KB
testcase_24 AC 98 ms
10,160 KB
testcase_25 AC 81 ms
9,168 KB
testcase_26 AC 78 ms
9,052 KB
testcase_27 AC 86 ms
9,544 KB
testcase_28 AC 142 ms
13,396 KB
testcase_29 AC 100 ms
10,420 KB
testcase_30 AC 148 ms
12,724 KB
testcase_31 AC 96 ms
9,844 KB
testcase_32 AC 96 ms
10,168 KB
testcase_33 AC 26 ms
10,864 KB
testcase_34 AC 38 ms
10,948 KB
testcase_35 AC 85 ms
14,184 KB
testcase_36 AC 89 ms
14,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
             author:ryo3ihara
                   ”継続は力なり、雨だれ石を穿つ”
                      ”slow but steady wins the race”


*/

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>

//#include<atcoder/all>
//using namespace atcoder;

/* 
// 多倍長テンプレ
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
*/

using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pii = pair<int, int>;
using pld = pair<ll, ld>;
using ppiii =  pair<pii, int>;
using ppiill = pair<pii, ll>;
using ppllll = pair<pll, ll>;
using pplii = pair<pli, int>;
using mii = map<int, int>;
using dll = deque<ll>;
using qll = queue<ll>;
using pqll = priority_queue<ll>;
using pqrll = priority_queue<ll, vector<ll>, greater<ll>>;
using vint = vector<int>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vvll = vector<vector<ll>>;
using vvint = vector<vector<int>>;
using vvpll = vector<vector<pll>>;

//マクロ
//forループ
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define ALL(x) x.begin(),x.end() 
#define rALL(x) x.rbegin(),x.rend() 
#define SIZE(x) ll(x.size()) 
#define fs first
#define sc second
//定数
const ll MOD = 1000000007;
const int inf = 1e9;
const ll INF = 1e18;
const ll MAXR = 100000; //10^5:配列の最大のrange

inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }

//最大化問題最小化問題
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
//高速冪乗化 modの時は一部入れ替える
ll power(ll x, ll y) {
    if(y == 1) {
        return x;
    }
    ll ans;
    if(y % 2 == 1) {
        ll r = power(x,(y-1)/2);
        //ans = r * r % MOD;
        //ans = ans * x % MOD;
        ans = x * r * r;
    }
    else {
        ll r = power(x,y/2);
        //ans = r * r % MOD;
        ans = r * r;
    }
    return ans;
}
/*
Bint powerb(Bint x, Bint y) {
    if(y == 1) {
        return x;
    }
    Bint ans;
    if(y % 2 == 1) {
        Bint r = powerb(x,(y-1)/2);
        //ans = r * r % MOD;
        //ans = ans * x % MOD;
        ans = x * r * r;
    }
    else {
        Bint r = powerb(x,y/2);
        //ans = r * r % MOD;
        ans = r * r;
    }
    return ans;
}
*/

template <class Abel> struct BIT {
private:
	vector<Abel> node; int n;
    Abel UNITY_SUM = 0; //to be set
public:
	BIT(int n_) {
		n = n_; node.resize(n, UNITY_SUM);
	}
    void clear() {
        node.assign(n, UNITY_SUM);
    }
	//0-indexed
	void add(int a, Abel w) {
		for (int i = a; i < n; i |= i + 1)node[i] += w;
	}
	//[0,a)
	Abel sum(int a) {
		Abel ret = UNITY_SUM;
		for (int i = a - 1; i >= 0; i = (i&(i + 1)) - 1)ret += node[i];
		return ret;
	}
	//[a,b)
	Abel sum(int a, int b) {
		return sum(b) - sum(a);
	}
	//k-th number (k is 0-indexed)
    int get(int k) {
        ++k;
        int res = 0;
        int n = 1; while(n < (int)node.size()) n *= 2;
        for(int i = n / 2; i > 0; i /= 2) {
            if(res + i <= (int)node.size() && node[res + i - 1] < k) {
                k -= node[res + i - 1];
                res += i;
            }
        }
        return res; //0-indexed
    }
    //debug
    void print() {
        for(int i = 0; i < n; ++i) cout << sum(i, i + 1) << ",";
        cout << endl;
    }
};

//const int mod = 1000000007;
const int mod = 998244353;
struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    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) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

template<typename T>
vector<T> compress(vector<T> A){
    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());
    return A;
}

signed main(){
    //入力の高速化用のコード
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    //入力
    int n;
    cin >> n;

    vint a(n);
    REP(i,n) cin >> a[i];

    auto coma = compress(a);
    int sz = (int)coma.size();
    BIT <mint> cnt(sz),sum(sz);
    vector<mint> left(n,0), right(n,0);
    vector<mint> lsum(n,0),rsum(n,0);
    REP(i,n){
        int pos = lower_bound(ALL(coma),a[i]) - coma.begin();
        left[i] = cnt.sum(pos+1,sz);
        lsum[i] = sum.sum(pos+1,sz);
        cnt.add(pos, 1);
        sum.add(pos,a[i]);
    }
    cnt.clear(), sum.clear();
    REPD(i,n){
        int pos = lower_bound(ALL(coma),a[i]) - coma.begin();
        right[i] = cnt.sum(0,pos);
        rsum[i] = sum.sum(0,pos);
        cnt.add(pos, 1);
        sum.add(pos,a[i]);
    }
    mint ans = 0;
    REP(i,n){
        ans += left[i] * rsum[i] + right[i]*lsum[i]+(mint)a[i]*left[i]*right[i];
    }

    //本文


    //出力
    cout << ans << endl;
    //cout << fixed << setprecision(10) << ans << endl;
  return 0;
}
0