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:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    n = len(s)
    Llis = [0] * (n + 1)
    Rlis = [0] * (n + 1)
    for i in range(n):
        if s[i] == "<":
            Llis[i + 1] = 1
        else:
            Rlis[i] = 1
    for i in range(n):
        if Llis[i + 1] == 0:
            continue
        Llis[i + 1] += Llis[i]
    for i in range(n):
        if Rlis[-i - 2] == 0:
            continue
        Rlis[-i - 2] += Rlis[-i - 1]
    ans = 0
    for i in range(n + 1):
        ans += max(Rlis[i], Llis[i])
    print(ans)
    return

#B
def B():
    n = II()
    LR = LIR(n)
    LR.sort(key=lambda x: x[1] - x[0], reverse=True)
    ans = [[LR[0][0], LR[0][1] + 1], [LR[1][0], LR[1][1] + 1]]
    for i in range(2,n):
        l, r = LR[i]
        r += 1
        a12 = min(ans[0][1], ans[1][1]) - max(ans[0][0], ans[1][0])
        a12 = (a12 > 0) * a12 + r - l
        a1lr = min(ans[0][1], r) - max(ans[0][0], l)
        a1lr = (a1lr > 0) * a1lr + ans[1][1] - ans[1][0]
        a2lr = min(ans[1][1], r) - max(ans[1][0], l)
        a2lr = (a2lr > 0) * a2lr + ans[0][1] - ans[0][0]
        res = [(a12, 0), (a1lr, 1), (a2lr, 2)]
        res.sort(reverse=True)
        if res[0][1] == 0:
            ans[0] = [max(ans[0][0], ans[1][0]), min(ans[0][1], ans[1][1])]
            ans[1] = [l, r]
        elif res[0][1] == 1:
            ans[0] = [max(ans[0][0], l), min(ans[0][1], r)]
        else:
            ans[1] = [max(ans[1][0], l), min(ans[1][1], r)]
    a = ans[0][1] - ans[0][0]
    b = ans[1][1] - ans[1][0]
    print((a > 0) * a + (b > 0) * b)

    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    B()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    n = len(s)
    Llis = [0] * (n + 1)
    Rlis = [0] * (n + 1)
    for i in range(n):
        if s[i] == "<":
            Llis[i + 1] = 1
        else:
            Rlis[i] = 1
    for i in range(n):
        if Llis[i + 1] == 0:
            continue
        Llis[i + 1] += Llis[i]
    for i in range(n):
        if Rlis[-i - 2] == 0:
            continue
        Rlis[-i - 2] += Rlis[-i - 1]
    ans = 0
    for i in range(n + 1):
        ans += max(Rlis[i], Llis[i])
    print(ans)
    return

#B
def B():
    n = II()
    LR = LIR(n)
    ans = [[LR[0][0], LR[0][1] + 1], [LR[1][0], LR[1][1] + 1]]
    for i in range(2,n):
        l, r = LR[i]
        r += 1
        a12 = min(ans[0][1], ans[1][1]) - max(ans[0][0], ans[1][0])
        a12 = (a12 > 0) * a12 + r - l
        a1lr = min(ans[0][1], r) - max(ans[0][0], l)
        a1lr = (a1lr > 0) * a1lr + ans[1][1] - ans[1][0]
        a2lr = min(ans[1][1], r) - max(ans[1][0], l)
        a2lr = (a2lr > 0) * a2lr + ans[0][1] - ans[0][0]
        res = [(a12, 0), (a1lr, 1), (a2lr, 2)]
        res.sort(reverse=True)
        if res[0][1] == 0:
            ans[0] = [max(ans[0][0], ans[1][0]), min(ans[0][1], ans[1][1])]
            ans[1] = [l, r]
        elif res[0][1] == 1:
            ans[0] = [max(ans[0][0], l), min(ans[0][1], r)]
        else:
            ans[1] = [max(ans[1][0], l), min(ans[1][1], r)]
    a = ans[0][1] - ans[0][0]
    b = ans[1][1] - ans[1][0]
    print((a > 0) * a + (b > 0) * b)

    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    B()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    n = len(s)
    Llis = [0] * (n + 1)
    Rlis = [0] * (n + 1)
    for i in range(n):
        if s[i] == "<":
            Llis[i + 1] = 1
        else:
            Rlis[i] = 1
    for i in range(n):
        if Llis[i + 1] == 0:
            continue
        Llis[i + 1] += Llis[i]
    for i in range(n):
        if Rlis[-i - 2] == 0:
            continue
        Rlis[-i - 2] += Rlis[-i - 1]
    ans = 0
    for i in range(n + 1):
        ans += max(Rlis[i], Llis[i])
    print(ans)
    return

