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 = int(input())
LRs = [tuple(map(int, input().split())) for _ in range(N)]


# [L1, R1]は最小のRを、[L2, R2]は最大のLを含む。L1を減らす形で走査。
R1 = min(map(lambda t: t[1], LRs))
L2 = max(map(lambda t: t[0], LRs))
LRs.sort(key=lambda t: (-t[0], t[1])) # Lについて降順、L同じならRについて昇順

ans = 0
R2 = 1001001001
for i in range(1, N-1):
    R2 = min(R2, LRs[i-1][1])
    L1 = LRs[i][0]  # LRsはLについて降順ソート済みより
    ans = max(ans, max(0, R2 - L2 + 1) + max(0, R1 - L1 + 1))


# 加えて、コンテスト1つだけ行うときを場合分け
LRs.sort(key = lambda t: t[0] - t[1])
one_problem_contest = LRs[0][1] - LRs[0][0] + 1
L_max = max(map(lambda t: t[0], LRs[1:]))
R_min = min(map(lambda t: t[1], LRs[1:]))
ans = max(ans, one_problem_contest + max(R_min - L_max + 1, 0))

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
LRs = [tuple(map(int, input().split())) for _ in range(N)]


# [L1, R1]は最小のRを、[L2, R2]は最大のLを含む。L1を減らす形で走査。
R1 = min(map(lambda t: t[1], LRs))
L2 = max(map(lambda t: t[0], LRs))
LRs.sort(key=lambda t: (-t[0], t[1])) # Lについて降順、L同じならRについて昇順

# ある区間が他の任意の区間に含まれる時だけ場合分け
if (L2, R1) == LRs[0]:
    one_problem_contest = max(R - L + 1 for L, R in LRs)
    ans = one_problem_contest + (R1 - L2 + 1)
else:
    R2 = 1001001001
    ans = 0
    for i in range(1, N-1):
        R2 = min(R2, LRs[i-1][1])
        L1 = LRs[i][0]  # LRsはLについて降順ソート済みより
        ans = max(ans, max(0, R2 - L2 + 1) + max(0, R1 - L1 + 1))

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
lr=[list(map(int,input().split())) for _ in range(n)]
lr.sort(key=lambda x:x[0])
left=[0]
l0,r0=lr[0]
left.append(r0-l0+1)
for i in range(1,n):
    l,r=lr[i]
    l0=max(l0,l)
    r0=min(r0,r)
    left.append(max(0,r0-l0+1))
l0,r0=lr[-1]
right=[0]
right.append(r0-l0+1)
for i in range(n-2,-1,-1):
    l,r=lr[i]
    l0=max(l0,l)
    r0=min(r0,r)
    right.append(max(0,r0-l0+1))
right.reverse()
ansl=0
for i in range(n+1):
    ansl=max(ansl,left[i]+right[i])

lr.sort(key=lambda x:x[1])
left=[0]
l0,r0=lr[0]
left.append(r0-l0+1)
for i in range(1,n):
    l,r=lr[i]
    l0=max(l0,l)
    r0=min(r0,r)
    left.append(max(0,r0-l0+1))
l0,r0=lr[-1]
right=[0]
right.append(r0-l0+1)
for i in range(n-2,-1,-1):
    l,r=lr[i]
    l0=max(l0,l)
    r0=min(r0,r)
    right.append(max(0,r0-l0+1))
right.reverse()
ansr=0
for i in range(n+1):
    ansr=max(ansr,left[i]+right[i])

print(max(ansl,ansr))
#print(ansl,ansr)

Submission status: Wrong Answer

Solution code:
n=int(input())
lr=[list(map(int,input().split())) for _ in range(n)]
lr.sort(key=lambda x:x[0])
left=[0]
l0,r0=lr[0]
left.append(r0-l0+1)
for i in range(1,n):
    l,r=lr[i]
    l0=max(l0,l)
    r0=min(r0,r)
    left.append(max(0,r0-l0+1))
