結果

問題 No.956 Number of Unbalanced
ユーザー HIR180HIR180
提出日時 2019-12-20 03:11:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 381 ms / 2,000 ms
コード長 1,490 bytes
コンパイル時間 2,900 ms
コンパイル使用メモリ 199,868 KB
実行使用メモリ 8,048 KB
最終ジャッジ日時 2023-09-21 10:36:03
合計ジャッジ時間 8,940 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 190 ms
5,656 KB
testcase_07 AC 298 ms
5,832 KB
testcase_08 AC 168 ms
5,576 KB
testcase_09 AC 97 ms
4,484 KB
testcase_10 AC 240 ms
4,788 KB
testcase_11 AC 303 ms
6,328 KB
testcase_12 AC 189 ms
5,488 KB
testcase_13 AC 45 ms
4,376 KB
testcase_14 AC 380 ms
8,016 KB
testcase_15 AC 87 ms
4,428 KB
testcase_16 AC 153 ms
6,200 KB
testcase_17 AC 65 ms
4,380 KB
testcase_18 AC 379 ms
7,860 KB
testcase_19 AC 62 ms
4,420 KB
testcase_20 AC 380 ms
8,048 KB
testcase_21 AC 47 ms
4,380 KB
testcase_22 AC 64 ms
4,380 KB
testcase_23 AC 152 ms
6,296 KB
testcase_24 AC 63 ms
4,376 KB
testcase_25 AC 254 ms
4,376 KB
testcase_26 AC 54 ms
4,380 KB
testcase_27 AC 185 ms
4,380 KB
testcase_28 AC 252 ms
4,376 KB
testcase_29 AC 381 ms
8,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,a[100005];
ll ans;
void rec(int l,int r){
	if(l > r) return;
	if(l == r) { ans++; return ;}
	int m = (l+r)/2;
	set<int>S;
	map<int,int>M;
	for(int i=m;i>=l;i--){
		M[a[i]]++;
		if( (m-i+1+1)/2 <= M[a[i]] ) S.insert(a[i]);
	}
	M.clear();
	for(int i=m+1;i<=r;i++){
		M[a[i]]++;
		if( (i-m+1)/2 <= M[a[i]] ) S.insert(a[i]);
	}
	for(auto cand:S){
		vector<int>vi;
		vi.resize((m-l+1)*2+5,0);
		int geta = (m-l+3);
		int cur = 0;
		for(int i=m;i>=l;i--){
			if(a[i] == cand) cur++;
			else cur--;
			vi[cur+geta]++;
		}
		for(int i=vi.size()-2;i>=0;i--) vi[i] += vi[i+1];
		cur = 0;
		for(int i=m+1;i<=r;i++){
			if(a[i] == cand) cur++;
			else cur--;
			int need = 1-cur+geta;
			ans += vi[need];
		}
	}
	rec(l,m);
	rec(m+1,r);
	return;
}
int main(){
	scanf("%d",&n);
	rep(i,n) scanf("%d",&a[i]);
	rec(0,n-1);
	printf("%lld\n",ans);
}
0