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:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



N=int(input())
LR=[list(map(int,input().split())) for i in range(N)]
for i in range(N):
    LR[i][1]+=1

value=max([R-L for L,R in LR])

LR=sorted(LR,key=lambda x:x[0])
a=2*(10**10)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]

for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



N=int(input())
LR=[list(map(int,input().split())) for i in range(N)]
for i in range(N):
    LR[i][1]+=1
#value=max([R-L for L,R in LR])
value=0
LR=sorted(LR,key=lambda x:x[0])
minRlst=[2*(10**10) for i in range(N)]
minRlst[-1]=LR[-1][1]
for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1,R1=L,min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



N=int(input())
LR=[list(map(int,input().split())) for i in range(N)]
for i in range(N):
    LR[i][1]+=1
value=max([R-L for L,R in LR])
#LR=sorted(LR,key=lambda x:x[0])
minRlst=[2*(10**10) for i in range(N)]
minRlst[-1]=LR[-1][1]
for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1,R1=L,min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



N=int(input())
LR=[list(map(int,input().split())) for i in range(N)]
for i in range(N):
    LR[i][1]+=1
value=max([R-L for L,R in LR])
LR=sorted(LR,key=lambda x:x[0])
minRlst=[2*(10**10) for i in range(N)]
minRlst[-1]=LR[-1][1]
for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1,R1=L,min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



N=int(input())
LR=[list(map(int,input().split())) for i in range(N)]
value=max([R-L for L,R in LR])+1
LR=sorted(LR,key=lambda x:x[0])




minRlst=[2*(10**10) for i in range(N)]
minRlst[-1]=LR[-1][1]
for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])


