結果

問題 No.1079 まお
ユーザー 沙耶花沙耶花
提出日時 2020-06-13 09:50:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 858 ms / 2,000 ms
コード長 1,234 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 209,288 KB
実行使用メモリ 12,952 KB
最終ジャッジ日時 2023-09-07 02:32:19
合計ジャッジ時間 17,299 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 5 ms
4,384 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 367 ms
8,064 KB
testcase_13 AC 388 ms
8,908 KB
testcase_14 AC 521 ms
9,820 KB
testcase_15 AC 590 ms
10,900 KB
testcase_16 AC 636 ms
11,620 KB
testcase_17 AC 779 ms
10,736 KB
testcase_18 AC 652 ms
7,712 KB
testcase_19 AC 797 ms
10,732 KB
testcase_20 AC 770 ms
10,744 KB
testcase_21 AC 756 ms
10,196 KB
testcase_22 AC 838 ms
12,812 KB
testcase_23 AC 816 ms
12,768 KB
testcase_24 AC 835 ms
12,892 KB
testcase_25 AC 826 ms
12,936 KB
testcase_26 AC 858 ms
12,768 KB
testcase_27 AC 43 ms
4,376 KB
testcase_28 AC 276 ms
8,144 KB
testcase_29 AC 363 ms
12,932 KB
testcase_30 AC 380 ms
12,952 KB
testcase_31 AC 57 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000005

int N,K;
long long ans = 0;
vector<int> A;

void dfs(int L,int R){
	if(R-L<=1)return;
	int M = (L+R)/2;

	dfs(L,M);
	dfs(M,R);
		
	map<int,long long> mpL,mpR,sumR;
	int r = M;
	for(int i=M-1;i>=L;i--){
		mpL[A[i]]++;
		if(mpL.begin()->second>1)continue;
		int m = mpL.begin()->first;
		if(mpR.size()!=0&&mpR.begin()->first<=m)continue;
		while(true){
			if(r==R)break;
			if(A[r]<=m)break;
			mpR[A[r]]++;
			sumR[A[r]]+=r;
			r++;
		}
		int t = K-A[i];
		if(!mpR.count(t))continue;
		ans += sumR[t] - mpR[t]*(i-1);
	}
	mpL.clear();
	mpR.clear();
	sumR.clear();
	r = M-1;
	for(int i=M;i<R;i++){
		mpL[A[i]]++;
		if(mpL.begin()->second>1)continue;
		int m = mpL.begin()->first;

		if(mpR.size()!=0&&mpR.begin()->first<=m)continue;

		while(true){
			if(r<L)break;
			if(A[r]<=m)break;
			mpR[A[r]]++;
			sumR[A[r]]+=r;
			r--;
		}
		int t = K-A[i];
		if(!mpR.count(t))continue;
		ans += mpR[t]*(i+1)-sumR[t];
	}
}
			

int main(){
	
	cin>>N>>K;
	
	A.resize(N);
	for(int i=0;i<N;i++)cin>>A[i];
	
	dfs(0,N);
	
	for(int i=0;i<N;i++)if(A[i]*2==K)ans++;
	
	cout<<ans<<endl;
	
	return 0;
}
0