結果

問題 No.956 Number of Unbalanced
ユーザー climpetclimpet
提出日時 2019-12-20 02:31:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 1,749 bytes
コンパイル時間 495 ms
コンパイル使用メモリ 57,524 KB
実行使用メモリ 7,924 KB
最終ジャッジ日時 2023-09-21 09:27:18
合計ジャッジ時間 4,444 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 22 ms
5,756 KB
testcase_07 AC 28 ms
4,440 KB
testcase_08 AC 22 ms
5,628 KB
testcase_09 AC 17 ms
4,376 KB
testcase_10 AC 58 ms
4,376 KB
testcase_11 AC 112 ms
7,924 KB
testcase_12 AC 23 ms
6,064 KB
testcase_13 AC 12 ms
4,380 KB
testcase_14 AC 32 ms
7,580 KB
testcase_15 AC 14 ms
4,380 KB
testcase_16 AC 20 ms
5,916 KB
testcase_17 AC 11 ms
4,376 KB
testcase_18 AC 34 ms
7,552 KB
testcase_19 AC 14 ms
4,376 KB
testcase_20 AC 28 ms
7,552 KB
testcase_21 AC 15 ms
6,540 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 20 ms
5,860 KB
testcase_24 AC 14 ms
4,380 KB
testcase_25 AC 436 ms
4,380 KB
testcase_26 AC 16 ms
4,380 KB
testcase_27 AC 692 ms
4,868 KB
testcase_28 AC 436 ms
4,376 KB
testcase_29 AC 28 ms
7,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

namespace{
typedef long long LL;

template <class Tp>
struct fenwick_tree{
	typedef unsigned size_type;
	std::vector<Tp> bit;
	int minidx;

	fenwick_tree(){}
	explicit fenwick_tree(size_type n, int minidx_ = 1){
		init(n, minidx_);
	}
	void init(size_type n, int minidx_ = 1){
		bit.assign(n + 1, Tp());
		minidx = minidx_;
	}
	Tp sum(int i_) const{
		if(i_ < minidx){ return Tp(); }
		size_type i = i_ - minidx + 1;
		Tp s = Tp();
		while(i){
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}
	void add(int i_, Tp x){
		if(i_ < minidx){ return; }
		size_type i = i_ - minidx + 1;
		size_type sz = bit.size();
		while(i < sz){
			bit[i] += x;
			i += i & -i;
		}
	}
};

void main2(){
	int n;
	scanf("%d", &n);
	vector<int> as(n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &as[i]);
	}
	int mval = *max_element(as.begin(), as.end());
	vector<vector<int> > postbl(mval + 1);
	for(int i = 0; i < n; ++i){
		postbl[as[i]].push_back(i);
	}

	fenwick_tree<int> ft;
	vector<int> hs;
	hs.reserve(n + 1);
	LL ans = 0;
	for(int x = 0; x <= mval; ++x){
		const vector<int> &poss = postbl[x];
		int sz = poss.size();
		for(int l = 0; l < sz; ){
			int r;
			for(r = l + 1; r < sz && poss[r] - poss[r - 1] <= sz; ++r);
			int beg = max(poss[l] - sz + 1, 0);
			int en = min(poss[r - 1] + sz, n);
			hs.assign(1, 0);
			for(int i = beg; i < en; ++i){
				hs.push_back(hs.back() + (x == as[i] ? 1 : -1));
			}
			int minh = *min_element(hs.begin(), hs.end());
			int maxh = *max_element(hs.begin(), hs.end());
			ft.init(maxh - minh + 2, minh);
			for(int y : hs){
				ans += ft.sum(y - 1);
				ft.add(y, 1);
			}
			l = r;
		}
	}
	printf("%lld\n", ans);
}

}

int main(){ main2(); }
0