#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 5e5 + 10;
int x[MAXN], e[MAXN];
int deg[MAXN];
int bit[MAXN];
int n;
void bit_add(int pos, int x){
for(int i=pos; i<=n; i+=(i&-i)){
bit[i] += x;
}
}
int bit_query(int pos){
int sum = 0;
for(int i=pos; i>0; i-=(i&-i)){
sum += bit[i];
}
return sum;
}
void compress(){
vector<pair<pair<int, int>, int>> r;
for(int i=0; i<n; i++) r.push_back({{x[i], e[i]}, i});
sort(r.begin(), r.end());
vector<int> to_erase;
int last_x = -1, last_e = -1;
for(int i=0; i<n; i++){
if(r[i].first.first == last_x && r[i].first.second == last_e){
to_erase.push_back(i);
}
last_x = r[i].first.first;
last_e = r[i].first.second;
}
while(!to_erase.empty()){
swap(r[to_erase.back()], r.back());
r.pop_back();
to_erase.pop_back();
}
n = r.size();
sort(r.begin(), r.end()); // ordenando por x
for(int i=0; i<n; i++){
x[i + 1] = r[i].first.first;
e[i + 1] = r[i].first.second;
}
}
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=0; i<n; i++) cin >> x[i] >> e[i];
compress();
/*
eh uma dag agora!
dai da para fazer bonitinho e deixar todo
mundo com out <= 2 (usando stack?)
*/
/*for(int i=1; i<=n; i++){
cout << x[i] << " " << e[i] << "\n";
}*/
stack<int> q;
q.push(0);
x[0] = -1e9;
e[0] = 1e9;
for(int i=1; i<=n; i++){
// primeiro menor à esquerda, porque da certo quando eh maior
while(!q.empty() && x[q.top()] - e[q.top()] >= x[i] - e[i]) q.pop();
if(!q.empty()){
if(q.top() + 1 != i){
bit_add(q.top() + 1, 1);
bit_add(i, -1);
}
// cout << i << " -> " << q.top() << "\n";
}
q.push(i);
}
while(!q.empty()) q.pop();
q.push(n + 1);
x[n + 1] = 1e9;
e[n + 1] = 1e9;
for(int i=n; i>=1; i--){
// primeiro maior à direita, porque da certo quando eh menor
while(!q.empty() && x[q.top()] + e[q.top()] <= x[i] + e[i]) q.pop();
if(!q.empty()){
if(i + 1 != q.top()){
bit_add(i + 1, 1);
bit_add(q.top(), -1);
}
// cout << i + 1 << " " << q.top() - 1 << "\n";
}
q.push(i);
}
int ans = 0;
for(int i=1; i<=n; i++) ans += (bit_query(i) == 0);
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |