結果

問題 No.1526 Sum of Mex 2
ユーザー spipipikespipipike
提出日時 2021-06-01 22:03:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 3,000 ms
コード長 1,147 bytes
コンパイル時間 2,784 ms
コンパイル使用メモリ 178,500 KB
実行使用メモリ 12,704 KB
最終ジャッジ日時 2023-08-08 17:19:00
合計ジャッジ時間 4,772 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 11 ms
4,476 KB
testcase_16 AC 49 ms
8,708 KB
testcase_17 AC 32 ms
7,120 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 21 ms
5,428 KB
testcase_21 AC 49 ms
8,352 KB
testcase_22 AC 27 ms
6,316 KB
testcase_23 AC 46 ms
8,340 KB
testcase_24 AC 48 ms
8,604 KB
testcase_25 AC 44 ms
8,356 KB
testcase_26 AC 48 ms
8,552 KB
testcase_27 AC 49 ms
8,604 KB
testcase_28 AC 49 ms
8,968 KB
testcase_29 AC 49 ms
8,584 KB
testcase_30 AC 49 ms
9,120 KB
testcase_31 AC 51 ms
8,864 KB
testcase_32 AC 48 ms
8,884 KB
testcase_33 AC 53 ms
12,704 KB
evil_largest AC 162 ms
31,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
	int n;
	cin>>n;

	vector<vector<int>> ai(n+2);
	for(int i=1; i<=n+1; i++) ai[i].push_back(-1);
	rep(i, n){
		int a;
		cin>>a;
		ai[a].push_back(i);
	}
	for(int i=1; i<=n+1; i++) ai[i].push_back(n);

	ll tot=0;
	map<int, int> lr, rl;
	for(int i=1; i<=n+1; i++){
		if(i==1){
			auto calc=[](int a){ return (ll)(a+1)*a/2; };
			rep(j, ai[i].size()-1) tot+=calc(ai[i][j+1]-ai[i][j]-1);

			for(int j=1; j<ai[i].size()-1; j++){
				lr[ai[i][j]]=ai[i][j];
			}
		}
		else{
			rep(j, ai[i].size()-1){
				int l=ai[i][j], r=ai[i][j+1];
				
				int cl=l;
				vector<map<int, int>::iterator> ml;
				for(auto it=lr.lower_bound(l); it!=lr.end() && (it->second)<r; it++){
					tot+=(ll)((it->first)-cl)*(r-(it->second))*i;
					cl=(it->first);
					ml.push_back(it);
				}
				if(ml.size()>=1){
					int firstr=ml[0]->second, lastl=ml.back()->first;
					for(auto it : ml) lr.erase(it);
					if(1<=j) lr[l]=firstr;
					if(j<ai[i].size()-1) lr[lastl]=r;
				}
			}
		}
	}

	cout<<tot<<endl;
	return 0;
}
0