Submission #4634379


Source Code Expand

Copy
#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<fstream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>

using namespace std;
typedef long long ll;

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N, D;
    cin >> N >> D;
    
    vector<ll> A(N);
    FOR(i, 0, N){
        cin >> A.at(i);
    }

    ll sum = 0;
    // 中央jから見る
    FOR(j, 1, N-1){
        auto it_j = lower_bound(ALL(A), A[j]);

        // 右
        auto it_k = lower_bound(ALL(A), A[j]+D);
        if(it_k==A.end()){
            it_k--;
        }
        else if(*it_k!=A[j]+D){
            it_k--;
        }
        ll right = it_k - it_j;

        // a_i
        auto it_i = lower_bound(ALL(A), A[j]-D);
        ll left = it_j - it_i;

        sum += left * right;
    }

    // 余分なのを除去
    FOR(i, 0, N-2){
        auto it_i = lower_bound(ALL(A), A[i]);
        auto it = lower_bound(ALL(A), A[i]+D);
        if(it==A.end()){
            it--;
        }
        else if(*it!=A[i]+D){
            it--;
        }
        ll num = it - it_i;
        if(num>=2){
            ll excess = num * (num-1) / 2;
            sum -= excess;
        }
    }

    p(sum);
    
    return 0;
}

Submission Info

Submission Time
Task C - 徒歩圏内
User peroon
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1811 Byte
Status AC
Exec Time 35 ms
Memory 1024 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 16
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
Case Name Status Exec Time Memory
01.txt AC 34 ms 1024 KB
02.txt AC 35 ms 1024 KB
03.txt AC 29 ms 1024 KB
04.txt AC 34 ms 1024 KB
05.txt AC 35 ms 1024 KB
06.txt AC 31 ms 1024 KB
07.txt AC 31 ms 1024 KB
08.txt AC 30 ms 1024 KB
09.txt AC 1 ms 256 KB
10.txt AC 31 ms 1024 KB
11.txt AC 34 ms 1024 KB
12.txt AC 31 ms 1024 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB
sample-03.txt AC 1 ms 256 KB
sample-04.txt AC 1 ms 256 KB