l0,r0=lr[-1]
right=[0]
right.append(r0-l0+1)
for i in range(n-2,-1,-1):
    l,r=lr[i]
    l0=max(l0,l)
    r0=min(r0,r)
    right.append(max(0,r0-l0+1))
right.reverse()
#print(left)
#print(right)
ans=0
for i in range(n+1):
    ans=max(ans,left[i]+right[i])
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 10 ** 10

N = int(input())
 
L = [tuple(map(int,input().split())) for _ in range(N)]
 
L.sort(key=lambda x:(-x[0], x[1]))
 
MAX_L = 0
MIN_R = INF
MAX = 0
a = 0
b = 0
 
for i in range(N):
    if MAX_L < L[i][0]:
        MAX_L = L[i][0]
        a = i
    if MIN_R > L[i][1]:
        MIN_R = L[i][1]
        b = i
    MAX = max(MAX, L[i][1] - L[i][0] + 1)
 
ans = MAX + max(0, MIN_R - MAX_L + 1)
 
p = INF
 
for i in range(N-1):
    p = min(p, L[i][1])
    ans = max(ans, max(0, p - L[0][0] + 1) + max(0, L[-1][1] - L[i+1][0] + 1))
 
print(ans)

Submission status: Wrong Answer

Solution code:
INF = 10 ** 10

N = int(input())

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

L.sort(key=lambda x:(-x[0], x[1]))

MAX_L = 0
MIN_R = INF
MAX = 0
a = 0
b = 0

for i in range(N):
    if MAX_L < L[i][0]:
        MAX_L = L[i][0]
        a = i
    if MIN_R > L[i][1]:
        MIN_R = L[i][1]
        b = i
    MAX = max(MAX, L[i][1] - L[i][0] + 1)

ans = MAX + max(0, MIN_R - MAX_L + 1)

p = 0

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


    print(ans)

Submission status: Wrong Answer

Solution code:
INF = 10 ** 9 + 1

N = int(input())

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

L.sort(key=lambda x:(-x[0], x[1]))

MAX_L = 0
MIN_R = INF
MAX = 0
a = 0
b = 0

for i in range(N):
    if MAX_L < L[i][0]:
        MAX_L = L[i][0]
        a = i
    if MIN_R > L[i][1]:
        MIN_R = L[i][1]
        b = i
    MAX = max(MAX, L[i][1] - L[i][0] + 1)

ans = MAX + max(0, MIN_R - MAX_L + 1)

p = INF

for i in range(N-1):
    p = min(p, L[i][1])
    ans = max(ans, max(0, p - L[0][0] + 1) + max(0, L[-1][1] - L[i+1][0] + 1))

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x: (x[0], -x[1]))


front = []
most_left_R = float('inf')
for l, r in X:
    most_left_R = min(r, most_left_R)
    front.append(max(0, most_left_R - l + 1))

back = []
most_left_R = float('inf')
for l, r in X[::-1]:
    most_left_R = min(r, most_left_R)
    back.append(max(0, most_left_R - X[-1][0] + 1))
back = back[::-1]

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

# most_left_R = min([r for l, r in X])
# for l, r in X:
#     ans = max(ans, (r - l + 1) + max(0, most_left_R - X[-1][0] + 1))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = sorted([list(map(int, input().split())) for _ in range(N)])

front = []
most_left_R = float('inf')
for l, r in X:
    most_left_R = min(r, most_left_R)
    front.append(max(0, most_left_R - l + 1))

back = []
most_left_R = float('inf')
for l, r in X[::-1]:
    most_left_R = min(r, most_left_R)
    back.append(max(0, most_left_R - X[-1][0] + 1))
back = back[::-1]

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

most_left_R = min([r for l, r in X])
for l, r in X:
    ans = max(ans, (r - l + 1) + (most_left_R - X[-1][0] + 1))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = sorted([list(map(int, input().split())) for _ in range(N)])

front = []
most_left_R = float('inf')
for l, r in X:
    most_left_R = min(r, most_left_R)
    front.append(max(0, most_left_R - l + 1))