#B
def B():
    n = II()
    LR = LIR(n)
    ans = [[LR[0][0], LR[0][1] + 1], [LR[1][0], LR[1][1] + 1]]
    for i in range(2,n):
        l, r = LR[i]
        r += 1
        a12 = min(ans[0][1], ans[1][1]) - max(ans[0][0], ans[1][0]) + r - l
        a1lr = min(ans[0][1], r) - max(ans[0][0], l) + ans[1][1] - ans[1][0]
        a2lr = min(ans[1][1], r) - max(ans[1][0], l) + ans[0][1] - ans[0][0]
        res = [(a12, 0), (a1lr, 1), (a2lr, 2)]
        res.sort(reverse=True)
        if res[0][1] == 0:
            ans[0] = [max(ans[0][0], ans[1][0]), min(ans[0][1], ans[1][1])]
            ans[1] = [l, r]
        elif res[0][1] == 1:
            ans[0] = [max(ans[0][0], l), min(ans[0][1], r)]
        else:
            ans[1] = [max(ans[1][0], l), min(ans[1][1], r)]
    a = ans[0][1] - ans[0][0]
    b = ans[1][1] - ans[1][0]
    print((a > 0) * a + (b > 0) * b)

    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    B()

Submission status: Wrong Answer

Solution code:
N = int(input())
questions = [list(map(int,input().split())) for _ in range(N)]
questions.sort(key=lambda x: x[1])
#print(questions)
flag = False
for i in range(len(questions) - 1):
    if questions[i][0] < questions[i+1][0] or questions[i][1] > questions[i+1][1]:
        flag = True
        break
ans = 0
if flag:
    for i in range(N - 1):
        if i == 0:
            left, right = questions[0][0] , questions[0][1]
            s1 = right - left + 1
            left, right = max(questions[i + 1][0], questions[N - 1][0]), min(questions[i + 1][1], questions[N - 1][1])
            s2 = right - left + 1
        elif i == N - 2:
            left, right = max(questions[0][0], questions[i][0]), min(questions[0][1], questions[i][1])
            s1 = right - left + 1
            left, right = questions[N - 1][0], questions[N - 1][1]
            s2 = right - left + 1
        else:
            left , right = max(questions[0][0] , questions[i][0]) , min(questions[0][1] , questions[i][1])
            s1 = right - left + 1
            left , right = max(questions[i + 1][0] , questions[N - 1][0]) , min(questions[i + 1][1] , questions[N - 1][1])
            s2 = right - left + 1

        tmp_ans = s1 + s2
        #print(tmp_ans)
        if tmp_ans > ans:
            ans = tmp_ans
else:
    ans = questions[N-1][1] - questions[N-1][0] + 1 + questions[0][1] - questions[0][0] + 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
