結果

問題 No.1300 Sum of Inversions
ユーザー 👑 jupirojupiro
提出日時 2020-12-02 16:57:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 4,092 bytes
コンパイル時間 1,464 ms
コンパイル使用メモリ 140,716 KB
実行使用メモリ 15,668 KB
最終ジャッジ日時 2023-10-11 11:47:52
合計ジャッジ時間 6,739 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 111 ms
12,780 KB
testcase_04 AC 111 ms
12,500 KB
testcase_05 AC 88 ms
11,020 KB
testcase_06 AC 130 ms
13,856 KB
testcase_07 AC 124 ms
13,488 KB
testcase_08 AC 139 ms
14,420 KB
testcase_09 AC 134 ms
14,540 KB
testcase_10 AC 72 ms
9,612 KB
testcase_11 AC 72 ms
9,572 KB
testcase_12 AC 112 ms
12,412 KB
testcase_13 AC 108 ms
12,444 KB
testcase_14 AC 154 ms
15,376 KB
testcase_15 AC 137 ms
14,320 KB
testcase_16 AC 116 ms
12,984 KB
testcase_17 AC 70 ms
9,260 KB
testcase_18 AC 82 ms
10,316 KB
testcase_19 AC 97 ms
11,380 KB
testcase_20 AC 102 ms
11,592 KB
testcase_21 AC 99 ms
11,652 KB
testcase_22 AC 89 ms
10,980 KB
testcase_23 AC 127 ms
13,872 KB
testcase_24 AC 93 ms
11,224 KB
testcase_25 AC 77 ms
10,220 KB
testcase_26 AC 76 ms
9,840 KB
testcase_27 AC 85 ms
10,616 KB
testcase_28 AC 141 ms
14,880 KB
testcase_29 AC 98 ms
11,564 KB
testcase_30 AC 137 ms
14,464 KB
testcase_31 AC 88 ms
11,004 KB
testcase_32 AC 96 ms
11,220 KB
testcase_33 AC 60 ms
15,644 KB
testcase_34 AC 75 ms
15,660 KB
testcase_35 AC 81 ms
15,668 KB
testcase_36 AC 86 ms
15,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long  mod = 998244353;

template<typename T>
struct BinaryIndexedTree {
	int n;
	vector<T> bit;
	BinaryIndexedTree() :n(0) {}
	BinaryIndexedTree(int _n) :n(_n) { bit = vector<T>(n + 1); }
	void add1(int idx, T val) {
		for (int i = idx; i <= n; i += i & -i) bit[i] += val;
	}
	T sum1(int idx) {
		T res = 0;
		for (int i = idx; i > 0; i -= i & -i) res += bit[i];
		return res;
	}
	//0-indexed
	void add(int idx, T val) { add1(idx + 1, val); }
	//0-indexed [left, right)
	T sum(int left, int right) { return sum1(right) - sum1(left); }

	int lower_bound(T x) {
		int res = 0;
		int k = 1;
		while (2 * k <= n) k <<= 1;
		for (; k > 0; k >>= 1) {
			if (res + k <= n and bit[res + k] < x) {
				x -= bit[res + k];
				res += k;
			}
		}
		return res;
	}
};

template<typename T>
struct Compress {
	vector<T> v;
	Compress() {}
	Compress(vector<T> _v) :v(_v) { build(); }

	void add(T x) {
		v.emplace_back(x);
	}

	void build() {
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end()), v.end());
	}
	void build(vector<T> _v) {
		v = _v;
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end()), v.end());
	}
	int get(T x) {
		return lower_bound(v.begin(), v.end(), x) - v.begin();
	}

	T& operator[](int i) { return v[i]; }


	int size() {
		return (int)v.size();
	}
};

struct mint {
	long long x;
	mint(long long x = 0) :x((x% mod + mod) % mod) {}
	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 {
		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;
	}
	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 {
		mint res(*this);
		return res /= a;
	}
};
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int n; cin >> n;
	vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i];
	Compress comp(a);
	vector<mint> lsum(n), lcnt(n), rsum(n), rcnt(n);
	{
		BinaryIndexedTree<ll> bsum(n), bcnt(n);
		for (int i = 0; i < n; i++) {
			int t = comp.get(a[i]);
			lcnt[i] = bcnt.sum(t + 1, n);
			lsum[i] = bsum.sum(t + 1, n);
			bcnt.add(t, 1);
			bsum.add(t, a[i]);
		}
	}
	{
		BinaryIndexedTree<ll> bsum(n), bcnt(n);
		for (int i = n - 1; i >= 0; i--) {
			int t = comp.get(a[i]);
			rcnt[i] = bcnt.sum(0, t);
			rsum[i] = bsum.sum(0, t);
			bcnt.add(t, 1);
			bsum.add(t, a[i]);
		}
	}

	mint res = 0;
	for (int i = 0; i < n; i++) {
		res += lsum[i] * rcnt[i];
		res += lcnt[i] * rsum[i];
		res += mint(a[i]) * lcnt[i] * rcnt[i];
	}
	cout << res.x << "\n";
}
0