back = []
most_left_R = float('inf')
for l, r in X[::-1]:
    most_left_R = min(r, most_left_R)
    back.append(max(0, most_left_R - X[-1][0] + 1))
back = back[::-1]

ans = 0
for i in range(N - 1):
    ans = max(ans, front[i] + back[i + 1])
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
lr=[list(map(int,input().split())) for i in range(n)]
lr.sort(key=lambda x: (x[0],-x[1]))
# print(lr)

alst=[(lr[0][0],lr[0][1])]
blst=[(lr[-1][0],lr[-1][1])]
for i in range(n-1):
    alst.append((lr[i+1][0], min(alst[i][1],lr[i+1][1])))
    blst.append((max(blst[i][0],lr[-i-2][0]), min(blst[i][1],lr[-i-2][1])))
# print(alst)
# print(blst)
ans=0
def sa(lst):
    return max(0,lst[1]-lst[0]+1)

for i in range(n-1):
    ans=max(ans,sa(alst[i])+sa(blst[-i-2]))

# lr.sort(key=lambda x:-x[1],x[0])
# lr.reverse()
# alst=[(lr[0][0],lr[0][1])]
# blst=[(lr[-1][0],lr[-1][1])]
# # print(lr)
# for i in range(n-1):
#     alst.append((max(alst[i][0],lr[i+1][0]), min(alst[i][1],lr[i+1][1])))
#     blst.append((max(blst[i][0],lr[-i-2][0]), min(blst[i][1],lr[-i-2][1])))

# # print(alst)
# # print(blst)
# for i in range(n-1):
#     ans=max(ans,sa(alst[i])+sa(blst[-i-2]))

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
lr=[list(map(int,input().split())) for i in range(n)]
lr.sort()
# print(lr)

alst=[(lr[0][0],lr[0][1])]
blst=[(lr[-1][0],lr[-1][1])]
for i in range(n-1):
    alst.append((lr[i+1][0], min(alst[i][1],lr[i+1][1])))
    blst.append((max(blst[i][0],lr[-i-2][0]), min(blst[i][1],lr[-i-2][1])))
# print(alst)
# print(blst)
ans=0
def sa(lst):
    return max(0,lst[1]-lst[0]+1)

for i in range(n-1):
    ans=max(ans,sa(alst[i])+sa(blst[-i-2]))

lr.sort(key=lambda x:-x[1])
lr.reverse()
alst=[(lr[0][0],lr[0][1])]
blst=[(lr[-1][0],lr[-1][1])]
# print(lr)
for i in range(n-1):
    alst.append((max(alst[i][0],lr[i+1][0]), min(alst[i][1],lr[i+1][1])))
    blst.append((max(blst[i][0],lr[-i-2][0]), min(blst[i][1],lr[-i-2][1])))

# print(alst)
# print(blst)
for i in range(n-1):
    ans=max(ans,sa(alst[i])+sa(blst[-i-2]))

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
lr=[list(map(int,input().split())) for i in range(n)]
lr.sort()
# print(lr)

alst=[(lr[0][0],lr[0][1])]
blst=[(lr[-1][0],lr[-1][1])]
for i in range(n-1):
    alst.append((lr[i+1][0], min(alst[i][1],lr[i+1][1])))
    blst.append((max(blst[i][0],lr[-i-2][0]), min(blst[i][1],lr[-i-2][1])))
# print(alst)
# print(blst)
ans=0
def sa(lst):
    return max(0,lst[1]-lst[0]+1)
for i in range(n-1):
    ans=max(ans,sa(alst[i])+sa(blst[-i-2]))
print(ans)

Submission status: Wrong Answer

