結果

問題 No.3456 Common Difference is D
コンテスト
ユーザー MM
提出日時 2026-02-28 13:42:20
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 498 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,402 ms
コンパイル使用メモリ 347,840 KB
実行使用メモリ 30,336 KB
最終ジャッジ日時 2026-02-28 13:42:27
合計ジャッジ時間 6,809 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
int main(){
	// input
	int N,D;
	cin >> N >> D;
	vector<int> A(N);
	map<int,vector<int>> mp;
	for(int i = 0; i < N; i++){
		cin >> A[i];
		mp[A[i]].push_back(i);
	}
	// solve
	long long ans = 0;
	for(int i = 0; i < N; i++){
		long long L = lower_bound(mp[A[i]-D].begin(),mp[A[i]-D].end(),i) - mp[A[i]-D].begin();
		long long R = mp[A[i]+D].end() - upper_bound(mp[A[i]+D].begin(),mp[A[i]+D].end(),i);
		ans += L * R;
	}
	// output
	cout << ans << endl;
}
0