# [AT2383] [AGC015 E] Mr.Aoki Incubator

### 輸入輸出樣例

#### 輸入樣例#1：

3
2 5
6 1
3 7


#### 輸出樣例#1：

6


#### 輸入樣例#2：

4
3 7
2 9
8 16
10 8


#### 輸出樣例#2：

9


### 解題分析

• $b$速度比$a$慢， 但在點$a$前面。
• $b$速度比$a$快， 但在點$a$後面。
• $b$速度比$a$慢， 在$a$後面， 但存在點$c$比點$b$速度慢， 在$a$前面。
• $b$速度比$a$快， 在$a$前面， 但存在點$c$比點$b$速度快， 在$a$後面。

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <climits>
#include <set>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define MX 200500
#define gc getchar()
#define ls (now << 1)
#define rs (now << 1 | 1)
#define ll long long
#define MOD 1000000007
#define lbt(i) ((i) & (-(i)))
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc);
for (;  isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
}
template <class T> IN T max(T a, T b) {return a > b ? a : b;}
template <class T> IN T min(T a, T b) {return a < b ? a : b;}
int n, bd;
int tree[MX];
struct INFO {int pos, spd;} dat[MX];
struct Seg {int lb, rb;} seg[MX];
IN bool operator < (const INFO &x, const INFO &y) {return x.spd < y.spd;}
IN bool operator < (const Seg &x, const Seg &y) {return x.rb == y.rb ? x.lb < y.lb : x.rb < y.rb;}
namespace SGT1
{
int mn[MX << 2], mx[MX << 2];
IN void pushup(R int now)
{
mn[now] = min(mn[ls], mn[rs]);
mx[now] = max(mx[ls], mx[rs]);
}
void build (R int now, R int lef, R int rig)
{
if (lef == rig) return mn[now] = mx[now] = dat[lef].pos, void();
int mid = lef + rig >> 1;
build(ls, lef, mid), build(rs, mid + 1, rig);
pushup(now);
}
int qmin(R int now, R int lef, R int rig, R int lb, R int rb)
{
if (lef >= lb && rig <= rb) return mn[now];
int mid = lef + rig >> 1, ret = INT_MAX;
if (lb <= mid) ret = qmin(ls, lef, mid, lb, rb);
if (rb >  mid) ret = min(ret, qmin(rs, mid + 1, rig, lb, rb));
return ret;
}
int qmax(R int now, R int lef, R int rig, R int lb, R int rb)
{
if (lef >= lb && rig <= rb) return mx[now];
int mid = lef + rig >> 1, ret = -INT_MAX;
if (lb <= mid) ret = qmax(ls, lef, mid, lb, rb);
if (rb >  mid) ret = max(ret, qmax(rs, mid + 1, rig, lb, rb));
return ret;
}
int qpos(R int now, R int lef, R int rig, R int lb, R int rb, R int tar, R bool typ)
{
if (lef == rig) return lef;
int mid = lef + rig >> 1;
if (!typ)//to query the most left point with pos > tar
{
if (mx[ls] >= tar) return qpos(ls, lef, mid, lb, rb, tar, typ);
else return qpos(rs, mid + 1, rig, lb, rb, tar, typ);
}
else
{
if (mn[rs] <= tar) return qpos(rs, mid + 1, rig, lb, rb, tar, typ);
else return qpos(ls, lef, mid, lb, rb, tar, typ);
}
}
}
IN void add(R int pos, R int v)
{for (; pos <= bd; pos += lbt(pos)) (tree[pos] += v) %= MOD;}
IN int qpfix(R int pos)
{
int ret = 0;
for (; pos; pos -= lbt(pos)) (ret += tree[pos]) %= MOD;
return ret;
}
IN int query(R int lb, R int rb) {return (qpfix(rb) - qpfix(lb - 1) + MOD) % MOD;}
int main(void)
{
in(n); bd = n + 1;
for (R int i = 1; i <= n; ++i) in(dat[i].pos), in(dat[i].spd);
std::sort(dat + 1, dat + 1 + n);
SGT1::build(1, 1, n);
for (R int i = 1; i <= n; ++i)
{
seg[i].lb = SGT1::qpos(1, 1, n, 1, i, dat[i].pos, 0);
seg[i].rb = SGT1::qpos(1, 1, n, i, n, dat[i].pos, 1);
}
std::sort(seg + 1, seg + 1 + n);