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 ll long long
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
if(x<y)
{
x=y;
return 1;
}
return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
if(x>y)
{
x=y;
return 1;
}
return 0;
}
void online()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
}
struct Fenwick
{
vector<ll> Tree;
ll n;
Fenwick(ll _n=0)
{
n=_n;
Tree.resize(n+2);
}
void update(ll x,ll vl)
{
for(;x<=n;x+=x&-x) maximize(Tree[x],vl);
}
ll get(ll vl)
{
ll cr=0,x=(1<<(31-__builtin_clz(n)));
while(x)
{
if(x+cr<=n&&Tree[x+cr]<=vl)
{
cr+=x;
}
x/=2;
}
return cr;
}
};
struct SegmentTree
{
struct Node
{
ll vl,lazy;
Node(ll _vl=1e9,ll _lazy=1e9)
{
vl=_vl;
lazy=_lazy;
}
};
vector<Node> Tree;
ll n;
SegmentTree(ll _n=0)
{
n=_n;
Tree.resize(4*n);
}
void down(ll i)
{
ll x=Tree[i].lazy;
Tree[i].lazy=1e9;
minimize(Tree[i*2].vl,x);
minimize(Tree[i*2+1].vl,x);
minimize(Tree[i*2].lazy,x);
minimize(Tree[i*2+1].lazy,x);
}
void update(ll i,ll l,ll r,ll l1,ll r1,ll vl)
{
if(r1<l||r<l1) return;
if(l1<=l&&r<=r1)
{
minimize(Tree[i].vl,vl);
minimize(Tree[i].lazy,vl);
return;
}
down(i);
update(i*2,l,(l+r)/2,l1,r1,vl);
update(i*2+1,(l+r)/2+1,r,l1,r1,vl);
Tree[i].vl=min(Tree[i*2].vl,Tree[i*2+1].vl);
}
ll query(ll i,ll l,ll r,ll l1,ll r1)
{
if(l1==0) return 0;
if(r1<l||r<l1) return 1e9;
if(l1<=l&&r<=r1) return Tree[i].vl;
down(i);
return min(query(i*2,l,(l+r)/2,l1,r1),query(i*2+1,(l+r)/2+1,r,l1,r1));
}
};
const ll N=5e5+10;
pll a[N];
ll n,b[N];
int main()
{
online();
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].fi>>a[i].se;
}
ll on=n;
sort(a+1,a+n+1);
for(int i=1;i<=on;i++)
{
if(a[i].fi==a[i-1].fi)
{
a[i-1].fi=1e9;
n--;
}
}
sort(a+1,a+on+1);
Fenwick F1(n);
for(int i=n;i>=1;i--)
{
F1.update(i,a[i].fi+a[i].se);
b[i]=F1.get(a[i].fi+a[i].se);
}
Fenwick F2(n);
SegmentTree S(n);
for(int i=1;i<=n;i++)
{
F2.update(n-i+1,a[i].se+1e9-a[i].fi);
ll z=n-F2.get(a[i].se+1e9-a[i].fi)+1;
ll vl=S.query(1,1,n,z-1,i-1);
S.update(1,1,n,i,i,vl+1);
vl=S.query(1,1,n,i,i);
S.update(1,1,n,i,b[i],vl);
}
cout<<S.query(1,1,n,n,n);
}
# | 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... |