This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
pi A[500005];
struct node{
int s, e, m, val, pos, lz;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e){
l = new node(s, m), r = new node(m+1, e);
}
pos = s, val = -1e18; lz = (int)1e18;
}
void prop(){
if(lz != (int)1e18){
val = lz;
if(s != e)l->lz = lz, r->lz = lz;
lz = (int)1e18;
}
}
void upd(int a, int b, int c){
prop();
if(s == a && b == e)lz = c;
else{
if(b <= m)l->upd(a, b, c);
else if(a > m)r->upd(a, b, c);
else l->upd(a, m, c), r->upd(m+1, b, c);
l->prop(), r->prop();
val = max(l->val, r->val);
if(l->val >= r->val)pos = l->pos;
else pos = r->pos;
}
}
pi qry(int a, int b){
prop();
if(s == a && b == e)return {val, pos};
if(b <= m)return l->qry(a, b);
if(a > m)return r->qry(a, b);
return max(l->qry(a, m), r->qry(m+1, b));
}
}*root, *root2;
int taken[500005];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++)cin >> A[i].fi >> A[i].se;
sort(A + 1, A + n + 1);
root = new node(1, n); root2 = new node(1, n);
for(int i = 1; i <= n; i++)root->upd(i, i, A[i].fi - A[i].se), root2->upd(i, i, -A[i].fi-A[i].se);
vector <pi> stuf;
for(int i = 1; i <= n; i++)stuf.push_back({A[i].se, i});
sort(stuf.begin(), stuf.end(), greater<>());
int ans = 0;
for(auto [i, j] : stuf){
if(taken[j])continue;
ans++;
//cerr << j << '\n';
if(j > 1){
while(1){
pi mx = root->qry(1, j - 1);
//cerr << j << ' ' << mx.fi << ' ' << mx.se << '\n';
if(mx.fi >= A[j].fi - A[j].se)taken[mx.se] = 1, root->upd(mx.se, mx.se, -1e18), root2->upd(mx.se, mx.se, -1e18);
else break;
}
}
if(j < n){
while(1){
pi mn = root2->qry(j + 1, n);
mn.fi = -mn.fi;
//cerr << mn.fi << ' ' << mn.se << '\n';
if(mn.fi <= A[j].fi + A[j].se)taken[mn.se] = 1, root->upd(mn.se, mn.se, -1e18), root2->upd(mn.se, mn.se, -1e18);
else break;
}
}
}
cout << ans;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
Main.cpp:92:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
92 | main(){
| ^~~~
# | 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... |