L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1,R1=L,min(R1,R)
    R2=minRlst[i+1]
    a=max(R1+1-L1,0)
    b=max(R2+1-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



N=int(input())
LR=[list(map(int,input().split())) for i in range(N)]
value=max([R-L for L,R in LR])+1
LR=sorted(LR,key=lambda x:x[0])

L1,R1=LR[0]
L2,R2=LR[1]
for i in range(2,N):
    L,R=LR[i]
    L2=L
    R2=min(R2,R)
a=max(R1-L1+1,0)
b=max(R2-L2+1,0)
value=max(value,a+b)

#table=[[0 for i in range(logN)] for n in range(N)]
minRlst=[2*(10**10) for i in range(N)]
minRlst[-1]=LR[-1][1]
for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
for i in range(1,N-1):
    L,R=LR[i]
    L1,R1=L,min(R1,R)
    R2=minRlst[i+1]
    a=max(R1+1-L1,0)
    b=max(R2+1-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
def numI(x):
    if len(x)==0:
        return 0
    L = x[0]
    R = x[1]
    return R-L+1
def iS(J1,J2):
    if len(J1)==0 or len(J2)==0:
        return []
    L1 = J1[0]
    R1 = J1[1]
    L2 = J2[0]
    R2 = J2[1]
    if R1<L2 or R2<L1:
        return []
    else:
        return [max(L1,L2),min(R1,R2)]
N = int(input())
A = [list(map(int,input().split())) for _ in range(N)]
I1 = A[0]
I2 = A[1]
for i in range(2,N):
    J = A[i]
    a1 = numI(J)
    a2 = numI(iS(I1,I2))
    a = a1+a2
    b1 = numI(iS(I1,J))
    b2 = numI(I2)
    b = b1+b2
    c1 = numI(I1)
    c2 = numI(iS(I2,J))
    c = c1+c2
    if max(a,b,c)==a:
        I1 = iS(I1,I2)
        I2 = J[:]
    elif max(a,b,c)==b:
        I1 = iS(I1,J)
    else:
        I2 = iS(I2,J)
print(numI(I1)+numI(I2))

Submission status: Wrong Answer

Solution code:
N=int(input())

aL,aR=map(int,input().split())
bL,bR=map(int,input().split())
k=2
while k<N:
  cL,cR=map(int,input().split())
  X=min([aR,cR])-max([aL,cL])+bR-bL
  Y=min([bR,cR])-max([bL,cL])+aR-aL
  Z=min([aR,bR])-max([aL,bL])+cR-cL
  
  W=max([X,Y,Z])
  if X==W:
    aR=min([aR,cR])
    aL=max([aL,cL])
  elif Y==W:
    bR=min([bR,cR])
    bL=max([bL,cL])
  else:
    aR=min([aR,bR])
    aL=max([aL,bL])
    bR=cR
    bL=cL
  k+=1
print(aR+bR-aL-bL+2)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from operator import add
N = int(input())
LR = [tuple(map(int, input().split())) for _ in range(N)]
LR.sort()
f = lambda lr1, lr2: (max(lr1[0], lr2[0]), min(lr1[1], lr2[1]))
g = lambda lr: max(0, lr[1]-lr[0]+1)
A = accumulate(LR, f)
A = list(map(g, A))[:-1]
B = accumulate(LR[::-1], f)
B = list(map(g, B))[-2::-1]
print(max(map(add, A, B)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=[list(map(int, input().split(' '))) for i in range(n)]
a.sort()
fnr_l = [a[0]]
fnr_r = [a[n-1]]
for i in range(1,n-1):
    l_l = a[i][0]
    l_r = min(fnr_l[-1][1],a[i][1])
    fnr_l.append([l_l,l_r])
    r_l = fnr_r[0][0]
    r_r = min(fnr_r[-1][1],a[-i-1][1])
    fnr_r.append([r_l,r_r])
ans=[]
for i in range(n-1):
    ans.append(max(0,fnr_l[i][1]-fnr_l[i][0]+1)+max(0,fnr_r[-i-1][1]-fnr_r[-i-1][0]+1))
print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=[list(map(int, input().split(' '))) for i in range(n)]
a.sort()
fnr_l = [a[0]]
fnr_r = [a[n-1]]
for i in range(1,n-1):
    l_l = a[i][0]
    l_r = min(fnr_l[-1][1],a[i][1])
    fnr_l.append([l_l,l_r])
    r_l = fnr_r[0][0]
    r_r = min(fnr_r[-1][1],a[-i-1][1])
    fnr_r.append([r_l,r_r])
ans=[]
for i in range(n-1):
    ans.append(max(0,fnr_l[i][1]-fnr_l[i][0])+max(0,fnr_r[-i-1][1]-fnr_r[-i-1][0])+2)
print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=[list(map(int, input().split(' '))) for i in range(n)]
a.sort()
fnr_l = [a[0]]
fnr_r = [a[n-1]]
for i in range(1,n-1):
    l_l = a[i][0]
    l_r = min(fnr_l[-1][1],a[i][1])
    fnr_l.append([l_l,l_r])
    r_l = fnr_r[0][0]
    r_r = min(fnr_r[-1][1],a[-i-1][1])
    fnr_r.append([r_l,r_r])
ans=[]
for i in range(n-1):
    ans.append(fnr_l[i][1]-fnr_l[i][0]+fnr_r[-i-1][1]-fnr_r[-i-1][0]+2)
print(max(ans))

Submission status: Wrong Answer

Solution code:
def main():
  n,*t=map(int,open(0).read().split())
  (x,y),*t,(a,b)=sorted(zip(*[iter(t)]*2))
  r_min=[b]
  l_max=[a]
  for l,r in t[::-1]:
    r_min+=min(r_min[-1],r),
    l_max+=max(l_max[-1],l),
  m=y-x+r_min.pop()-l_max.pop()+2
  for l,r in t:
    i=r_min.pop()
    j=l_max.pop()
    v=min(y,r)
    w=max(x,l)
    m=max(m,i-j+v-w+2,min(i,y)-max(j,x)+r-l+2)
    y,x=v,w
  m=max(m,b-a+y-x+2,0)
  print(m)
main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    ans = 0
    min_ri = (10 ** 9 + 1, -1)
    max_li = (-1, -1)
    for i, (l, r) in enumerate(ranges):
        min_ri = min(min_ri, (r, i))
        max_li = max(max_li, (l, i))
    ri = min_ri[1]
    li = max_li[1]
    subranges = [ranges[i] for i in range(n) if i != ri and i != li]
    for r in subranges:
        ans = max(ans, min_ri[0] - max_li[0] + r[1] - r[0] + 2)
    if ri != li:
        n = len(subranges)
        leftlmax = [ranges[ri][0]] * (n + 1)
        leftrmin = [ranges[ri][1]] * (n + 1)
        rightlmax = [ranges[li][0]] * (n + 1)
        rightrmin = [ranges[li][1]] * (n + 1)
        for i in range(n):
            leftlmax[i + 1] = max(leftlmax[i], subranges[i][0])
            leftrmin[i + 1] = min(leftrmin[i], subranges[i][1])
        for i in range(n - 1, -1, -1):
            rightlmax[i] = max(rightlmax[i + 1], subranges[i][0])
            rightrmin[i] = min(rightrmin[i + 1], subranges[i][1])
        for i in range(0, n):
            left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
            right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
            ans = max(ans, left + right)
    return ans


def main():
    n = int(input())
    ranges = []
    ans = 0
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
        ans = max(ans, r - l + 1)
    ranges.sort(key=lambda tup: (tup[0], -tup[1]))
    ans = max(ans, solve(ranges))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    ans = 0
    min_ri = (10 ** 9 + 1, -1)
    max_li = (-1, -1)
    for i, (l, r) in enumerate(ranges):
        min_ri = min(min_ri, (r, i))
        max_li = max(max_li, (l, i))
    if min_ri[1] == max_li[1]:
        for i in range(n):
            if i == min_ri[1]:
                continue
            ans = max(ans, min_ri[0] - max_li[0] + ranges[i][1] - ranges[i][0] + 2)
    else:
        ri = min_ri[1]
        li = max_li[1]
        subranges = [ranges[i] for i in range(n) if i != ri and i != li]
        n = len(subranges)
        leftlmax = [ranges[ri][0]] * (n + 1)
        leftrmin = [ranges[ri][1]] * (n + 1)
        rightlmax = [ranges[li][0]] * (n + 1)
        rightrmin = [ranges[li][1]] * (n + 1)
        for i in range(n):
            leftlmax[i + 1] = max(leftlmax[i], subranges[i][0])
            leftrmin[i + 1] = min(leftrmin[i], subranges[i][1])
        for i in range(n - 1, -1, -1):
            rightlmax[i] = max(rightlmax[i + 1], subranges[i][0])
            rightrmin[i] = min(rightrmin[i + 1], subranges[i][1])
        ans = 0
        for i in range(0, n):
            left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
            right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
            ans = max(ans, left + right)
    return ans


def main():
    n = int(input())
    ranges = []
    ans = 0
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
        ans = max(ans, r - l + 1)
    ranges.sort(key=lambda tup: (tup[0], -tup[1]))
    ans = max(ans, solve(ranges))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    leftlmax = [0] * (n + 1)
    leftrmin = [10 ** 9] * (n + 1)
    rightlmax = [0] * (n + 1)
    rightrmin = [10 ** 9] * (n + 1)
    for i in range(n):
        leftlmax[i + 1] = max(leftlmax[i], ranges[i][0])
        leftrmin[i + 1] = min(leftrmin[i], ranges[i][1])
    for i in range(n - 1, -1, -1):
        rightlmax[i] = max(rightlmax[i + 1], ranges[i][0])
        rightrmin[i] = min(rightrmin[i + 1], ranges[i][1])
    ans = 0
    for i in range(0, n - 1):
        left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
        right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
        ans = max(ans, left + right)
        # print(i, left, right, left + right)
    return ans


def main():
    n = int(input())
    ranges = []
    ans = 0
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
        ans = max(ans, r - l + 1)
    ranges.sort(key=lambda tup: (tup[0], -tup[1]))
    ans = max(ans, solve(ranges))
    revranges = []
    maxv = 10 ** 9 + 1
    for i in range(n):
        l, r = ranges[n - 1 - i]
        revranges.append((maxv - r, maxv - l))
    revranges.sort(key=lambda tup: (tup[0], -tup[1]))
    ans = max(ans, solve(revranges))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    ans = 0
    min_ri = (10 ** 9 + 1, -1)
    max_li = (-1, -1)
    for i, (l, r) in enumerate(ranges):
        min_ri = min(min_ri, (r, i))
        max_li = max(max_li, (l, i))
    if min_ri[1] == max_li[1]:
        for i in range(n):
            if i == min_ri[1]:
                continue
            ans = max(ans, min_ri[0] - max_li[0] + ranges[i][1] - ranges[i][0] + 2)
    else:
        ri = min_ri[1]
        li = max_li[1]
        subranges = [ranges[i] for i in range(n) if i != ri and i != li]
        n = len(subranges)
        leftlmax = [ranges[ri][0]] * (n + 1)
        leftrmin = [ranges[ri][1]] * (n + 1)
        rightlmax = [ranges[li][0]] * (n + 1)
        rightrmin = [ranges[li][1]] * (n + 1)
        for i in range(n):
            leftlmax[i + 1] = max(leftlmax[i], subranges[i][0])
            leftrmin[i + 1] = min(leftrmin[i], subranges[i][1])
        for i in range(n - 1, -1, -1):
            rightlmax[i] = max(rightlmax[i + 1], subranges[i][0])
            rightrmin[i] = min(rightrmin[i + 1], subranges[i][1])
        ans = 0
        for i in range(0, n):
            left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
            right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
            ans = max(ans, left + right)
    return ans


def main():
    n = int(input())
    ranges = []
    ans = 0
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
        ans = max(ans, r - l + 1)
    ranges.sort(key=lambda tup: (tup[0], tup[1]))
    ans = max(ans, solve(ranges))
    revranges = []
    maxv = 10 ** 9 + 1
    for i in range(n):
        l, r = ranges[n - 1 - i]
        r = -r
        revranges.append((maxv - r, -(maxv - l)))
    ans = max(ans, solve(revranges))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    ans = 0
    min_ri = (10 ** 9 + 1, -1)
    max_li = (-1, -1)
    for i, (l, r) in enumerate(ranges):
        min_ri = min(min_ri, (r, i))
        max_li = max(max_li, (l, i))
    if min_ri[1] == max_li[1]:
        for i in range(n):
            if i == min_ri[1]:
                continue
            ans = max(ans, min_ri[0] - max_li[0] + ranges[i][1] - ranges[i][0] + 2)
    else:
        ri = min_ri[1]
        li = max_li[1]
        subranges = [ranges[i] for i in range(n) if i != ri and i != li]
        n = len(subranges)
        leftlmax = [ranges[ri][0]] * (n + 1)
        leftrmin = [ranges[ri][1]] * (n + 1)
        rightlmax = [ranges[li][0]] * (n + 1)
        rightrmin = [ranges[li][1]] * (n + 1)
        for i in range(n):
            leftlmax[i + 1] = max(leftlmax[i], subranges[i][0])
            leftrmin[i + 1] = min(leftrmin[i], subranges[i][1])
        for i in range(n - 1, -1, -1):
            rightlmax[i] = max(rightlmax[i + 1], subranges[i][0])
            rightrmin[i] = min(rightrmin[i + 1], subranges[i][1])
        ans = 0
        for i in range(0, n):
            left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
            right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
            ans = max(ans, left + right)
    return ans


def main():
    n = int(input())
    ranges = []
    ans = 0
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
        ans = max(ans, r - l + 1)
    ranges.sort()
    ans = max(ans, solve(ranges))
    revranges = []
    maxv = 10 ** 9 + 1
    for i in range(n):
        l, r = ranges[n - 1 - i]
        r = -r
        revranges.append((maxv - r, -(maxv - l)))
    ans = max(ans, solve(revranges))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    leftlmax = [0] * (n + 1)
    leftrmin = [10 ** 9] * (n + 1)
    rightlmax = [0] * (n + 1)
    rightrmin = [10 ** 9] * (n + 1)
    for i in range(n):
        leftlmax[i + 1] = max(leftlmax[i], ranges[i][0])
        leftrmin[i + 1] = min(leftrmin[i], -ranges[i][1])
    for i in range(n - 1, -1, -1):
        rightlmax[i] = max(rightlmax[i + 1], ranges[i][0])
        rightrmin[i] = min(rightrmin[i + 1], -ranges[i][1])
    ans = 0
    for i in range(0, n - 1):
        left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
        right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
        ans = max(ans, left + right)
        # print(i, left, right, left + right)
    return ans


def main():
    n = int(input())
    ranges = []
    ans = 0
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, -r))
        ans = max(ans, r - l + 1)
    ranges.sort()
    ans = max(ans, solve(ranges))
    revranges = []
    maxv = 10 ** 9 + 1
    for i in range(n):
        l, r = ranges[n - 1 - i]
        r = -r
        revranges.append((maxv - r, -(maxv - l)))
    ans = max(ans, solve(revranges))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    leftlmax = [0] * (n + 1)
    leftrmin = [10 ** 9] * (n + 1)
    rightlmax = [0] * (n + 1)
    rightrmin = [10 ** 9] * (n + 1)
    for i in range(n):
        leftlmax[i + 1] = max(leftlmax[i], ranges[i][0])
        leftrmin[i + 1] = min(leftrmin[i], ranges[i][1])
    for i in range(n - 1, -1, -1):
        rightlmax[i] = max(rightlmax[i + 1], ranges[i][0])
        rightrmin[i] = min(rightrmin[i + 1], ranges[i][1])
    ans = 0
    for i in range(0, n - 1):
        left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
        right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
        ans = max(ans, left + right)
        # print(i, left, right, left + right)
    return ans

def main():
    n = int(input())
    ranges = []
    ans = 0
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
        ans = max(ans, r - l + 1)
    ranges.sort()
    ans = max(ans, solve(ranges))
    revranges = []
    maxv = 10 ** 9 + 1
    for i in range(n):
        l, r = ranges[n - 1 - i]
        revranges.append((maxv - r, maxv - l))
    ans = max(ans, solve(revranges))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(ranges):
    n = len(ranges)
    leftlmax = [0] * (n + 1)
    leftrmin = [10 ** 9] * (n + 1)
    rightlmax = [0] * (n + 1)
    rightrmin = [10 ** 9] * (n + 1)
    for i in range(n):
        leftlmax[i + 1] = max(leftlmax[i], ranges[i][0])
        leftrmin[i + 1] = min(leftrmin[i], ranges[i][1])
    for i in range(n - 1, -1, -1):
        rightlmax[i] = max(rightlmax[i + 1], ranges[i][0])
        rightrmin[i] = min(rightrmin[i + 1], ranges[i][1])
    ans = 0
    for i in range(0, n - 1):
        left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
        right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
        ans = max(ans, left + right)
        # print(i, left, right, left + right)
    return ans

def main():
    n = int(input())
    ranges = []
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
    ranges.sort()
    ans = solve(ranges)
    revranges = []
    maxv = 10 ** 9 + 1
    for i in range(n):
        l, r = ranges[n - 1 - i]
        revranges.append((maxv - r, maxv - l))
    ans = max(ans, solve(revranges))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    ranges = []
    for i in range(n):
        l, r = map(int, input().split())
        ranges.append((l, r))
    ranges.sort()
    leftlmax = [0] * (n + 1)
    leftrmin = [10 ** 9] * (n + 1)
    rightlmax = [0] * (n + 1)
    rightrmin = [10 ** 9] * (n + 1)
    for i in range(n):
        leftlmax[i + 1] = max(leftlmax[i], ranges[i][0])
        leftrmin[i + 1] = min(leftrmin[i], ranges[i][1])
    for i in range(n - 1, -1, -1):
        rightlmax[i] = max(rightlmax[i + 1], ranges[i][0])
        rightrmin[i] = min(rightrmin[i + 1], ranges[i][1])
    ans = 0
    for i in range(0, n - 1):
        left = max(0, leftrmin[i + 1] - leftlmax[i + 1] + 1)
        right = max(0, rightrmin[i + 1] - rightlmax[i + 1] + 1)
        ans = max(ans, left + right)
        # print(i, left, right, left + right)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[int(i) for i in input().split()] for _ in range(N)]
con1 = [0]*2
con2 = [0]*2

# Lの最小値を求める
MIN = float("inf")
MIN_INDEX = 0
for i,x in enumerate(LR):
    if x[0] < MIN:
        MIN = x[0]
        MIN_INDEX = i

con1 = LR.pop(MIN_INDEX)

MIN = float("inf")
# LR[MIN_INDEX]との共通領域の最小を求める
for i,x in enumerate(LR):
    common_range = min(x[1], con1[1]) - max(x[0], con1[0]) + 1
    if common_range <= 0:
        MIN_INDEX = i
        break
    else:
        if common_range < MIN:
            MIN = common_range
            MIN_INDEX = i

con2 = LR.pop(MIN_INDEX)

for i,x in enumerate(LR):
    common_range1 = min(x[1], con1[1]) - max(x[0], con1[0]) + 1
    if common_range1 <= 0:
        con1[0] = max(con1[0], x[0])
        con1[1] = min(con1[1], x[1])
        continue
    common_range2 = min(x[1], con2[1]) - max(x[0], con2[0]) + 1
    if common_range2 <= 0:
        con2[0] = max(con2[0], x[0])
        con2[1] = min(con2[1], x[1])
        continue

    # 変化量
    change1 = abs(con1[0] - max(x[0], con1[0])) + abs(con1[1] - min(x[1], con1[1]))
    change2 = abs(con2[0] - max(x[0], con2[0])) + abs(con2[1] - min(x[1], con2[1]))
    if change1 <= change2:
        con1[0] = max(con1[0], x[0])
        con1[1] = min(con1[1], x[1])
    else:
        con2[0] = max(con2[0], x[0])
        con2[1] = min(con2[1], x[1])

print(max((con2[1]-con2[0]+1)+(con1[1]-con1[0]+1),0))

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[int(i) for i in input().split()] for _ in range(N)]
con1 = [0]*2
con2 = [0]*2

# Lの最小値を求める
MIN = float("inf")
MIN_INDEX = 0
for i,x in enumerate(LR):
    if x[0] < MIN:
        MIN = x[0]
        MIN_INDEX = i

con1 = LR.pop(MIN_INDEX)

MIN = float("inf")
# LR[MIN_INDEX]との共通領域の最小を求める
for i,x in enumerate(LR):
    common_range = min(x[1], con1[1]) - max(x[0], con1[0]) + 1
    if common_range <= 0:
        MIN_INDEX = i
        break
    else:
        if common_range < MIN:
            MIN = common_range
            MIN_INDEX = i

con2 = LR.pop(MIN_INDEX)

for i,x in enumerate(LR):
    common_range1 = min(x[1], con1[1]) - max(x[0], con1[0]) + 1
    if common_range1 <= 0:
        con1[0] = max(con1[0], x[0])
        con1[1] = min(con1[1], x[1])
        continue
    common_range2 = min(x[1], con2[1]) - max(x[0], con2[0]) + 1
    if common_range2 <= 0:
        con2[0] = max(con2[0], x[0])
        con2[1] = min(con2[1], x[1])
        continue

    # 変化量
    change1 = abs(con1[0] - max(x[0], con1[0])) + abs(con1[1] - min(x[1], con1[1]))
    change2 = abs(con2[0] - max(x[0], con2[0])) + abs(con2[1] - min(x[1], con2[1]))
    if change1 <= change2:
        con1[0] = max(con1[0], x[0])
        con1[1] = min(con1[1], x[1])
    else:
        con2[0] = max(con2[0], x[0])
        con2[1] = min(con2[1], x[1])

print((con2[1]-con2[0]+1)+(con1[1]-con1[0]+1))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
LR = [[int(i) for i in input().split()] for _ in range(N)]

first = [LR[0][0],LR[0][1]]
two = [0,0]
index = 0
MINRANGE = float("inf")
for i,x in enumerate(LR):
    if i == 0:
        continue
    
    RANGE = min(first[1], x[1]) - max(first[0], x[0])
    if RANGE <= 0:
        two[0] = x[0]
        two[1] = x[1]
        index = i
        break
    else:
        if MINRANGE > RANGE:
            MINRANGE = RANGE
            index = i
            two[0] = x[0]
            two[1] = x[1]

LR.pop(index)
LR = LR[1:]

for i,x in enumerate(LR):
    RANGE1 = min(first[1], x[1]) - max(first[0], x[0])
    RANGE2 = min(two[1], x[1]) - max(two[0], x[0])

    if RANGE1 <= 0:
        two[0] = max(two[0], x[0])
        two[1] = min(two[1], x[1])
    elif RANGE2 <= 0:
        first[0] = max(first[0], x[0])
        first[1] = min(first[1], x[1])
    else:
        # 損失の大きさ
        lost1 = abs(first[1]-min(first[1], x[1])) + abs(first[0]-max(first[0], x[0]))
        lost2 = abs(two[1]-min(two[1], x[1])) + abs(two[0]-max(two[0], x[0]))
        if lost1 <= lost2:
            first[0] = max(first[0], x[0])
            first[1] = min(first[1], x[1])
        else:
            two[0] = max(two[0], x[0])
            two[1] = min(two[1], x[1])

print(first[1]-first[0]+two[1]-two[0]+2)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import numpy as np


def solve(*args: str) -> str:
    n = int(args[0])
    LR = [tuple(map(int, a.split())) for a in args[1:]]
    L, R = zip(*LR)

    ret = 0

    p, q = np.argmax(L), np.argmin(R)
    lp, rq = L[p], R[q]
    if p == q:
        ret = max(0, 1+rq-lp) + max(0, 1+max(R)-min(L))
    else:
        AB = [[max(0, 1+r-lp), max(0, 1+rq-l)] for l, r in LR]
        AB.sort(key=lambda x: (x[0], -x[1]))
        A, B = map(list, zip(*AB))

        # for i in range(1, n):
        #     ret = max(ret, min(A[i:]) + min(B[:i]))
        for i in range(1, n):
            B[i] = min(B[i], B[i-1])
            A[-i-1] = min(A[-i-1], A[-i])
        ret = max(A[i+1]+B[i] for i in range(n-1))

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


from operator import itemgetter


# In[2]:


N = int(input())
LR = [[0, 0] for _ in range(N)]
for i in range(N):
    LR[i][0], LR[i][1] = map(int, input().split())


# In[3]:


maxL = 0
minR = 10 ** 9
for i in range(N):
    maxL = max(maxL, LR[i][0])
    minR = min(minR, LR[i][1])


# In[4]:


ans = 0


# In[5]:


def func(list):
    ret = []
    left = list[0][0]
    right = list[0][1]
    ret.append(right - left + 1)
    for i in range(1, len(list)):
        left = max(left, list[i][0])
        right = min(right, list[i][1])
        ret.append(right - left + 1)
    return ret


# In[6]:


LR.sort(key=itemgetter(1))
LR.sort(key=itemgetter(0))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[7]:


m = 0
for i in range(N - 1):
    m = max(m, list1[i] + list2[N - i - 2])
ans = max(ans, m)


# In[8]:


LR.sort(key=itemgetter(1), reverse=True)
LR.sort(key=itemgetter(0))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[9]:


m = 0
for i in range(N - 1):
    m = max(m, list1[i] + list2[N - i - 2])
ans = max(ans, m)


# In[10]:


LR.sort(key=itemgetter(0))
LR.sort(key=itemgetter(1))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[11]:


m = 0
for i in range(N - 1):
    m = max(m, list1[i] + list2[N - i - 2])
ans = max(ans, m)


# In[12]:


LR.sort(key=itemgetter(0), reverse=True)
LR.sort(key=itemgetter(1))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[13]:


m = 0
for i in range(N):
    m = max(m, LR[i][1] - LR[i][0] + 1)
ans = max(ans, m + minR - maxL + 1)


# In[14]:


print(ans)


# In[ ]:

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


from operator import itemgetter


# In[2]:


N = int(input())
LR = [[0, 0] for _ in range(N)]
for i in range(N):
    LR[i][0], LR[i][1] = map(int, input().split())


# In[5]:


ans = 0


# In[6]:


def func(list):
    ret = []
    left = list[0][0]
    right = list[0][1]
    ret.append(right - left + 1)
    for i in range(1, len(list)):
        left = max(left, list[i][0])
        right = min(right, list[i][1])
        ret.append(right - left + 1)
    return ret


# In[7]:


LR.sort(key=itemgetter(1))
LR.sort(key=itemgetter(0))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[8]:


m = 0
for i in range(N - 1):
    m = max(m, list1[i] + list2[N - i - 2])
ans = max(ans, m)


# In[ ]:


LR.sort(key=itemgetter(1), reverse=True)
LR.sort(key=itemgetter(0))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[ ]:


m = 0
for i in range(N - 1):
    m = max(m, list1[i] + list2[N - i - 2])
ans = max(ans, m)


# In[9]:


LR.sort(key=itemgetter(0))
LR.sort(key=itemgetter(1))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[1]:


m = 0
for i in range(N - 1):
    m = max(m, list1[i] + list2[N - i - 2])
ans = max(ans, m)


# In[ ]:


LR.sort(key=itemgetter(0), reverse=True)
LR.sort(key=itemgetter(1))
list1 = func(LR)
LR.reverse()
list2 = func(LR)


# In[11]:


m = 0
for i in range(N):
    m = max(m, LR[i][1] - LR[i][0] + 1)
ans = max(ans, m)


# In[12]:


print(ans)


# In[ ]: