#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MID ((l+r)/2)
struct node{
ll val;
node *L, *R;
void init(ll l, ll r){
val=0;
if(l!=r){
L=new node;
L->init(l,MID);
R=new node;
R->init(MID+1,r);
}
}
void upd(ll l, ll r, ll i, ll x){
if(l==i && i==r){
val=max(val,x);
}
else if(l<=i && i<=r){
L->upd(l,MID,i,x);
R->upd(MID+1,r,i,x);
val=max(L->val,R->val);
}
}
ll ans(ll l, ll r, ll tl, ll tr){
if(tr-tl+1<=0) return 0;
else if(tl<=l && r<=tr) return val;
else if(r<tl || tr<l) return 0;
else return max(L->ans(l,MID,tl,tr),R->ans(MID+1,r,tl,tr));
}
};
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n;
cin>>n;
ll l[n], r[n];
for(ll i=0; i<n; i++){
cin>>l[i]>>r[i];
l[i]=max(0ll,i-l[i]);
r[i]=min(i+r[i],n-1);
}
ll ans[n]={};
node seg; seg.init(0,n-1);
vector<ll> xr[n];
for(ll i=0; i<n; i++){
xr[r[i]].push_back(i);
}
for(ll pos=n-1; pos>=0; pos--){
for(auto i:xr[pos]){
ans[i]=seg.ans(0,n-1,i+1,n-1)+1;
}
seg.upd(0,n-1,l[pos],ans[pos]);
}
ll max_ans=0;
for(ll i=0; i<n; i++){
max_ans=max(max_ans,ans[i]);
}
cout<<max_ans<<endl;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |