Pagini recente » Cod sursa (job #1080940) | Cod sursa (job #1432378) | Cod sursa (job #2463607) | Cod sursa (job #437785) | Cod sursa (job #2555065)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream fin("heavymetal.in");
ofstream fout("heavymetal.out");
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;
class aib{
private:
int n;
vector <int> tree;
public:
aib(int n){
this->n=n;
tree.resize(n+1);
}
void update(int x,int quantity){
int i;
for(i=x;i<=n;i+= ((i)&(-i)))
tree[i]=max(tree[i],quantity);
}
int query(int x){
int i,ans=0;
for(i=x;i>0;i-= ((i)&(-i)))
ans=max(tree[i],ans);
return ans;
}
};
const int DMAX = 2e5+10;
vector <int> vec;
int pos[DMAX];
pii V[DMAX];
int n;
int dif;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t,i,j;
int poz,poz2,maxim;
fin>>n;
for(i=1;i<=n;i++){
fin>>V[i].first>>V[i].second;
vec.pb(V[i].first);
vec.pb(V[i].second);
}
sort(V+1,V+n+1);
sort(vec.begin(),vec.end());
dif=1;
pos[0]=1;
for(i=1;i<vec.size();i++)
if(vec[i] != vec[i-1])
pos[i]=++dif;
else
pos[i]=dif;
aib tree(dif);
for(i=1;i<=n;i++){
poz=lower_bound(vec.begin(),vec.end(),V[i].first)-vec.begin();
poz2=lower_bound(vec.begin(),vec.end(),V[i].second)-vec.begin();
maxim=tree.query(pos[poz]);
tree.update(pos[poz2],maxim+V[i].second-V[i].first);
}
fout<<tree.query(dif)<<'\n';
return 0;
}