Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
All square houses have their centers on the -axis and sides parallel to the axes. Since every square contains , two houses overlap in 2D iff their projections on the -axis overlap. So this is secretly a 1D interval problem wearing a fake mustache.
Represent house by the interval Sort the houses by . Then any valid place for the new house is either:
If the new house has side , then its interval length on the -axis is also . Inside one gap between adjacent houses, the only interesting positions are the ones where the new house touches the left house or touches the right house. Anything else in the middle doesn’t touch anybody, so it’s invalid.
For adjacent houses and , let the empty distance between them be Then:
Initialize for the two outside positions: one touching the leftmost house from the left, and one touching the rightmost house from the right. After sorting, for each adjacent pair compute This is just , so compare it with :
Each house is a square centered on the -axis, with sides parallel to the axes. So house occupies
on the -axis.
Because every square is centered on the -axis, their vertical projections always overlap around . That means two houses overlap in 2D iff their horizontal intervals overlap with positive length.
So yes, the whole problem is just a 1D gap-counting problem. The geometry was mostly there to look scary.
Sort all houses by their center .
Any valid position for the new house must be one of these:
There is no point checking non-adjacent pairs: if something lies between house and house with another house in between, that middle house is the real obstacle.
Let the new house have side . Its projection on the -axis has length .
Consider two adjacent houses and . Their free gap is
The new house must:
Inside a gap, there are only two possible touching placements:
Now compare with :
That’s the entire combinatorics. No DP, no segment tree, no black magic.
Everything involves halves, which is annoying but unnecessary.
Multiply the gap formula by :
So for each adjacent pair we compute
and compare it with :
There is always exactly one valid position touching the leftmost house from the left, and exactly one valid position touching the rightmost house from the right.
So we start with
Then we process all adjacent gaps.
After sorting, every valid placement lies either outside all houses or in a gap between adjacent houses. The two outside placements are always valid and unique.
For a fixed adjacent gap:
Summing these contributions over all gaps counts every valid center exactly once.
Sorting dominates:
Time complexity is , and memory usage is
For a Codeforces , this is pleasantly straightforward once you stop letting the squares bully you.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n;
ll t;
cin >> n >> t;
vector<pair<ll, ll>> houses(n); // {x, a}
for (int i = 0; i < n; ++i) {
cin >> houses[i].first >> houses[i].second;
}
sort(houses.begin(), houses.end());
ll ans = 2; // one on the far left, one on the far right
for (int i = 0; i + 1 < n; ++i) {
ll x1 = houses[i].first;
ll a1 = houses[i].second;
ll x2 = houses[i + 1].first;
ll a2 = houses[i + 1].second;
// doubled free gap between the two houses
ll d = 2 * (x2 - x1) - a1 - a2;
if (d == 2 * t) ans += 1;
else if (d > 2 * t) ans += 2;
}
cout << ans << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n;
ll t;
cin >> n >> t;
vector<pair<ll, ll>> houses(n); // {x, a}
for (int i = 0; i < n; ++i) {
cin >> houses[i].first >> houses[i].second;
}
sort(houses.begin(), houses.end());
ll ans = 2; // one on the far left, one on the far right
for (int i = 0; i + 1 < n; ++i) {
ll x1 = houses[i].first;
ll a1 = houses[i].second;
ll x2 = houses[i + 1].first;
ll a2 = houses[i + 1].second;
// doubled free gap between the two houses
ll d = 2 * (x2 - x1) - a1 - a2;
if (d == 2 * t) ans += 1;
else if (d > 2 * t) ans += 2;
}
cout << ans << '\n';
return 0;
}