Problem:


Score : 600 points

Problem Statement

10^9 contestants, numbered 1 to 10^9, will compete in a competition.
There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests.
When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests.
Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest.
Find the maximum possible total joyfulness of the two contests.

Constraints

2 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.

Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.

The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931



Sample input:
4
4 7
1 4
5 8
2 5


Sample output:
6

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))


class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  # build
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  # set value at position p (0-indexed)
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_right(self, p, v):  # apply operator from the right side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(dat[p], v)
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_left(self, p, v):  # apply operator from the left side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(v, dat[p])
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  # result on interval [l, r) (0-indexed)
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        vl = vr = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                vl = f(vl, dat[l])
                l += 1
            if r & 1:
                r -= 1
                vr = f(dat[r], vr)
            l >>= 1
            r >>= 1
        return f(vl, vr)


minr = min(r for l, r in LR)
lminr = max(l for l, r in LR if r == minr)
LR2 = [(lminr, minr)] + [lr for lr in LR if lr != (lminr, minr)]
M = len(LR2)
if M == 1:
    ans = 2 * (LR2[0][1] - LR2[0][0])
elif M == 2:
    ans = sum(r - l for l, r in LR)
else:
    st = SegmentTree([r for l, r in LR2], min, 10**10)
    ans = max(
        max(0,
            st.query(0, i) - max(lminr, LR2[i - 1][0])) +
        max(0,
            st.query(i, M) - LR2[-1][0]) for i in range(1, M))
    ans = max(
        ans,
        max(0, minr - LR[-1][0]) + max(0,
                                       st.query(1, M - 1) - LR[-2][0]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))


class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  # build
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  # set value at position p (0-indexed)
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_right(self, p, v):  # apply operator from the right side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(dat[p], v)
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_left(self, p, v):  # apply operator from the left side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(v, dat[p])
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  # result on interval [l, r) (0-indexed)
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        vl = vr = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                vl = f(vl, dat[l])
                l += 1
            if r & 1:
                r -= 1
                vr = f(dat[r], vr)
            l >>= 1
            r >>= 1
        return f(vl, vr)


minr = min(r for l, r in LR)
lminr = max(l for l, r in LR if r == minr)
LR2 = [(lminr, minr)] + [lr for lr in LR if lr != (lminr, minr)]
M = len(LR2)
if M == 1:
    ans = 2 * (LR2[0][1] - LR2[0][0])
else:
    st = SegmentTree([r for l, r in LR2], min, 10**10)
    ans = max(
        st.query(0, i) + st.query(i, M) - max(lminr, LR2[i - 1][0]) -
        LR2[-1][0] for i in range(1, M))
    if M > 2:
        ans = max(ans, minr - LR[-1][0] + st.query(1, M - 1) - LR[-2][0])
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))


class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  # build
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  # set value at position p (0-indexed)
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_right(self, p, v):  # apply operator from the right side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(dat[p], v)
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_left(self, p, v):  # apply operator from the left side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(v, dat[p])
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  # result on interval [l, r) (0-indexed)
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        vl = vr = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                vl = f(vl, dat[l])
                l += 1
            if r & 1:
                r -= 1
                vr = f(dat[r], vr)
            l >>= 1
            r >>= 1
        return f(vl, vr)


st = SegmentTree([r for l, r in LR], min, 10**10)
ans = max(
    st.query(0, i) + st.query(i, N) - LR[i - 1][0] - LR[-1][0]
    for i in range(1, N))
