結果

問題 No.3456 Common Difference is D
コンテスト
ユーザー tau1235
提出日時 2026-02-28 15:49:46
言語 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  
実行時間 219 ms / 2,000 ms
コード長 492 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,226 ms
コンパイル使用メモリ 344,956 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2026-02-28 15:50:08
合計ジャッジ時間 5,579 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;

int main(){
	using ll=long long;
	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);
	}
	auto f=[&](int x,int r){
		if (!mp.count(x)) return 0LL;
		ll ret=lower_bound(mp[x].begin(),mp[x].end(),r)-mp[x].begin();
		return ret;
	};
	ll ans=0;
	for (int i=0;i<n;i++){
		ll c0=f(a[i]-d,i)-f(a[i]-d,0);
		ll c1=f(a[i]+d,n)-f(a[i]+d,i+1);
		ans+=c0*c1;
	}
	cout<<ans<<endl;
}
0