t6

EternalEpic發表於2024-06-27
// Program written by Liu Zhaozhou ~~~
#include <bits/stdc++.h>

#define lowbit(x) (x & -x)

using namespace std;

inline char gc(void) {
	static char buf[100000], *p1 = buf, *p2 = buf;
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

#define gc() getchar()

template <class T> inline void read(T &x) {
	T f = 1; x = 0; static char c = gc();
	for (; !isdigit(c); c = gc()) if (c == '-') f = -f;
	for (; isdigit(c); c = gc()) x = x * 10 + (c & 15);
	x *= f;
}

inline void readstr(char *s) {
	do *s = gc(); while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
	do *(++s) = gc(); while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
	*s = 0; return;
}

inline void readch(char&x) { while (isspace(x = gc())); }

char pf[100000], *o1 = pf, *o2 = pf + 100000;
#define ot(x) (o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf) ++= x : *o1 ++= x)
template <class T>
inline void println(T x, char c = '\n') {
	if (x < 0) ot(45), x = -x;
	static char s[15], *b; b = s;
	if (!x) *b ++= 48;
	for (; x; * b ++= x % 10 + 48, x /= 10);
	for (; b-- != s; ot(*b)); ot(c);
}

template <class T> inline void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + 48);
}

template <class T> inline void writeln(T x, char c = '\n') { write(x); putchar(c); }
template <class T> inline void chkmax(T &x, const T y) { x > y ? x = x : x = y; }
template <class T> inline void chkmin(T &x, const T y) { x < y ? x = x : x = y; }

#define Ms(arr, opt) memset(arr, opt, sizeof(arr))
#define Mp(x, y) make_pair(x, y)

typedef long long ll;
typedef pair <int, int> pii;
#define MAXN 2000001

struct node {
    int l, r;
} a[MAXN];
inline bool cmp(node a, node b) { return a.l < b.l; }

struct Sgt {
#define lson o<<1
#define rson o<<1|1
    int sum1[MAXN << 2];
    int sum2[MAXN << 2];
    inline void pushup(int o) {
        sum1[o] = max(sum1[lson], sum1[rson]);
        sum2[o] = min(sum2[lson], sum2[rson]);
    }
    inline void build(int o, int l, int r) {
        if (l == r) {
            sum1[o] = a[l].l + a[l].r;
            sum2[o] = a[l].r - a[l].l;
            return ;
        }

        int mid = (l + r) >> 1;
        build(lson, l, mid);
        build(rson, mid + 1, r);
        pushup(o);
    }
    inline int min_query(int o, int l, int r, int ll, int rr) {
        if (ll <= l && r <= rr)
            return sum2[o];

        int mid = (l + r) >> 1;
        int ans = INT_MAX;

        if (ll <= mid)
            ans = min(ans, min_query(lson, l, mid, ll, rr));

        if (mid < rr)
            ans = min(ans, min_query(rson, mid + 1, r, ll, rr));

        return ans;
    }
    inline int max_query(int o, int l, int r, int ll, int rr) {
        if (ll <= l && r <= rr)
            return sum1[o];

        int mid = (l + r) >> 1;
        int ans = -1;

        if (ll <= mid)
            ans = max(ans, max_query(lson, l, mid, ll, rr));

        if (mid < rr)
            ans = max(ans, max_query(rson, mid + 1, r, ll, rr));

        return ans;
    }
} pp;

int n;
inline int Find(int x) {
    int l = 1, r = n, ans = 0;
    while (l <= r) {
        int mid = (l + r) >> 1;

        if (a[mid].l <= x)
            l = mid + 1, ans = mid;
        else
            r = mid - 1;
    }
    return ans;
}

signed main(void) {
    read(n);
    for (int i = 1; i <= n; i++)
        read(a[i].l), read(a[i].r);

    sort(a + 1, a + n + 1, cmp);
    pp.build(1, 1, n);
    int MAXX = -INT_MAX;

    for (int i = 1; i <= n; i++) {
        int r = a[i].r;
        int f = Find(r);
        int f1 = pp.min_query(1, 1, n, i, f);
        int f2 = pp.max_query(1, 1, n, i, f);
        MAXX = max(MAXX, max(r - a[i].l - f1, f2 - a[i].l - r));
    }

    writeln(MAXX);
    return 0;
}

/**/