Solution code:
class SegTree():
    # 1-indexed
    def __init__(self, lists, function, basement):
        self.n = len(lists)
        self.K = (self.n-1).bit_length()
        self.f = function
        self.b = basement
        self.seg = [basement]*(2**(self.K+1)+1)
        X = 2**self.K
        for i, v in enumerate(lists):
            self.seg[i+X] = v
        for i in range(X-1, 0, -1):
            self.seg[i] = self.f(self.seg[i << 1], self.seg[i << 1 | 1])

    def update(self, k, value):
        X = 2**self.K
        k += X
        self.seg[k] = value
        while k:
            k = k >> 1
            self.seg[k] = self.f(self.seg[k << 1], self.seg[(k << 1) | 1])

    def query(self, L, R):
        num = 2**self.K
        L += num
        R += num
        vL = self.b
        vR = self.b
        while L < R:
            if L & 1:
                vL = self.f(vL, self.seg[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.f(self.seg[R], vR)
            L >>= 1
            R >>= 1
        return self.f(vL, vR)

N=int(input())
d=[]
for i in range(N):
    L,R=map(int,input().split())
    d.append((L,R))
d.sort(key=lambda x:(x[0],x[1]))
left=[0 for i in range(N)]
right=[0 for i in range(N)]
for i,v in enumerate(d):
    left[i]=v[0]
    right[i]=v[1]
L_tree=SegTree(left,max,0)
R_tree=SegTree(right,min,10**10)
ans=0
for i in range(N-1):
    p1=L_tree.query(0,i+1)
    p2=L_tree.query(i+1,N)
    q1=R_tree.query(0,i+1)
    q2=R_tree.query(i+1,N)
    ans=max(ans,max(q1-p1+1,0)+max(q2-p2+1,0))
for i in range(N):
	ans=max(ans,d[i][1]-d[i][0])
print(ans)

Submission status: Wrong Answer

Solution code:
class SegTree():
    # 1-indexed
    def __init__(self, lists, function, basement):
        self.n = len(lists)
        self.K = (self.n-1).bit_length()
        self.f = function
        self.b = basement
        self.seg = [basement]*(2**(self.K+1)+1)
        X = 2**self.K
        for i, v in enumerate(lists):
            self.seg[i+X] = v
        for i in range(X-1, 0, -1):
            self.seg[i] = self.f(self.seg[i << 1], self.seg[i << 1 | 1])

    def update(self, k, value):
        X = 2**self.K
        k += X
        self.seg[k] = value
        while k:
            k = k >> 1
            self.seg[k] = self.f(self.seg[k << 1], self.seg[(k << 1) | 1])

    def query(self, L, R):
        num = 2**self.K
        L += num
        R += num
        vL = self.b
        vR = self.b
        while L < R:
            if L & 1:
                vL = self.f(vL, self.seg[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.f(self.seg[R], vR)
            L >>= 1
            R >>= 1
        return self.f(vL, vR)

N=int(input())
d=[]
for i in range(N):
    L,R=map(int,input().split())
    d.append((L,R))
d.sort(key=lambda x:(x[0],x[1]))
left=[0 for i in range(N)]
right=[0 for i in range(N)]
for i,v in enumerate(d):
    left[i]=v[0]
    right[i]=v[1]
L_tree=SegTree(left,max,0)
R_tree=SegTree(right,min,10**10)
ans=0
for i in range(N-1):
    p1=L_tree.query(0,i+1)
    p2=L_tree.query(i+1,N)
    q1=R_tree.query(0,i+1)
    q2=R_tree.query(i+1,N)
    ans=max(ans,max(q1-p1+1,0)+max(q2-p2+1,0))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob) + 1
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L) + 1
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R) + 1
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L)) + 1
    
    ans1 = max_diff + max(min_R - max_L + 1,0)
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:(x[1],x[0]))
    L,R = INF,-INF
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        if cummax_L == max_L:
            break
        if r != min_R and cummax_L <= min_R and R - L <= r - cummax_L:
            R = r
            L = cummax_L
        cummax_L = max(cummax_L,l)
    
    ans2 = max(min_R - L + 1,0) + max(R - max_L + 1,0)

    print(max(ans1,ans2))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob) + 1
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L) + 1
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R) + 1
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L)) + 1
    
    ans1 = max_diff + max(min_R - max_L + 1,0)
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:(x[1],x[0]))
    L,R = INF,-INF
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        if cummax_L == max_L:
            break
        if r != min_R and cummax_L <= min_R and R - L <= r - cummax_L:
            R = r
            L = cummax_L
        cummax_L = max(cummax_L,l)
    
    ans2 = max(min_R - L + 1,0) + max(R - max_L + 1,0)

    print(max(ans1,ans2))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob) + 1
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L) + 1
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R) + 1
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L)) + 1
    
    ans1 = max_diff + max(min_R - max_L + 1,0)
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:(x[1],x[0]))
    L,R = INF,-INF
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        if cummax_L == max_L:
            break
        if r != min_R and r >= max_L and cummax_L <= min_R and R - L <= r - cummax_L:
            R = r
            L = cummax_L
        cummax_L = max(cummax_L,l)
    
    ans2 = max(min_R - L + 1,0) + max(R - max_L + 1,0)

    print(max(ans1,ans2))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob) + 1
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L) + 1
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R) + 1
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L)) + 1
    
    ans1 = max_diff + max(min_R - max_L + 1,0)
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:(x[1],x[0]))
    L,R = INF,-INF
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        if cummax_L == max_L:
            break
        if r != min_R and l <= min_R and R - L <= r - cummax_L:
            R = r
            L = cummax_L
        cummax_L = max(cummax_L,l)
    
    ans2 = max(min_R - L + 1,0) + max(R - max_L + 1,0)

    print(max(ans1,ans2))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob) + 1
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L) + 1
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R) + 1
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L)) + 1
    
    ans1 = max_diff + max(min_R - max_L + 1,0)
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:(x[1],x[0]))
    diff = -INF
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        if cummax_L == max_L:
            break
        diff = max(diff,r - cummax_L + 1)
        cummax_L = max(cummax_L,l)
    
    ans2 = min_R - max_L + diff + 1

    print(max(ans1,ans2,0))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob)
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L)
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R)
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L))
    
    ans1 = max_diff + max(min_R - max_L + 1,0) + 1
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:x[1])
    diff = 0
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        diff = max(diff,r - cummax_L + 1)
        if l != max_L:
            cummax_L = max(cummax_L,l)
    
    ans2 = min_R - max_L + diff + 1

    print(max(ans1,ans2,0))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    prob = [tuple(map(int,input().split())) for _ in range(N)]
    
    if N == 2:
        ans = sum(r - l + 1 for l,r in prob)
        print(ans)
        return 

    #min_Rとmax_Lが同じグループにいる時
    max_L = 0
    min_R = INF
    cnt_max = 0 #L最大を満たすのが何個あるか
    cnt_min = 0 #R最小を満たすのが何個あるか
    for l,r in prob:
        if l == max_L:
            cnt_max += 1
        elif l > max_L:
            cnt_max = 1
            max_L = l
        if r == min_R:
            cnt_min += 1
        elif r < min_R:
            cnt_min = 1
            min_R = r
    
    if cnt_min > 1 and cnt_max > 1:
        max_diff = max(r - l for l,r in prob)
    elif cnt_min > 1:
        max_diff = max(r - l for l,r in prob if l != max_L)
    elif cnt_max > 1:
        max_diff = max(r - l for l,r in prob if r != min_R)
    else:
        max_diff = max(r - l for l,r in prob if (r != min_R) and (l != max_L))
    
    ans1 = max_diff + max(min_R - max_L + 1,0) + 1
    
    #min_Rとmax_Lが違うグループにいる時
    prob.sort(key = lambda x:x[1])
    diff = 0
    cummax_L = prob[0][0]
    for l,r in prob[1:]:
        diff = max(diff,r - cummax_L + 1)
        cummax_L = max(cummax_L,l)
    
    ans2 = min_R - max_L + diff + 1

    print(max(ans1,ans2,0))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
