結果

問題 No.3456 Common Difference is D
コンテスト
ユーザー あるふぁ
提出日時 2026-10-02 16:49:06
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 253 ms / 2,000 ms
+ 394µs
コード長 555 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,231 ms
コンパイル使用メモリ 186,904 KB
実行使用メモリ 30,336 KB
最終ジャッジ日時 2026-10-02 16:49:14
合計ジャッジ時間 7,368 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <map>
using namespace std;
using ll = long long;

int main(){
    int N, D;
    cin >> N >> D;
    vector<int> A(N);
    map<int, vector<int>> I;
    for (int i = 0; i < N; i++){
        cin >> A[i];
        I[A[i]].push_back(i);
    }

    ll ans = 0;
    for (int i = 0; i < N; i++){
        ll l = lower_bound(I[A[i]-D].begin(), I[A[i]-D].end(), i)-I[A[i]-D].begin();
        ll r = I[A[i]+D].end()-upper_bound(I[A[i]+D].begin(), I[A[i]+D].end(), i);
        ans += l*r;
    }

    cout << ans << endl;
}
0