結果

問題 No.956 Number of Unbalanced
ユーザー QCFiumQCFium
提出日時 2020-01-26 00:31:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 1,904 bytes
コンパイル時間 2,738 ms
コンパイル使用メモリ 173,924 KB
実行使用メモリ 44,972 KB
最終ジャッジ日時 2023-10-12 04:49:22
合計ジャッジ時間 6,288 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,632 KB
testcase_01 AC 4 ms
5,420 KB
testcase_02 AC 4 ms
5,420 KB
testcase_03 AC 4 ms
5,628 KB
testcase_04 AC 3 ms
5,392 KB
testcase_05 AC 3 ms
5,424 KB
testcase_06 AC 117 ms
11,700 KB
testcase_07 AC 117 ms
11,504 KB
testcase_08 AC 111 ms
11,696 KB
testcase_09 AC 74 ms
11,228 KB
testcase_10 AC 101 ms
11,188 KB
testcase_11 AC 167 ms
12,748 KB
testcase_12 AC 122 ms
11,700 KB
testcase_13 AC 66 ms
31,532 KB
testcase_14 AC 202 ms
12,580 KB
testcase_15 AC 100 ms
32,416 KB
testcase_16 AC 139 ms
12,096 KB
testcase_17 AC 107 ms
44,972 KB
testcase_18 AC 192 ms
12,564 KB
testcase_19 AC 92 ms
33,320 KB
testcase_20 AC 197 ms
12,544 KB
testcase_21 AC 67 ms
33,640 KB
testcase_22 AC 101 ms
35,468 KB
testcase_23 AC 137 ms
11,964 KB
testcase_24 AC 89 ms
31,180 KB
testcase_25 AC 102 ms
11,504 KB
testcase_26 AC 57 ms
11,320 KB
testcase_27 AC 105 ms
11,508 KB
testcase_28 AC 104 ms
11,448 KB
testcase_29 AC 210 ms
12,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct BIT {
	int n;
	std::vector<int64_t> data;
	std::vector<int> written;
	BIT (int n) : n(n), data(n + 1, 0) {}
	void add(int i, int64_t val) {
		for (i++; i <= n; i += i & -i) data[i] += val, written.push_back(i);
	}
	int64_t sum(int r) {
		int64_t res = 0;
		for (; r; r &= r - 1) res += data[r];
		return res;
	}
	void clear() {
		for (auto i : written) data[i] = 0;
		written.clear();
	}
};

struct BIT2 {
	int n;
	BIT tree0, tree1, tree2;
	BIT2(int n) : n(n), tree0(2 * n + 1), tree1(2 * n + 1), tree2(2 * n + 1) {}
	void add(int l, int r, int val) {
		l += n;
		r += n;
		tree0.add(l, val);
		tree1.add(l, -val * l);
		tree2.add(l, val * ((int64_t) l * (l - 1) / 2));
		tree0.add(r, -val);
		tree1.add(r, val * r);
		tree2.add(r, -val * ((int64_t) l * (l - 1) / 2 + (int64_t) r * (r - l) - (int64_t) (r - l) * (r - l + 1) / 2));
	}
private :
	int64_t sum1(int r) {
		return ((int64_t) r * (r + 1) / 2 * tree0.sum(r) + (int64_t) r * tree1.sum(r) + tree2.sum(r));
	}
public :
	int64_t sum2(int l, int r) {
		l += n;
		r += n;
		return sum1(r) - sum1(l);
	}
	void clear() {
		tree0.clear();
		tree1.clear();
		tree2.clear();
	}
};

int main() {
	int n = ri();
	int a[n];
	std::vector<std::vector<int> > indexes(100001);
	for (int i = 0; i < n; i++) a[i] = ri(), indexes[a[i]].push_back(i);
	BIT2 tree(n);
	int64_t res = 0;
	for (auto &i : indexes) {
		if (!i.size()) continue;
		tree.add(0, 1, 1);
		int last = 0;
		int cur = 0;
		auto process_empty = [&] (int len) {
			if (!len) return;
			res += tree.sum2(cur - len - 1, cur - 1);
			tree.add(cur - len, cur, 1);
			cur -= len;
		};
		for (auto j : i) {
			process_empty(j - last);
			res += tree.sum2(cur, cur + 1);
			cur++;
			tree.add(cur, cur + 1, 1);
			last = j + 1;
		}
		process_empty(n - last);
		tree.clear();
	}
	std::cout << res << std::endl;
	return 0;
}
0