n = int(input())
ps  = []
for i in range(n):
    l,r = map(int, input().split())
    ps.append((r-l+1,l,r))

#右端の小さい順
psr = sorted(ps,key=itemgetter(2))
n1,l1,r1 = psr[0]
# 左端の大きい順
psl = sorted(psr[1:],key=itemgetter(1),reverse=True)
n2,l2,r2 = psl[0]

if l1 >= l2:
    ps.sort(reverse = True)
    n,l,r = ps[0]
    print(n1+n)
    exit()


a_range = psr[0]
b_range = psl[0]
ps = sorted(psl[1:], key=itemgetter(1))
for n, l, r in ps:
    # print(a_range,b_range,(n,l,r))
    # 右端の最小と、左端の最大は固定できる
    an,al,ar = a_range
    bn,bl,br = b_range
    al2 = max(al,l)
    an2 = ar - al2 + 1
    br2 = min(br,r)
    bn2 = br2 - bl + 1
    if an2 <= 0 and bn2 <= 0:
        print(0)
        exit()
    elif an2 > 0 and bn2 <= 0:
        a_range = (an2, al2, ar)
    elif bn2 > 0 and an2 <= 0:
        b_range = (bn2, bl, br2)
    else:
        if an2 + bn  < bn2 + an:
            #bを更新
            b_range = (bn2, bl, br2)
        else:
            a_range = (an2, al2, ar)
    # print("aft",a_range,b_range,(n,l,r))
    

an, _, _ = a_range
bn, _, _ = b_range
print(an+bn)

Submission status: Wrong Answer

Solution code:

from operator import itemgetter
n = int(input())
ps  = []
for i in range(n):
    l,r = map(int, input().split())
    ps.append((r-l+1,l,r))

#右端の小さい順
psr = sorted(ps,key=itemgetter(2))
n1,l1,r1 = psr[0]

# 左端の大きい順
psl = sorted(psr[1:],key=itemgetter(1))
n2,l2,r2 = psl[0]

if l1 >= l2:
    ps.sort(reverse = True)
    n,l,r = ps[0]
    print(n1+n)


psl = sorted(ps,key=itemgetter(1),reverse = True)
a_range = psl[0]
psr = sorted(psl[1:],key=itemgetter(2))
b_range = psr[0]
ps = sorted(psr[1:], key=itemgetter(1))

for n, l, r in ps:
    an,al,ar = a_range
    bn,bl,br = b_range
    al2 = max(al,l)
    ar2 = min(ar,r)
    an2 = ar2 - al2 + 1
    bl2 = max(bl,l)
    br2 = min(br,r)
    bn2 = br2 - bl2 + 1
    if an2 <= 0 and bn2 <= 0:
        print(0)
        exit()
    elif an2 > 0 and bn2 <= 0:
        a_range = (an2, al2, ar2)
    elif bn2 > 0 and an2 <= 0:
        b_range = (bn2, bl2, br2)
    else:
        if an2 + bn  < bn2 + an:
            #bを更新
            b_range = (bn2, bl2, br2)
        else:
            a_range = (an2, al2, ar2)

an, _, _ = a_range
bn, _, _ = b_range
print(an+bn)

Submission status: Wrong Answer

Solution code:
"""
1st 1 2 3 4 | 5 6 7 | 8  
2nd 1 | 2 3 4 | 5 6 7 8
なるべく多くのカブリが生えるようにしたい

⇨まず、なるべく遠くの2つを選ぶのがポイント？
右端の最も小さいやつと、左端の最も大きいやつを選ぶか。

------l------r------
--l--r--------------
↑初見で最もカブリ小さいやつ

右端の小さい順に見て、何個の島に分けられるかを先に出す
1個 : r-1の最大と最小が答え
2個 : それぞれの島のrmin-lmaxが答え
3個以上 : 0
"""

from operator import itemgetter
n = int(input())
ps  = []
for i in range(n):
    l,r = map(int, input().split())
    ps.append((r-l+1,l,r))

