結果

問題 No.1300 Sum of Inversions
ユーザー 👑 jupirojupiro
提出日時 2020-12-02 16:54:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,090 bytes
コンパイル時間 1,392 ms
コンパイル使用メモリ 139,276 KB
実行使用メモリ 15,664 KB
最終ジャッジ日時 2023-10-11 11:47:29
合計ジャッジ時間 8,675 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 111 ms
12,804 KB
testcase_04 AC 109 ms
12,520 KB
testcase_05 AC 88 ms
10,876 KB
testcase_06 AC 127 ms
13,820 KB
testcase_07 AC 122 ms
13,600 KB
testcase_08 AC 134 ms
14,420 KB
testcase_09 AC 133 ms
14,480 KB
testcase_10 AC 71 ms
9,480 KB
testcase_11 AC 71 ms
9,572 KB
testcase_12 AC 110 ms
12,696 KB
testcase_13 AC 106 ms
12,428 KB
testcase_14 AC 146 ms
15,588 KB
testcase_15 AC 132 ms
14,452 KB
testcase_16 AC 112 ms
13,060 KB
testcase_17 AC 68 ms
9,412 KB
testcase_18 AC 80 ms
10,196 KB
testcase_19 AC 95 ms
11,392 KB
testcase_20 AC 98 ms
11,712 KB
testcase_21 AC 97 ms
11,712 KB
testcase_22 AC 86 ms
11,168 KB
testcase_23 AC 124 ms
13,732 KB
testcase_24 AC 90 ms
11,252 KB
testcase_25 AC 75 ms
10,008 KB
testcase_26 AC 75 ms
9,964 KB
testcase_27 AC 85 ms
10,544 KB
testcase_28 AC 137 ms
14,748 KB
testcase_29 AC 95 ms
11,564 KB
testcase_30 AC 133 ms
14,464 KB
testcase_31 AC 88 ms
10,896 KB
testcase_32 AC 92 ms
11,284 KB
testcase_33 AC 61 ms
15,644 KB
testcase_34 AC 74 ms
15,512 KB
testcase_35 AC 78 ms
15,648 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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<ll> 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