questions = [list(map(int,input().split())) for _ in range(N)]
questions.sort(key=lambda x: x[1])
#print(questions)
ans = 0
for i in range(N - 1):
    if i == 0:
        left, right = questions[0][0] , questions[0][1]
        s1 = right - left + 1
        left, right = max(questions[i + 1][0], questions[N - 1][0]), min(questions[i + 1][1], questions[N - 1][1])
        s2 = right - left + 1
    elif i == N - 2:
        left, right = max(questions[0][0], questions[i][0]), min(questions[0][1], questions[i][1])
        s1 = right - left + 1
        left, right = questions[N - 1][0], questions[N - 1][1]
        s2 = right - left + 1
    else:
        left , right = max(questions[0][0] , questions[i][0]) , min(questions[0][1] , questions[i][1])
        s1 = right - left + 1
        left , right = max(questions[i + 1][0] , questions[N - 1][0]) , min(questions[i + 1][1] , questions[N - 1][1])
        s2 = right - left + 1

    tmp_ans = s1 + s2
    #print(tmp_ans)
    if tmp_ans > ans:
        ans = tmp_ans
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
questions = [list(map(int,input().split())) for _ in range(N)]
questions.sort(key=lambda x: x[1])
print(questions)
ans = 0
for i in range(N - 1):
    if i == 0:
        left, right = questions[0][0] , questions[0][1]
        s1 = right - left + 1
        left, right = max(questions[i + 1][0], questions[N - 1][0]), min(questions[i + 1][1], questions[N - 1][1])
        s2 = right - left + 1
    elif i == N - 2:
        left, right = max(questions[0][0], questions[i][0]), min(questions[0][1], questions[i][1])
        s1 = right - left + 1
        left, right = questions[N - 1][0], questions[N - 1][1]
        s2 = right - left + 1
    else:
        left , right = max(questions[0][0] , questions[i][0]) , min(questions[0][1] , questions[i][1])
        s1 = right - left + 1
        left , right = max(questions[i + 1][0] , questions[N - 1][0]) , min(questions[i + 1][1] , questions[N - 1][1])
        s2 = right - left + 1

    tmp_ans = s1 + s2
    #print(tmp_ans)
    if tmp_ans > ans:
        ans = tmp_ans
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N = int(input())
L, R = [], []
length = []
for i in range(N):
	Li, Ri = map(int,input().split())
	L.append(Li)
	R.append(Ri)
	length.append(Ri-Li)

sindL = sorted(range(N), key=lambda k: L[k])
sindR = sorted(range(N), key=lambda k: R[k])
sind_len = sorted(range(N), key=lambda k: length[k])

indLmax = sindL[-1]
indRmin = sindR[0]

if N==2:
	ans = sum(length)
else:
	if indLmax==indRmin:
		ans = R[indRmin]-L[indLmax]+max(length)+2
	else:
		lenmax_ind = sind_len[-1]
		if lenmax_ind==indLmax or lenmax_ind==indRmin:
			lenmax_ind = sind_len[-2]
			if lenmax_ind==indLmax or lenmax_ind==indRmin:
				lenmax_ind = sind_len[-3]
		ans = R[indRmin]-L[indLmax]+length[lenmax_ind]+2

		Qind = [0]*N
		Qind[indRmin] = 1
		ri = 1
		for li in range(0,N-1):
			QL, QR = L[sindL[li]], R[indRmin]
			Qind[sindL[li]] = 1
			while Qind[sindR[ri]]==1:
				ri += 1
				if ri==N: break
			PL, PR = L[indLmax], R[sindR[ri]]

			ans = max(ans,PR-PL+QR-QL+2)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
lr_list = LIR(n)
lr_list.sort()
max_l1 = lr_list[-1][0]
min_r1 = INF


for i, (l, r) in enumerate(lr_list):
    if r < min_r1:
        min_r1 = r
        min_r_ind = i




if n == 2:
    print(lr_list[0][1] - lr_list[0][0] + lr_list[1][1] - lr_list[1][0])


else:
    max_diff = 0
    for i in range(n - 1):
        if i == min_r_ind:
            continue
        max_diff = max(max_diff, lr_list[i][1] - lr_list[i][0] + 1)

    ans = max(0, min_r1 -  max_l1 + 1) + max_diff

    
    min_r2 = lr_list[-1][1]
    for j in range(n - 2, -1, -1):
        if j == min_r_ind:
            continue
        max_l2 = lr_list[j][0]
        ans = max(ans, max(min_r1 - max_l2 + 1, 0) + max(min_r2 - max_l1 + 1, 0))
        min_r2 = min(min_r2, lr_list[j][1])


    max_l2 = lr_list[min_r_ind][0]
    ans = max(ans, max(min_r1 - max_l2 + 1, 0) + max(min_r2 - max_l1 + 1, 0))
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
lr_list = LIR(n)
lr_list.sort()
max_l1 = lr_list[-1][0]
min_r1 = INF


for i, (l, r) in enumerate(lr_list):
    if r < min_r1:
        min_r1 = r
        min_r_ind = i


max_diff = 0
for i in range(n - 1):
    if i == min_r_ind:
        continue
    max_diff = max(max_diff, lr_list[i][1] - lr_list[i][0] + 1)


ans = max(0, min_r1 -  max_l1 + 1) + max_diff
min_r2 = lr_list[-1][1]
max_l2 = lr_list[min_r_ind][0]
for j in range(n - 2, -1, -1):
    if j == min_r_ind:
        continue
    max_l2 = lr_list[j][0]
    ans = max(ans, max(min_r1 - max_l2 + 1, 0) + max(min_r2 - max_l1 + 1, 0))
    min_r2 = min(min_r2, lr_list[j][1])


ans = max(ans, max(min_r1 - max_l2 + 1, 0) + max(min_r2 - max_l1 + 1, 0))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
lr_list = LIR(n)
lr_list.sort(key=lambda x:x[1], reverse=True)
lr_list.sort(key=lambda x:x[0])
max_l1 = lr_list[-1][0]
min_r1 = INF


for i, (l, r) in enumerate(lr_list):
    if r < min_r1:
        min_r1 = r
        min_r_ind = i


max_diff = 0
for i in range(n - 1):
    if i == min_r_ind:
        continue
    max_diff = max(max_diff, lr_list[i][1] - lr_list[i][0] + 1)


ans = max(0, min_r1 -  max_l1 + 1) + max_diff
min_r2 = lr_list[-1][1]
max_l2 = lr_list[min_r_ind][0]
for j in range(n - 2, -1, -1):
    if j == min_r_ind:
        continue
    max_l2 = lr_list[j][0]
    ans = max(ans, max(min_r1 - max_l2 + 1, 0) + max(min_r2 - max_l1 + 1, 0))
    min_r2 = min(min_r2, lr_list[j][1])

ans = max(ans, max(min_r1 - max_l2 + 1, 0) + max(min_r2 - max_l1 + 1, 0))




print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
MOD = 10**9 + 7
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
tests = []
for i in range(n):
    l, r = MAP()
    tests.append([l, r])
lmax = tests[0]
for i in range(1,n):
    if tests[i][0] > lmax[0]:
        lmax = tests[i]
rmin = tests[0]
for i in range(1,n):
    if tests[i][1] < rmin[1]:
        rmin = tests[i]

if lmax == rmin:
    res = 0
    for i in range(n-1):
        res = max(res, tests[i][1] - tests[i][0]+ 1)
    res += rmin[1] - lmax[0] + 1
else:
    tests.remove(lmax)
    tests.remove(rmin)
    for i in range(n-2):
        if max(tests[i][0] - rmin[0], 0) <= max(lmax[1] - tests[i][1], 0):
            rmin[0] = max(tests[i][0], rmin[0])
        else:
            lmax[1] = max(tests[i][1], lmax[1])
    res = rmin[1] - rmin[0] + lmax[1] - lmax[0] + 2

print(res)

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1

