#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define vl vector<long long>
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
const int tree_siz = 1024*1024-1;
ll drzewo[tree_siz+1];
ll oper[tree_siz+1];
void spych(int v)
{
drzewo[v*2+1] += oper[v];
drzewo[v*2] += oper[v];
oper[v*2+1] += oper[v];
oper[v*2] += oper[v];
oper[v] = 0;
}
void add_seg(int akt, int p1, int p2, int s1, int s2, ll w)
{
if(p2 < s1 || p1 > s2) return;
if(p1 >= s1 && p2 <= s2)
{
drzewo[akt] += w;
oper[akt] += w;
return;
}
spych(akt);
add_seg(akt*2,p1,(p1+p2)/2,s1,s2,w);
add_seg(akt*2+1,(p1+p2)/2+1,p2,s1,s2,w);
drzewo[akt] = max(drzewo[akt*2],drzewo[akt*2+1]);
}
ll get_seg(int akt, int p1, int p2, int s1, int s2)
{
if(p2 < s1 || p1 > s2) return 0;
if(p1 >= s1 && p2 <= s2)
{
return drzewo[akt];
}
spych(akt);
return max(get_seg(akt*2,p1,(p1+p2)/2,s1,s2),get_seg(akt*2+1,(p1+p2)/2+1,p2,s1,s2));
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random();
int n;
cin >> n;
vector<pll> v;
rep(i,n)
{
ll a,b;
cin >> a >> b;
v.pb({a,b});
}
sort(all(v));
ll ans = -1e18;
rep(i,n)
{
add_seg(1,0,tree_siz/2,0,i,v[i].ss);
add_seg(1,0,tree_siz/2,i,i,v[i].ff);
ans = max(ans,get_seg(1,0,tree_siz/2,0,i)-v[i].ff);
}
cout << ans << "\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... |