# 右端の小さい順
cnt = 0
r_min = -1
l_max = 0
r_max = 0
l_min = 10**10
psr = sorted(ps,key=itemgetter(2))
cnts = []
for n,l,r in psr:
    r_max = max(r,r_max)
    l_min = min(l,l_min)
    # print(l_max, r_min, n,l,r)
    if l > r_min:
        cnt += 1
        # cnts.append(r_min - l_max + 1)
        r_min = r
        l_max = l
    else:
        l_max = max(l_max, l)
    if cnt == 3:
        print(0)
        exit()
# print(cnt,l_min,r_max)
# 島が1つしかなくて、かつ全域を覆う範囲が存在する
ps = sorted(ps,reverse=True)
if cnt == 1 and ps[0][1] == l_min and ps[0][2] == r_max:
    print(ps[0][0] + ps[-1][0])
    exit()



psl = sorted(ps,key=itemgetter(1),reverse = True)
a_range = psl[0]
psr = sorted(psl[1:],key=itemgetter(2))
b_range = psr[0]
ps = sorted(psr[1:], key=itemgetter(1))

for n, l, r in ps:
    an,al,ar = a_range
    bn,bl,br = b_range
    al2 = max(al,l)
    ar2 = min(ar,r)
    an2 = ar2 - al2 + 1
    bl2 = max(bl,l)
    br2 = min(br,r)
    bn2 = br2 - bl2 + 1
    if an2 <= 0 and bn2 <= 0:
        print(0)
        exit()
    elif an2 > 0 and bn2 <= 0:
        a_range = (an2, al2, ar2)
    elif bn2 > 0 and an2 <= 0:
        b_range = (bn2, bl2, br2)
    else:
        if an2 + bn  < bn2 + an:
            #bを更新
            b_range = (bn2, bl2, br2)
        else:
            a_range = (an2, al2, ar2)

an, _, _ = a_range
bn, _, _ = b_range
print(an+bn)

"""
4
1 2
2 3
4 5
3 4

"""

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
n = int(input())
ps  = []
for i in range(n):
    l,r = map(int, input().split())
    ps.append((r-l+1,l,r))

# 右端の小さい順
cnt = 0
r_min = -1
l_max = 0
r_max = 0
l_min = 10**10
psr = sorted(ps,key=itemgetter(2))
cnts = []
for n,l,r in psr:
    r_max = max(r,r_max)
    l_min = min(l,l_min)
    # print(l_max, r_min, n,l,r)
    if l > r_min:
        cnt += 1
        # cnts.append(r_min - l_max + 1)
        r_min = r
        l_max = l
    else:
        l_max = max(l_max, l)
    if cnt == 3:
        print(0)
        exit()
# print(cnt,l_min,r_max)
# 島が1つしかなくて、かつ全域を覆う範囲が存在する
ps = sorted(ps,reverse=True)
if cnt == 1 and ps[0][1] == l_min and ps[0][2] == r_max:
    print(ps[0][0] + ps[-1][0])
    exit()



psl = sorted(ps,key=itemgetter(1),reverse = True)
a_range = psl[0]
psr = sorted(psl[1:],key=itemgetter(2))
b_range = psr[0]
ps = psr[1:]
# print(a_range,b_range)
# print(ps)

for n, l, r in ps:
    an,al,ar = a_range
    bn,bl,br = b_range
    al2 = max(al,l)
    ar2 = min(ar,r)
    an2 = ar2 - al2 + 1
    bl2 = max(bl,l)
    br2 = min(br,r)
    bn2 = br2 - bl2 + 1
    if an2 <= 0 and bn2 <= 0:
        print(0)
        exit()
    elif an2 > 0 and bn2 <= 0:
        a_range = (an2, al2, ar2)
    elif bn2 > 0 and an2 <= 0:
        b_range = (bn2, bl2, br2)
    else:
        if an2 + bn  < bn2 + an:
            #bを更新
            b_range = (bn2, bl2, br2)
        else:
            a_range = (an2, al2, ar2)

an, _, _ = a_range
bn, _, _ = b_range
print(an+bn)