def main():
    inf = 10 ** 10
    n = int(input())
    lr = [list(map(int, input().split())) for _ in range(n)]
    biggest_l = max(l for l, r in lr)
    smallest_r = min(r for l, r in lr)
    mx = max(max(r - l + 1 for l, r in lr), 0) + max(smallest_r - biggest_l + 1, 0)
    ab = [[max(0, r - biggest_l + 1), max(0, smallest_r - l + 1)] for l, r in lr]
    ab.sort(key=lambda x: (x[0], -x[1]))
    # print(ab)
    for i in range(n - 1):
        cur = ab[i][1] + ab[i + 1][0]
        if cur > mx: mx = cur
    print(mx)

main()

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1

def main():
    inf = 10 ** 10
    n = int(input())
    lr = [list(map(int, input().split())) for _ in range(n)]
    biggest_l = max(l for l, r in lr)
    smallest_r = min(r for l, r in lr)
    mx = max(r - l + 1 for l, r in lr) + max(smallest_r - biggest_l + 1, 0)
    ab = [[max(0, r - biggest_l + 1), max(0, smallest_r - l + 1)] for l, r in lr]
    ab.sort(key=lambda x: (x[0], -x[1]))
    # print(ab)
    for i in range(n - 1):
        cur = ab[i][1] + ab[i + 1][0]
        if cur > mx: mx = cur
    print(mx)

main()

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1

def main():
    inf = 10 ** 10
    n = int(input())
    lr = [list(map(int, input().split())) for _ in range(n)]
    biggest_l = max(l for l, r in lr)
    smallest_r = min(r for l, r in lr)
    mx = max(r - l for l, r in lr) + smallest_r - biggest_l + 2
    ab = [[max(0, r - biggest_l + 1), max(0, smallest_r - l + 1)] for l, r in lr]
    ab.sort(key=lambda x: (x[0], -x[1]))
    # print(ab)
    for i in range(n - 1):
        cur = ab[i][1] + ab[i + 1][0]
        if cur > mx: mx = cur
    print(mx)

main()

Submission status: Wrong Answer

Solution code:
#https://img.atcoder.jp/agc040/editorial.pdf
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  if n == 2:
    ans = 0
    for i in range(2):
      l,r = (int(i) for i in input().split())
      ans += r-l+1
    print(ans)
    exit()
    
  lquery = []
  rquery = []
  lrquery = []
  for i in range(n):
    l,r = (int(i) for i in input().split())
    lquery.append(l)
    rquery.append(r)
    lrquery.append((r-l+1,l,r))
  lrquery = sorted(lrquery,reverse= True)
  Lmax = max(lquery)
  Rmin = min(rquery)
  #ans1
  ans = 0
  found = False
  for i in range(3):
    damp,l,r = lrquery[i]
    if found:
      break
    if l != Lmax and r != Rmin:
      ans += damp
      found = True
  ans += max(0,Rmin+1-Lmax)
  #print(ans)
  
  #ans2
  ans2 = 0
  newLR = []
  for i in range(n):
    damp,l,r = lrquery[i]
    newLR.append((Rmin-l+1,r-Lmax+1))
  newLR.sort()

  query = [0]*n
  #print(newLR)
  _,a = newLR[0]
  query[0] = a
  backquery = [0]*n

  for i in range(1,n):
    _,a = (newLR[i])
    query[i] = min(query[i-1],a)
  b,_ = newLR[-1]
  backquery[-1] = b
  for i in range(-2,-1*n-1,-1):
    b,_ = newLR[i]
    backquery[i] = min(backquery[i+1],b)

  for i in range(1,n-1):
    ans2 = max(ans2,query[i]+backquery[i+1])
  print(max(ans,ans2))
solve()

Submission status: Wrong Answer

Solution code:
#https://img.atcoder.jp/agc040/editorial.pdf
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  if n == 2:
    ans = 0
    for i in range(2):
      l,r = (int(i) for i in input().split())
      ans += r-l+1
    print(ans)
    exit()
    
  lquery = []
  rquery = []
  lrquery = []
  for i in range(n):
    l,r = (int(i) for i in input().split())
    lquery.append(l)
    rquery.append(r)
    lrquery.append((r-l+1,l,r))
  lrquery = sorted(lrquery,reverse= True)
  Lmax = max(lquery)
  Rmin = min(rquery)
  #ans1
  ans = 0
  found = False
  for i in range(3):
    damp,l,r = lrquery[i]
    if found:
      break
    if l != Lmax and r != Rmin:
      ans += damp
      found = True
  ans += max(0,Rmin+1-Lmax)
  #print(ans)
  
  #ans2
  ans2 = 0
  newLR = []
  for i in range(n):
    damp,l,r = lrquery[i]
    newLR.append((Rmin-l+1,r-Lmax+1))
  newLR.sort()

  query = [0]*n
  #print(newLR)
  _,a = newLR[0]
  query[0] = a
  backquery = [0]*n

  for i in range(1,n):
    _,a = (newLR[i])
    query[i] = min(query[i-1],a)
  b,_ = newLR[-1]
  backquery[-1] = b
  for i in range(-2,-1*n-1,-1):
    b,_ = newLR[i]
    backquery[i] = min(backquery[i+1],b)
  for i in range(n-1):
    ans2 = max(ans2,query[i]+backquery[i+1])
  print(max(ans,ans2))
solve()

Submission status: Wrong Answer

Solution code:
#https://img.atcoder.jp/agc040/editorial.pdf
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  if n == 2:
    ans = 0
    for i in range(2):
      l,r = (int(i) for i in input().split())
      ans += r-l+1
    print(ans)
    exit()
    
  lquery = []
  rquery = []
  lrquery = []
  for i in range(n):
    l,r = (int(i) for i in input().split())
    lquery.append(l)
    rquery.append(r)
    lrquery.append((r-l+1,l,r))
  lrquery = sorted(lrquery,reverse= True)
  Lmax = max(lquery)
  Rmin = min(rquery)
  #ans1
  ans = 0
  found = False
  for i in range(3):
    damp,l,r = lrquery[i]
    if found:
      break
    if l != Lmax and r != Rmin:
      ans += damp
      found = True
  ans += max(0,Rmin+1-Lmax)
  #print(ans)
  
  #ans2
  ans2 = 0
  newLR = []
  for i in range(n):
    damp,l,r = lrquery[i]
    newLR.append((Rmin-l+1,r-Lmax+1))
  newLR.sort()
  for i in range(1,n):
    _,a = max(newLR[:i])
    b,_ = min(newLR[i:])
    ans2 = max(ans2,a+b)
  print(max(ans,ans2))
solve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
LR = [tuple(map(int, input().split())) for _ in range(n)]

def quicksort(x):
    if x==[]: return []

    smallerSorted = quicksort([a for a in x[1:] if a[0] < x[0][0] or (a[0] == x[0][0] and a[1] < x[0][1])])
    biggerSorted = quicksort([a for a in x[1:] if a[0] > x[0][0] or (a[0] == x[0][0] and a[1] >= x[0][1])])

    return(smallerSorted+[x[0]]+biggerSorted)

LR = quicksort(LR)

ans = [0] * (n+1)
temp = 10 ** 9
i = 1
for L, R in LR:
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] = temp - L + 1
    i += 1

temp = 10 ** 9
i = n - 1
L = LR[-1][0]
for x, R in reversed(LR):
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] += temp - L + 1
    i -= 1

res = max(ans)
for L, R in LR:
    res = max(res, R - L + 1)

print(res)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
LR = [tuple(map(int, input().split())) for _ in range(n)]

def quicksort(x):
    if x==[]: return []

    smallerSorted = quicksort([a for a in x[1:] if a[0] < x[0][0] or (a[0] == x[0][0] and a[1] >= x[0][1])])
    biggerSorted = quicksort([a for a in x[1:] if a[0] > x[0][0] or (a[0] == x[0][0] and a[1] < x[0][1])])

    return(smallerSorted+[x[0]]+biggerSorted)

LR = quicksort(LR)