minr = min(r for l, r in LR)
lminr = max(l for l, r in LR if r == minr)
ans = max(ans, minr - lminr + max(r - l for l, r in LR[:-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))


class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  # build
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  # set value at position p (0-indexed)
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_right(self, p, v):  # apply operator from the right side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(dat[p], v)
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def operate_left(self, p, v):  # apply operator from the left side
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = f(v, dat[p])
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  # result on interval [l, r) (0-indexed)
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        vl = vr = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                vl = f(vl, dat[l])
                l += 1
            if r & 1:
                r -= 1
                vr = f(dat[r], vr)
            l >>= 1
            r >>= 1
        return f(vl, vr)


minr = min(r for l, r in LR)
lminr = max(l for l, r in LR if r == minr)
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
elif lminr == LR[-1][0]:
    ans = minr - lminr + max(r - l for l, r in LR[:-1])
else:
    st = SegmentTree([r for l, r in LR], min, 10**10)
    ans = max(
        st.query(0, i) + st.query(i, N) - LR[i - 1][0] - LR[-1][0]
        for i in range(1, N))
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最大のもの
    minr = min(r for l, r in LR)
    lminr = max(l for l, r in LR if r == minr)
    #Lが最大のもののなかでRが最小のもの
    maxl = max(l for l, r in LR)
    rmaxl = min(r for l, r in LR if l == maxl)
    ans = minr - maxl + max(r - l for l, r in [(0, 0)] + LR if (l, r) not in (
        (lminr, minr), (maxl, rmaxl)))

    class SegmentTree:
        def __init__(self, array, f, ti):
            """
            Parameters
            ----------
            array : list
                to construct segment tree from
            f : func
                binary operation of the monoid
            ti : 
                identity element of the monoid
            """
            self.f = f
            self.ti = ti
            self.n = n = 2**(len(array).bit_length())
            self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
            for i in range(n - 1, 0, -1):  # build
                dat[i] = f(dat[i << 1], dat[i << 1 | 1])

        def update(self, p, v):  # set value at position p (0-indexed)
            f, n, dat = self.f, self.n, self.dat
            p += n
            dat[p] = v
            while p > 1:
                p >>= 1
                dat[p] = f(dat[p << 1], dat[p << 1 | 1])

        def query(self, l, r):  # result on interval [l, r) (0-indexed)
            f, ti, n, dat = self.f, self.ti, self.n, self.dat
            vl = vr = ti
            l += n
            r += n
            while l < r:
                if l & 1:
                    vl = f(vl, dat[l])
                    l += 1
                if r & 1:
                    r -= 1
                    vr = f(dat[r], vr)
                l >>= 1
                r >>= 1
            return f(vl, vr)

    LR = [(l, r)
          for l, r in LR[:-1] if (l, r) != (lminr, minr)] + [(maxl, rmaxl)]
    M = len(LR)
    ST = SegmentTree([r for l, r in LR], min, 10**18)
    for i, lr in enumerate(LR[:-1]):
        l, r = lr
        l2 = max(l, lminr)
        r2 = ST.query(i + 1, M)
        ans = max(ans, r2 - maxl + minr - l2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最大のもの
    minr = min(r for l, r in LR)
    lminr = max(l for l, r in LR if r == minr)
    #Lが最大のもののなかでRが最小のもの
    maxl = max(l for l, r in LR)
    rmaxl = min(r for l, r in LR if l == maxl)
    if (lminr, minr) == (maxl, rmaxl):
        ans = minr - lminr
        ans += max(r - l for l, r in LR if (l, r) != (lminr, minr))
    else:
        class SegmentTree:
            def __init__(self, array, f, ti):
                """
                Parameters
                ----------
                array : list
                    to construct segment tree from
                f : func
                    binary operation of the monoid
                ti : 
                    identity element of the monoid
                """
                self.f = f
                self.ti = ti
                self.n = n = 2**(len(array).bit_length())
                self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
                for i in range(n - 1, 0, -1):  # build
                    dat[i] = f(dat[i << 1], dat[i << 1 | 1])
        
            def update(self, p, v):  # set value at position p (0-indexed)
                f, n, dat = self.f, self.n, self.dat
                p += n
                dat[p] = v
                while p > 1:
                    p >>= 1
                    dat[p] = f(dat[p << 1], dat[p << 1 | 1])
        
            def query(self, l, r):  # result on interval [l, r) (0-indexed)
                f, ti, n, dat = self.f, self.ti, self.n, self.dat
                vl = vr = ti
                l += n
                r += n
                while l < r:
                    if l & 1:
                        vl = f(vl, dat[l])
                        l += 1
                    if r & 1:
                        r -= 1
                        vr = f(dat[r], vr)
                    l >>= 1
                    r >>= 1
                return f(vl, vr)
        
        LR = [(l, r) for l, r in LR[:-1] if (l, r) != (lminr, minr)] + [(maxl, rmaxl)]
        M = len(LR)
        ST = SegmentTree([r for l, r in LR], min, 10 ** 18)
        ans = ST.query(0, M) - maxl + minr - lminr
        for i, lr in enumerate(LR[:-1]):
            l, r = lr
            l2 = max(l, lminr)
            r2 = ST.query(i + 1, M)
            ans = max(ans, r2 - maxl + minr - l2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最小のもの
    minr = min(r for l, r in LR)
    lminr = 10 ** 10
    for l, r in LR:
        if r == minr:
            lminr = min(lminr, l)
    #Lが最大のもののなかでRが最大のもの
    maxl = max(l for l, r in LR)
    rmaxl = -1
    for l, r in LR:
        if l == maxl:
            rmaxl = max(rmaxl, r)
    if (lminr, minr) == (maxl, rmaxl):
        ans = minr - lminr
        ans += max(r - l for l, r in LR if (l, r) != (lminr, minr))
    else:
        class SegmentTree:
            def __init__(self, array, f, ti):
                """
                Parameters
                ----------
                array : list
                    to construct segment tree from
                f : func
                    binary operation of the monoid
                ti : 
                    identity element of the monoid
                """
                self.f = f
                self.ti = ti
                self.n = n = 2**(len(array).bit_length())
                self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
                for i in range(n - 1, 0, -1):  # build
                    dat[i] = f(dat[i << 1], dat[i << 1 | 1])
        
            def update(self, p, v):  # set value at position p (0-indexed)
                f, n, dat = self.f, self.n, self.dat
                p += n
                dat[p] = v
                while p > 1:
                    p >>= 1
                    dat[p] = f(dat[p << 1], dat[p << 1 | 1])
        
            def query(self, l, r):  # result on interval [l, r) (0-indexed)
                f, ti, n, dat = self.f, self.ti, self.n, self.dat
                vl = vr = ti
                l += n
                r += n
                while l < r:
                    if l & 1:
                        vl = f(vl, dat[l])
                        l += 1
                    if r & 1:
                        r -= 1
                        vr = f(dat[r], vr)
                    l >>= 1
                    r >>= 1
                return f(vl, vr)
        
        LR = [(l, r) for l, r in LR if (l, r) not in ((lminr, minr), (maxl, rmaxl))] + [(maxl, rmaxl)]
        M = len(LR)
        ST = SegmentTree([r for l, r in LR], min, 10 ** 18)
        ans = ST.query(0, M) - maxl + minr - lminr
        for i, lr in enumerate(LR[:-1]):
            l, r = lr
            l2 = max(l, lminr)
            r2 = ST.query(i + 1, M)
            ans = max(ans, r2 - maxl + minr - l2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最小のもの
    minr = min(r for l, r in LR)
    lminr = 10 ** 10
    for l, r in LR:
        if r == minr:
            lminr = min(lminr, l)
    #Lが最大のもののなかでRが最大のもの
    maxl = max(l for l, r in LR)
    rmaxl = -1
    for l, r in LR:
        if l == maxl:
            rmaxl = max(rmaxl, r)
    if (lminr, minr) == (maxl, rmaxl):
        ans = minr - lminr
        ans += max(r - l for l, r in LR if (l, r) != (lminr, minr))
    else:
        class SegmentTree:
            def __init__(self, array, f, ti):
                """
                Parameters
                ----------
                array : list
                    to construct segment tree from
                f : func
                    binary operation of the monoid
                ti : 
                    identity element of the monoid
                """
                self.f = f
                self.ti = ti
                self.n = n = 2**(len(array).bit_length())
                self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
                for i in range(n - 1, 0, -1):  # build
                    dat[i] = f(dat[i << 1], dat[i << 1 | 1])
        
            def update(self, p, v):  # set value at position p (0-indexed)
                f, n, dat = self.f, self.n, self.dat
                p += n
                dat[p] = v
                while p > 1:
                    p >>= 1
                    dat[p] = f(dat[p << 1], dat[p << 1 | 1])
        
            def query(self, l, r):  # result on interval [l, r) (0-indexed)
                f, ti, n, dat = self.f, self.ti, self.n, self.dat
                vl = vr = ti
                l += n
                r += n
                while l < r:
                    if l & 1:
                        vl = f(vl, dat[l])
                        l += 1
                    if r & 1:
                        r -= 1
                        vr = f(dat[r], vr)
                    l >>= 1
                    r >>= 1
                return f(vl, vr)
        
        LR = [(l, r) for l, r in LR if (l, r) != (lminr, minr)]
        M = len(LR)
        ST = SegmentTree([r for l, r in LR], min, 10 ** 18)
        ans = ST.query(0, M) - maxl + minr - lminr
        for i, lr in enumerate(LR[:-1]):
            l, r = lr
            l2 = max(l, lminr)
            r2 = ST.query(i + 1, M)
            ans = max(ans, r2 - maxl + minr - l2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最小のもの
    minr = min(r for l, r in LR)
    lminr = 10 ** 10
    for l, r in LR:
        if r == minr:
            lminr = min(lminr, l)
    #Lが最大のもののなかでRが最大のもの
    maxl = max(l for l, r in LR)
    rmaxl = -1
    for l, r in LR:
        if l == maxl:
            rmaxl = max(rmaxl, r)
    if (lminr, minr) == (maxl, rmaxl):
        ans = minr - lminr
        ans += max(r - l for l, r in LR if (l, r) != (lminr, minr))
    else:
        class SegmentTree:
            def __init__(self, array, f, ti):
                """
                Parameters
                ----------
                array : list
                    to construct segment tree from
                f : func
                    binary operation of the monoid
                ti : 
                    identity element of the monoid
                """
                self.f = f
                self.ti = ti
                self.n = n = 2**(len(array).bit_length())
                self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
                for i in range(n - 1, 0, -1):  # build
                    dat[i] = f(dat[i << 1], dat[i << 1 | 1])
        
            def update(self, p, v):  # set value at position p (0-indexed)
                f, n, dat = self.f, self.n, self.dat
                p += n
                dat[p] = v
                while p > 1:
                    p >>= 1
                    dat[p] = f(dat[p << 1], dat[p << 1 | 1])
        
            def query(self, l, r):  # result on interval [l, r) (0-indexed)
                f, ti, n, dat = self.f, self.ti, self.n, self.dat
                vl = vr = ti
                l += n
                r += n
                while l < r:
                    if l & 1:
                        vl = f(vl, dat[l])
                        l += 1
                    if r & 1:
                        r -= 1
                        vr = f(dat[r], vr)
                    l >>= 1
                    r >>= 1
                return f(vl, vr)
        
        LR = [(l, r) for l, r in LR if (l, r) != (lminr, minr)]
        M = len(LR)
        ST = SegmentTree([r for l, r in LR], min, 10 ** 18)
        ans = ST.query(0, M + 1) - maxl + minr - lminr
        for i, lr in enumerate(LR[:-1]):
            l, r = lr
            l2 = max(l, lminr)
            r2 = ST.query(i + 1, M + 1)
            ans = max(ans, r2 - maxl + minr - l2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最小のもの
    minr = min(r for l, r in LR)
    lminr = 10 ** 10
    for l, r in LR:
        if r == minr:
            lminr = min(lminr, l)
    #Lが最大のもののなかでRが最大のもの
    maxl = max(l for l, r in LR)
    rmaxl = -1
    for l, r in LR:
        if l == maxl:
            rmaxl = max(rmaxl, r)
    if (lminr, minr) == (maxl, rmaxl):
        ans = minr - lminr
        ans += max(r - l for l, r in LR if (l, r) != (lminr, minr))
    else:
        class SegmentTree:
            def __init__(self, array, f, ti):
                """
                Parameters
                ----------
                array : list
                    to construct segment tree from
                f : func
                    binary operation of the monoid
                ti : 
                    identity element of the monoid
                """
                self.f = f
                self.ti = ti
                self.n = n = 2**(len(array).bit_length())
                self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
                for i in range(n - 1, 0, -1):  # build
                    dat[i] = f(dat[i << 1], dat[i << 1 | 1])
        
            def update(self, p, v):  # set value at position p (0-indexed)
                f, n, dat = self.f, self.n, self.dat
                p += n
                dat[p] = v
                while p > 1:
                    p >>= 1
                    dat[p] = f(dat[p << 1], dat[p << 1 | 1])
        
            def query(self, l, r):  # result on interval [l, r) (0-indexed)
                f, ti, n, dat = self.f, self.ti, self.n, self.dat
                vl = vr = ti
                l += n
                r += n
                while l < r:
                    if l & 1:
                        vl = f(vl, dat[l])
                        l += 1
                    if r & 1:
                        r -= 1
                        vr = f(dat[r], vr)
                    l >>= 1
                    r >>= 1
                return f(vl, vr)
        
        LR = [(l, r) for l, r in LR if (l, r) != (lminr, minr)]
        M = len(LR)
        ST = SegmentTree([r for l, r in LR], min, 10 ** 18)
        ans = -10 ** 10
        for i, lr in enumerate(LR[:-1]):
            l, r = lr
            l2 = max(l, lminr)
            r2 = ST.query(i + 1, M + 1)
            ans = max(ans, r2 - l2)
        ans += minr - maxl
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最小のもの
    minr = min(r for l, r in LR)
    minl = 10 ** 10
    for l, r in LR:
        if r == minr:
            minl = min(minl, l)
    #Lが最大のもののなかでRが最大のもの
    maxl = max(l for l, r in LR)
    maxr = -1
    for l, r in LR:
        if l == maxl:
            maxr = max(maxr, r)
    if (minl, minr) == (maxl, maxr):
        ans = minr - minl
        ans += max(r - l for l, r in LR if (l, r) != (minl, minr))
    else:
        class SegmentTree:
            def __init__(self, array, f, ti):
                """
                Parameters
                ----------
                array : list
                    to construct segment tree from
                f : func
                    binary operation of the monoid
                ti : 
                    identity element of the monoid
                """
                self.f = f
                self.ti = ti
                self.n = n = 2**(len(array).bit_length())
                self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
                for i in range(n - 1, 0, -1):  # build
                    dat[i] = f(dat[i << 1], dat[i << 1 | 1])
        
            def update(self, p, v):  # set value at position p (0-indexed)
                f, n, dat = self.f, self.n, self.dat
                p += n
                dat[p] = v
                while p > 1:
                    p >>= 1
                    dat[p] = f(dat[p << 1], dat[p << 1 | 1])
        
            def query(self, l, r):  # result on interval [l, r) (0-indexed)
                f, ti, n, dat = self.f, self.ti, self.n, self.dat
                vl = vr = ti
                l += n
                r += n
                while l < r:
                    if l & 1:
                        vl = f(vl, dat[l])
                        l += 1
                    if r & 1:
                        r -= 1
                        vr = f(dat[r], vr)
                    l >>= 1
                    r >>= 1
                return f(vl, vr)
        
        LR = [(l, r) for l, r in LR if (l, r) != (minl, minr)]
        M = len(LR)
        ST = SegmentTree([r for l, r in LR], min, 10 ** 18)
        ans = -10 ** 10
        for i, lr in enumerate(LR[:-1]):
            l, r = lr
            if (l, r) == (minl, minr):
                continue
            r2 = ST.query(i + 1, M + 1)
            ans = max(ans, r2 - l)
        ans += minr - maxl
print(ans)

Submission status: Wrong Answer

Solution code:
N, *LR = [int(_) for _ in open(0).read().split()]
LR = sorted(set([(l, r + 1) for l, r in zip(LR[::2], LR[1::2])]))
if len(LR) == 1:
    l, r = LR.pop()
    ans = 2 * (r - l)
else:
    #Rが最小のもののなかでLが最小のもの
    minr = min(r for l, r in LR)
    minl = 10 ** 10
    for l, r in LR:
        if r == minr:
            minl = min(minl, l)
    #Lが最大のもののなかでRが最大のもの
    maxl = max(l for l, r in LR)
    maxr = -1
    for l, r in LR:
        if l == maxl:
            maxr = max(maxr, r)
    if (minl, minr) == (maxl, maxr):
        ans = minr - minl
        ans += max(r - l for l, r in LR if (l, r) != (minl, minr))
    else:
        class SegmentTree:
            def __init__(self, array, f, ti):
                """
                Parameters
                ----------
                array : list
                    to construct segment tree from
                f : func
                    binary operation of the monoid
                ti : 
                    identity element of the monoid
                """
                self.f = f
                self.ti = ti
                self.n = n = 2**(len(array).bit_length())
                self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
                for i in range(n - 1, 0, -1):  # build
                    dat[i] = f(dat[i << 1], dat[i << 1 | 1])
        
            def update(self, p, v):  # set value at position p (0-indexed)
                f, n, dat = self.f, self.n, self.dat
                p += n
                dat[p] = v
                while p > 1:
                    p >>= 1
                    dat[p] = f(dat[p << 1], dat[p << 1 | 1])
        
            def query(self, l, r):  # result on interval [l, r) (0-indexed)
                f, ti, n, dat = self.f, self.ti, self.n, self.dat
                vl = vr = ti
                l += n
                r += n
                while l < r:
                    if l & 1:
                        vl = f(vl, dat[l])
                        l += 1
                    if r & 1:
                        r -= 1
                        vr = f(dat[r], vr)
                    l >>= 1
                    r >>= 1
                return f(vl, vr)
        
        M = len(LR)
        ST = SegmentTree([r for l, r in LR], min, 10 ** 18)
        ans = -10 ** 10
        for i, lr in enumerate(LR[:-1]):
            l, r = lr
            r2 = ST.query(i + 1, M + 1)
            ans = max(ans, r2 - l)
        ans += minr - maxl
print(ans)