Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <ctime>
- using namespace std;
- template <typename T>
- T gcd(T a, T b) {
- while (a && b) {
- a %= b;
- swap(a, b);
- }
- return a + b;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int n;
- long long r;
- cin >> n >> r;
- vector<long long> a(n);
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- int first = 0;
- int second = 0;
- while (a[second] - a[first] <= r) {
- second++;
- if (second == n) {
- break;
- }
- }
- long long ans = 0;
- while (second < n) {
- ans += n - second;
- first++;
- while (a[second] - a[first] <= r) {
- second++;
- if (second == n) {
- break;
- }
- }
- }
- while (a[second] - a[first] < r) {
- ans += n - second;
- first++;
- if (first == n) {
- break;
- }
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement