895B - XK Segments - CodeForces Solution


binary search math sortings two pointers *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast", "unroll-loops")
const int maxn = 3e5 + 10, block = 320, mod = 1e9 + 9;
void solve(){
	long long n, x, k;
	cin >> n >> x >> k;
	long long a[n];
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	sort(a, a + n);
	long long ans = 0;
	for(int i = 0; i < n; i++){
		long long temp = (a[i] - 1) / x;
		long long l = (temp + k) * x;
		long long r = (temp + k) * x + x - 1;
		l = max(l, a[i]);
		ans += upper_bound(a, a + n, r) - lower_bound(a, a + n, l);
	}
	cout << ans << "\n";
}
int main(){
	int t;
	t = 1;
	while(t--){
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

67B - Restoration of the Permutation
1734A - Select Three Sticks
1734B - Bright Nice Brilliant
357B - Flag Day
937A - Olympiad
1075A - The King's Race
1734C - Removing Smallest Multiples
1004C - Sonya and Robots
922A - Cloning Toys
817A - Treasure Hunt
1136B - Nastya Is Playing Computer Games
1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays
424C - Magic Formulas
1730C - Minimum Notation
1730B - Meeting on the Line
1730A - Planets
302B - Eugeny and Play List
1730D - Prefixes and Suffixes
1515C - Phoenix and Towers
998A - Balloons
1734F - Zeros and Ones
1144B - Parity Alternated Deletions
92B - Binary Number
1144C - Two Shuffled Sequences