ans = [0] * (n+1)
temp = 10 ** 9
i = 1
for L, R in LR:
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] = temp - L + 1
    i += 1

temp = 10 ** 9
i = n - 1
L = LR[-1][0]
for x, R in reversed(LR):
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] += temp - L + 1
    i -= 1

res = max(ans)
for L, R in LR:
    res = max(res, R - L + 1)

print(res)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
LR = [tuple(map(int, input().split())) for _ in range(n)]

def quicksort(x):
    if x==[]: return []

    smallerSorted = quicksort([a for a in x[1:] if a[0] < x[0][0] or (a[0] == x[0][0] and a[1] > x[0][1])])
    biggerSorted = quicksort([a for a in x[1:] if a[0] > x[0][0] or (a[0] == x[0][0] and a[1] <= x[0][1])])

    return(smallerSorted+[x[0]]+biggerSorted)

LR = quicksort(LR)

ans = [0] * (n+1)
temp = 10 ** 9
i = 1
for L, R in LR:
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] = temp - L + 1
    i += 1

temp = 10 ** 9
i = n - 1
L = LR[-1][0]
for x, R in reversed(LR):
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] += temp - L + 1
    i -= 1

res = max(ans)
for L, R in LR:
    res = max(res, R - L + 1)

print(res)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    dg = 10**10
    lr = []
    for _ in [0]*n:
        lll,rrr = map(int,input().split())
        lr.append(lll*dg+dg-rrr)
    lr.sort()

    for i in range(n):
        lr[i] = (lr[i]//dg)*dg+(dg-lr[i]%dg)

    #mirl[i],marl[i]:R0~Riのmin,max
    mirl = [10**10]*n
    marl = [0]*n
    for i,e in enumerate(lr):
        mirl[i] = min(mirl[i-1],e%dg)
        marl[i] = max(marl[i-1],e%dg)
    
    #mirr[i]:Ri~Rn-1のmin
    mirr = [0]*n
    mirr[-1] = lr[-1]%dg
    for i in range(n-2,-1,-1):
        mirr[i] = min(mirr[i+1],lr[i]%dg)

    L1 = lr[-1]//dg
    res = (lr[0]%dg - lr[0]//dg+1) + max(0,mirr[1]-L1+1)

    for k in range(n-2,0,-1):
        #print(res)
        L2 = lr[k]//dg
        R1 = mirr[k+1]
        R2 = lr[k]%dg

        restMaxR = marl[k-1]
        restMinR = mirl[k-1]

        #print(L1,L2,R1,R2,restMaxR,restMinR)

        tmp = max(0,min(restMaxR,R1)-L1+1)+max(0,min(restMinR,R2)-L2+1)
        tmp = max(tmp,max(0,min(restMinR,R1)-L1+1)+max(0,min(restMaxR,R2)-L2+1))
        tmp = max(tmp,max(0,R1-L1+1)+max(0,min(restMinR,R2)-L2+1))
        tmp = max(tmp,max(0,min(restMinR,R1)-L1+1),max(0,R2-L2+1))

        res = max(res,tmp)

    
    print(res)
        



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
LR = [tuple(map(int, input().split())) for _ in range(n)]

def quicksort(x):
    if x==[]: return []

    smallerSorted = quicksort([a for a in x[1:] if a[0] < x[0][0] or (a[0] == x[0][0] and a[1] > x[0][1])])
    biggerSorted = quicksort([a for a in x[1:] if a[0] > x[0][0] or (a[0] == x[0][0] and a[1] <= x[0][1])])

    return(smallerSorted+[x[0]]+biggerSorted)

LR = quicksort(LR)

ans = [0] * (n+1)
temp = 10 ** 9
i = 1
for L, R in LR:
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] = temp - L + 1
    i += 1
#print(ans)
temp = 10 ** 9
i = n - 1
L = LR[-1][0]
for x, R in reversed(LR):
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] += temp - L + 1
    i -= 1

#print(LR)
#print(ans)
print(max(ans))