結果

問題 No.956 Number of Unbalanced
ユーザー kmjp
提出日時 2019-12-19 00:21:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 768 ms / 2,000 ms
コード長 1,610 bytes
コンパイル時間 1,501 ms
コンパイル使用メモリ 171,504 KB
実行使用メモリ 9,156 KB
最終ジャッジ日時 2024-07-06 23:05:17
合計ジャッジ時間 12,847 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
int A[101010];
vector<int> E[101010];
int C[101010];

template<class V, int ME> class BIT {
public:
	V bit[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { e++; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
};
BIT<int,18> bt;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) {
		cin>>A[i];
		E[A[i]].push_back(i);
	}
	
	ll ret=0;
	FOR(x,N) {
		int ma=0;
		FOR(i,1000) if(x+i<N) {
			if(E[A[x+i]].size()<500) {
				C[A[x+i]]++;
				ma=max(ma,C[A[x+i]]);
			}
			if(ma*2>i+1) ret++;
		}
		FOR(i,1000) if(x+i<N && E[A[x+i]].size()<500) C[A[x+i]]--;
	}
	
	FOR(i,100100) if(E[i].size()>=500) {
		int cur=1<<17;
		bt.add(cur,1);
		FOR(x,N) {
			if(A[x]==i) cur++;
			else cur--;
			ret+=bt(cur-1);
			bt.add(cur,1);
		}
		cur=1<<17;
		bt.add(cur,-1);
		FOR(x,N) {
			if(A[x]==i) cur++;
			else cur--;
			bt.add(cur,-1);
		}
	}
	
	
	
	
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0