結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー mine691mine691
提出日時 2020-03-17 16:55:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,500 ms
コード長 1,368 bytes
コンパイル時間 1,544 ms
コンパイル使用メモリ 165,760 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 18:51:02
合計ジャッジ時間 2,662 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 10 ms
4,376 KB
testcase_12 AC 10 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define digit(N) cout << fixed << setprecision((N))

// 1 - indexed
template <typename T>
struct BIT
{
	vector<T> bit;

	BIT(int sz) : bit(sz + 1, 0) {}

	// sum of [1, k]
	T sum(int k)
	{
		T res = 0;
		for (int x = k; x > 0; x -= x & -x)
			res += bit[x];
		return res;
	}

	void add(int k, T w)
	{
		for (int x = k; x < bit.size(); x += x & -x)
			bit[x] += w;
	}

	// sum of [x, y)
	T getSum(int x, int y)
	{
		return sum(y - 1) - sum(x - 1);
	}

	// find an index i s.t. a[1] + a[2] + ⋯ + a[x] ≧ w
	T lower_bound(T w)
	{
		if (w <= 0)
			return 0;

		T x = 0;
		int p = 1, N = bit.size();

		while (2 * p <= N)
			p *= 2;

		for (T k = p; k > 0; k /= 2)
		{
			if (x + k <= N && bit[x + k] < w)
			{
				w -= bit[x + k];
				x += k;
			}
		}

		return x + 1;
	}
};

using ll = long long;
// p : permutation
ll inversion(vector<int> &p)
{
	int N = p.size();

	BIT<int> bit(N + 1);
	ll inv = 0;
	for (int i = 0; i < N; i++)
	{
		inv += bit.getSum(p[i], N + 1);
		bit.add(p[i], 1);
	}
	return inv;
}

int main()
{
	int N;
	cin >> N;
	vector<int> M(N);

	for (int i = 0; i < N; i++)
	{
		cin >> M[i];
	}

	cout << inversion(M) << "\n";
}
0