結果

問題 No.956 Number of Unbalanced
ユーザー square1001square1001
提出日時 2019-12-19 00:20:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,235 ms / 2,000 ms
コード長 3,207 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 84,696 KB
実行使用メモリ 7,620 KB
最終ジャッジ日時 2023-09-21 04:28:43
合計ジャッジ時間 6,585 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 50 ms
5,652 KB
testcase_07 AC 84 ms
5,204 KB
testcase_08 AC 50 ms
5,620 KB
testcase_09 AC 33 ms
4,756 KB
testcase_10 AC 212 ms
4,616 KB
testcase_11 AC 222 ms
7,620 KB
testcase_12 AC 52 ms
5,620 KB
testcase_13 AC 22 ms
6,496 KB
testcase_14 AC 75 ms
7,188 KB
testcase_15 AC 27 ms
5,768 KB
testcase_16 AC 41 ms
6,400 KB
testcase_17 AC 24 ms
5,776 KB
testcase_18 AC 72 ms
7,256 KB
testcase_19 AC 29 ms
6,252 KB
testcase_20 AC 72 ms
7,192 KB
testcase_21 AC 22 ms
6,660 KB
testcase_22 AC 25 ms
5,940 KB
testcase_23 AC 42 ms
6,400 KB
testcase_24 AC 29 ms
6,196 KB
testcase_25 AC 912 ms
5,956 KB
testcase_26 AC 41 ms
4,680 KB
testcase_27 AC 1,235 ms
5,916 KB
testcase_28 AC 925 ms
5,908 KB
testcase_29 AC 70 ms
7,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef CLASS_FENWICKTREE
#define CLASS_FENWICKTREE

#include <cstddef>

template <class type>
class fenwick_tree {
private:
	std::size_t n, sz;
	type* val;
public:
	fenwick_tree() : n(0), sz(0) {};
	fenwick_tree(std::size_t n_) : n(n_) {
		sz = 1; while (sz < n) sz *= 2;
		val = new type[sz + 1]();
	}
	template <class InputIterator>
	fenwick_tree(InputIterator first, InputIterator last) : n(last - first) {
		sz = 1; while (sz < n) sz *= 2;
		val = new type[sz + 1]();
		std::size_t cur = 0;
		for (InputIterator it = first; it != last; ++it) val[++cur] += *it;
		for (std::size_t i = 1; i < sz; ++i) val[i + (i & ~(i - 1))] += val[i];
	}
	~fenwick_tree() { delete[] val; }
	void add(std::size_t pos, type delta) {
		for (std::size_t i = pos + 1; i <= sz; i += i & ~(i - 1)) {
			val[i] += delta;
		}
	}
	type getsum(std::size_t r) const {
		type ans = 0;
		for (std::size_t i = r; i >= 1; i -= i & ~(i - 1)) {
			ans += val[i];
		}
		return ans;
	}
	type getsum(std::size_t l, std::size_t r) const {
		return getsum(r) - getsum(l);
	}
	std::size_t binary_search(type threshold) const {
		std::size_t ans = 0;
		for (std::size_t i = sz / 2; i >= 1; i >>= 1) {
			if (threshold >= val[ans + i]) {
				threshold -= val[ans + i];
				ans += i;
			}
		}
		return ans;
	}
};

#endif // CLASS_FENWICKTREE

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	vector<int> SA = A;
	sort(SA.begin(), SA.end());
	SA.erase(unique(SA.begin(), SA.end()), SA.end());
	int S = SA.size();
	for (int i = 0; i < N; ++i) {
		A[i] = lower_bound(SA.begin(), SA.end(), A[i]) - SA.begin();
	}
	vector<vector<int> > G(S);
	for (int i = 0; i < N; ++i) {
		G[A[i]].push_back(i);
	}
	long long ans = 0;
	for (int i = 0; i < S; ++i) {
		int M = G[i].size();
		vector<int> l(M), r(M);
		for (int j = 0; j < M; ++j) {
			l[j] = max(G[i][j] - 2 * M + 2, 0);
			r[j] = min(G[i][j] + 2 * M, N + 1);
		}
		vector<int> cp;
		cp.insert(cp.begin(), l.begin(), l.end());
		cp.insert(cp.begin(), r.begin(), r.end());
		sort(cp.begin(), cp.end());
		cp.erase(unique(cp.begin(), cp.end()), cp.end());
		vector<int> sum(cp.size());
		for (int j = 0; j < M; ++j) {
			l[j] = lower_bound(cp.begin(), cp.end(), l[j]) - cp.begin();
			r[j] = lower_bound(cp.begin(), cp.end(), r[j]) - cp.begin();
			++sum[l[j]];
			--sum[r[j]];
		}
		for (int j = 1; j < cp.size(); ++j) {
			sum[j] += sum[j - 1];
		}
		int lp = -1;
		for (int j = 0; j < cp.size(); ++j) {
			if (sum[j] >= 1 && lp == -1) {
				lp = j;
			}
			else if (sum[j] == 0 && lp != -1) {
				vector<int> seq;
				for (int k = cp[lp]; k < cp[j]; ++k) {
					seq.push_back(2 * (lower_bound(G[i].begin(), G[i].end(), k) - G[i].begin()) - k);
				}
				int mn = *min_element(seq.begin(), seq.end());
				for (int k = 0; k < seq.size(); ++k) {
					seq[k] -= mn;
				}
				int mx = *max_element(seq.begin(), seq.end());
				fenwick_tree<int> bit(mx + 1);
				for (int k = 0; k < seq.size(); ++k) {
					ans += bit.getsum(seq[k]);
					bit.add(seq[k], 1);
				}
				lp = -1;
			}
		}
	}
	cout << ans << endl;
	return 0;
}
0