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())
x=sorted([tuple(map(int,input().split()))for i in range(n)])


mminfront=[]
m=float("inf")
for s,t in x:
    m=min(m,t)
    mminfront.append(m)

mminback=[]
m=float("inf")
for s,t in x[::-1]:
    m=min(m,t)
    mminback.append(m)
mminback=mminback[::-1]

ans=[]
for i in range(len(x)-1):
    ans.append(max(mminfront[i]-x[i][0],0)+max(mminback[i+1]-x[-1][0],0))
#print(ans)
#y=sorted(x,key=lambda x:x[1])
#print(y)

#maxfront=[]
#m=0
#for s,t in y:
#    m=max(s,m)
#    maxfront.append(m)
#maxback=[]
#m=0
#for s,t in y[::-1]:
#    m=max(s,m)
#    maxback.append(m)
#maxback=maxback[::-1]
#print(maxfront)
#print(maxback)
#for i in range(len(x)-1,1,-1):
    
print(max(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
x=sorted([tuple(map(int,input().split()))for i in range(n)])


mminfront=[]
m=float("inf")
for s,t in x:
    m=min(m,t)
    mminfront.append(m)

mminback=[]
m=float("inf")
for s,t in x[::-1]:
    m=min(m,t)
    mminback.append(m)
mminback=mminback[::-1]

ans=[]
for i in range(len(x)-1):
    ans.append(max(mminfront[i]-x[i][0],0)+max(mminback[i+1]-x[-1][0],0))
#print(ans)
#y=sorted(x,key=lambda x:x[1])
#print(y)

#maxfront=[]
#m=0
#for s,t in y:
#    m=max(s,m)
#    maxfront.append(m)
#maxback=[]
#m=0
#for s,t in y[::-1]:
#    m=max(s,m)
#    maxback.append(m)
#maxback=maxback[::-1]
#print(maxfront)
#print(maxback)
#for i in range(len(x)-1,1,-1):
    
print(max(ans))

Submission status: Wrong Answer

Solution code:
def pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,-1]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

N = int(input())
List = [list(map(int,input().split())) for i in range(N)]

max_l = 0
max_l_problem = 0
min_r = 10**9+1
min_r_problem = 0
for i in range(N):
    now_l = List[i][0]
    max_l = max(max_l, now_l)
    now_r = List[i][1]
    min_r = min(min_r, now_r)

# Rが大きいものから順にfirst_contestに入れ、残りはsecond_contest
# その中で楽しさ最大のものを選ぶ
ans1 = 0
List_sorted = sorted(List, key=lambda x: (-x[1], x[0]))
for i in range(1,len(List_sorted)):
    List_first_contest = List_sorted[0:i]
    List_second_contest = List_sorted[i:N]
    first_pleasure = max(List_first_contest[-1][1] - max_l + 1, 0)
    second_pleasure = max(min_r - List_second_contest[0][0] + 1, 0)
    #print(List_first_contest, List_second_contest, first_pleasure, second_pleasure)
    if ans1 < first_pleasure+second_pleasure:
        ans1 = first_pleasure+second_pleasure
    else:
        break

# 最長問題1つだけのコンテストをつくるパターン
max_len = 0
max_len_problem = 0
for i in range(N):
    now_len = List[i][1]-List[i][0]+1
    if max_len < now_len:
        max_len = now_len
        max_len_problem = i
first_contest = List[max_len_problem]

# 最長問題以外を片方のコンテストに入れる
second_contest = [1,10**9]
for i in range(N):
    if i == max_len_problem:
        continue
    second_contest = pleasure(second_contest,List[i])
        
ans2 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
lr = [list(map(int, input().split())) for i in range(n)]

lr.sort(key=lambda x: (x[0], x[1]))
L = []
R = []
for i in lr:
  L.append(i[0])
  R.append(i[1])

l1, r1 = L[0], R[0]
L2,R2 = set(L[1:]), set(R[1:])
l2, r2 = max(L2), min(R2)
for l,r in zip(L[1:-1],R[1:-1]):
  L2.discard(l)
  R2.discard(r)
  ll,rr = max(L2), min(R2)
  if max(0,(l - l1)) + max(0,(r1 - r)) < max(0, l - ll) + max(0, rr-r):
    l1, r1 = max(l,l1), min(r,r1)
    l2, r2 = ll, rr
      
  else:
    L2.add(l2)
    R2.add(r2)
print(max(r1-l1,0)+max(r2-l2,0)+2)

Submission status: Wrong Answer

Solution code:
def pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,-1]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

N = int(input())
List = [list(map(int,input().split())) for i in range(N)]

# Lが最大の問題をfirst_contestにする
max_l = 0
max_l_problem = 0
for i in range(N):
    now_l = List[i][0]
    if max_l < now_l:
        max_l = now_l
        max_l_problem = i
first_contest = List[max_l_problem]

# Rが最小の問題をsecond_contestにする
min_r = 10**9+1
min_r_problem = 0
for i in range(N):
    now_r = List[i][1]
    if min_r > now_r:
        min_r = now_r
        min_r_problem = i
second_contest = List[min_r_problem]

# Rが大きいものから順にfirst_contestに入れ、残りはsecond_contest
# その中で楽しさ最大のものを選ぶ
ans1 = 0
List_sorted = sorted(List, key=lambda x: (-x[1], x[0])) #R降順
for i in range(1,len(List_sorted)):
    List_first_contest = List_sorted[0:i]
    List_second_contest = List_sorted[i:N]
    first_pleasure = max(List_first_contest[-1][1] - List[max_l_problem][0] + 1, 0)
    second_pleasure = max(List[min_r_problem][1] - List_second_contest[0][0] + 1, 0)
    if ans1 < first_pleasure+second_pleasure:
        ans1 = first_pleasure+second_pleasure
    else:
        break

# 最長問題1つだけのコンテストをつくるパターン
max_len = 0
max_len_problem = 0
for i in range(N):
    now_len = List[i][1]-List[i][0]+1
    if max_len < now_len:
        max_len = now_len
        max_len_problem = i
first_contest = List[max_len_problem]

# 最長問題以外を片方のコンテストに入れる
second_contest = [1,10**9]
for i in range(N):
    if i == max_len_problem:
        continue
    second_contest = pleasure(second_contest,List[i])
        
ans2 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
def pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,-1]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

N = int(input())
List = [list(map(int,input().split())) for i in range(N)]

# Lが最大の問題をfirst_contestにする
max_l = 0
max_l_problem = 0
for i in range(N):
    now_l = List[i][0]
    if max_l < now_l:
        max_l = now_l
        max_l_problem = i
first_contest = List[max_l_problem]

# Rが最小の問題をsecond_contestにする
min_r = 10**9+1
min_r_problem = 0
for i in range(N):
    now_r = List[i][1]
    if min_r > now_r:
        min_r = now_r
        min_r_problem = i
second_contest = List[min_r_problem]

# Rが大きいものから順にfirst_contestに入れ、残りはsecond_contest
# その中で楽しさ最大のものを選ぶ
ans1 = 0
List_sorted = sorted(List, key=lambda x:-x[1]) #R降順
for i in range(1,len(List_sorted)):
    List_first_contest = List_sorted[0:i]
    List_second_contest = List_sorted[i:N]
    first_pleasure = max(List_first_contest[-1][1] - List[max_l_problem][0] + 1, 0)
    second_pleasure = max(List[min_r_problem][1] - List_second_contest[0][0] + 1, 0)
    if ans1 < first_pleasure+second_pleasure:
        ans1 = first_pleasure+second_pleasure
    else:
        break

# 最長問題1つだけのコンテストをつくるパターン
max_len = 0
max_len_problem = 0
for i in range(N):
    now_len = List[i][1]-List[i][0]+1
    if max_len < now_len:
        max_len = now_len
        max_len_problem = i
first_contest = List[max_len_problem]

# 最長問題以外を片方のコンテストに入れる
second_contest = [1,10**9]
for i in range(N):
    if i == max_len_problem:
        continue
    second_contest = pleasure(second_contest,List[i])
        
ans2 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1

print(max(ans1,ans2))

Submission status: Wrong Answer

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

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

dp = [[[0, 0], [0, 0]] for _ in range(N)]

L0, R0 = LR[0]
L1, R1 = LR[1]
dp[1][0][0] = L0
dp[1][0][1] = R0
dp[1][1][0] = L1
dp[1][1][1] = R1
L, R = max(L0, L1), min(R0, R1)
for i in range(1, N-1):
    l, r = LR[i+1]
    a0 = max(min(dp[i][0][1], r) - max(dp[i][0][0], l)+1, 0) + max(dp[i][1][1] - dp[i][1][0] + 1, 0)
    a1 = max(min(dp[i][1][1], r) - max(dp[i][1][0], l)+1, 0) + max(dp[i][0][1] - dp[i][0][0] + 1, 0)
    a2 = max(R-L+1, 0) + r-l+1
    a = max(a0, a1, a2)
    if a == a0:
        dp[i+1][0][0] = max(dp[i][0][0], l)
        dp[i+1][0][1] = min(dp[i][0][1], r)
        dp[i+1][1][0] = dp[i][1][0]
        dp[i+1][1][1] = dp[i][1][1]
    elif a == a1:
        dp[i+1][0][0] = dp[i][0][0]
        dp[i+1][0][1] = dp[i][0][1]
        dp[i+1][1][0] = max(dp[i][1][0], l)
        dp[i+1][1][1] = min(dp[i][1][1], r)
    else:
        dp[i+1][0][0] = l
        dp[i+1][0][1] = r
        dp[i+1][1][0] = L
        dp[i+1][1][1] = R
    L, R = max(L, l), min(R, r)

ans = max(dp[N-1][0][1]-dp[N-1][0][0]+1, 0) + max(dp[N-1][1][1] - dp[N-1][1][0] + 1, 0)
print(ans)

Submission status: Wrong Answer

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

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

dp = [[[0, 0], [0, 0]] for _ in range(N+1)]

L0, R0 = LR[0]
L1, R1 = LR[1]
dp[1][0][0] = L0
dp[1][0][1] = R0
dp[1][1][0] = L1
dp[1][1][1] = R1
L, R = max(L0, L1), min(R0, R1)
for i in range(1, N):
    l, r = LR[i]
    a0 = max(min(dp[i][0][1], r) - max(dp[i][0][0], l)+1, 0) + max(dp[i][1][1] - dp[i][1][0] + 1, 0)
    a1 = max(min(dp[i][1][1], r) - max(dp[i][1][0], l)+1, 0) + max(dp[i][0][1] - dp[i][0][0] + 1, 0)
    a2 = max(R-L+1, 0) + r-l+1
    a = max(a0, a1, a2)
    if a == a0:
        dp[i+1][0][0] = max(dp[i][0][0], l)
        dp[i+1][0][1] = min(dp[i][0][1], r)
        dp[i+1][1][0] = dp[i][1][0]
        dp[i+1][1][1] = dp[i][1][1]
    elif a == a1:
        dp[i+1][0][0] = dp[i][0][0]
        dp[i+1][0][1] = dp[i][0][1]
        dp[i+1][1][0] = max(dp[i][1][0], l)
        dp[i+1][1][1] = min(dp[i][1][1], r)
    else:
        dp[i+1][0][0] = l
        dp[i+1][0][1] = r
        dp[i+1][1][0] = L
        dp[i+1][1][1] = R
    L, R = max(L, l), min(R, r)

ans = max(dp[N][0][1]-dp[N][0][0]+1, 0) + max(dp[N][1][1] - dp[N][1][0] + 1, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
inp = []
maxi = 0
ind = -1
for k in range(N):
    L, R = map(int, sys.stdin.readline().split())
    inp.append([L, R])
    if maxi < R - L:
        maxi = R - L
        ind = k


A = inp[ind]
B = None
now = float("inf")
now_len = None
for k in range(0, N):
    if k == ind:
        continue
    mx = max(inp[k][0], A[0])
    mn = min(inp[k][1], A[1])
    if mx <= mn:
        leng = mn - mx + 1
    else:
        leng = 0
    if now > leng:
        now = leng
        now_len = k

B = inp[now_len]

for k in range(0, N):
    if k == now_len or k == ind:
        continue
    mxa = max(inp[k][0], A[0])
    mna = min(inp[k][1], A[1])
    mxb = max(inp[k][0], B[0])
    mnb = min(inp[k][1], B[1])
    
    

    if mxa > mna:
        ma = 0
    else:
        ma = (mna - mxa + 1)
    
    if mxb > mnb:
        mb = 0
    else:
        mb = (mnb - mxb + 1)

    if (A[1] - A[0] + 1) - ma > (B[1] - B[0] + 1) - mb:
        B = [mxb, mnb]
    else:
        A = [mxa, mna]

result = (A[1] - A[0] + 1) + (B[1] - B[0] + 1)

for i in inp:
    if result < i[1] - i[0] + 1:
        result = i[1] - i[0] + 1

print(result)

Submission status: Wrong Answer

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


def f(c1, c2):
    l, r = -1, -1
    l1, r1 = c1[0], c1[1]
    l2, r2 = c2[0], c2[1]
    if l1 <= l2 <= r1:
        l, r = l2, r1
    if l2 <= l1 <= r2:
        l, r = l1, r2
    if l2 <= l1 and r1 <= r2:
        l, r = l1, r1
    if l1 <= l2 and r2 <= r1:
        l, r = l2, r2
    n = r-l+1
    if l == -1 and r == -1:
        l, r = l1, r1
        n = 0
    return (l, r, n)


def main():
    N = int(input())
    LR = []
    for _ in range(N):
        l, r = map(int, input().split())
        LR.append((l, r, r-l+1))

    LR.sort(key=lambda x: x[2])
    c1 = LR[-1]  # 区間長が最大
    c1_bkup = c1
    LR.remove(c1)

    ans = 0

    LR.sort(key=lambda x: x[0])
    c2 = LR[-1]  # Lが最大
    c2_bkup = c2
    LR.remove(c2)
#    print(LR, c1, c2)
    for x in LR:
        tmp1 = f(c1, x)
        tmp2 = f(c2, x)
        a = c1[2] + c2[2]
        if abs(a - (tmp1[2] + c2[2])) < abs(a - (c1[2] + tmp2[2])):
            c1 = tmp1
        else:
            c2 = tmp2
#        print("c1/c2", c1, c2, ans)
    ans = max(ans, c1[2]+c2[2])

    LR.append(c2_bkup)
    c1 = c1_bkup
    LR.sort(key=lambda x: x[1])
    c2 = LR[0]   # Rが最小
    LR.remove(c2)
#    print(LR, c1, c2)
    for x in LR:
        tmp1 = f(c1, x)
        tmp2 = f(c2, x)
        a = c1[2] + c2[2]
        if abs(a - (tmp1[2] + c2[2])) < abs(a - (c1[2] + tmp2[2])):
            c1 = tmp1
        else:
            c2 = tmp2
#        print("c1/c2", c1, c2)
    ans = max(ans, c1[2]+c2[2])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,-1]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

N = int(input())
List = [list(map(int,input().split())) for i in range(N)]

# Lが最大の問題をfirst_contestにする
max_l = 0
max_l_problem = 0
for i in range(N):
    now_l = List[i][0]
    if max_l < now_l:
        max_l = now_l
        max_l_problem = i
first_contest = List[max_l_problem]

# Rが最小の問題をsecond_contestにする
min_r = 10**9+1
min_r_problem = 0
for i in range(N):
    now_r = List[i][1]
    if min_r > now_r:
        min_r = now_r
        min_r_problem = i
second_contest = List[min_r_problem]

# 順にどちらのコンテストに入れるかを考えていく
for i in range(N):
    pf = pleasure(first_contest,List[i])
    ps = pleasure(second_contest,List[i])
    if (pf[1]-pf[0]+1) - (first_contest[1]-first_contest[0]+1) > (ps[1]-ps[0]+1) - (second_contest[1]-second_contest[0]+1):
        first_contest = pf
    else:
        second_contest = ps

ans1 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1


# 最長問題1つだけのコンテストをつくるパターン
max_len = 0
max_len_problem = 0
for i in range(N):
    now_len = List[i][1]-List[i][0]+1
    if max_len < now_len:
        max_len = now_len
        max_len_problem = i
first_contest = List[max_len_problem]

# 最長問題以外を片方のコンテストに入れる
second_contest = [1,10**9]
for i in range(N):
    if i == max_len_problem:
        continue
    second_contest = pleasure(second_contest,List[i])
        
ans2 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
def pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,-1]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

N = int(input())
List = [list(map(int,input().split())) for i in range(N)]

# Lが最大の問題をfirst_contestにする
max_l = 0
max_l_problem = 0
for i in range(N):
    now_l = List[i][0]
    if max_l < now_l:
        max_l = now_l
        max_l_problem = i
first_contest = List[max_l_problem]

# Rが最小の問題をsecond_contestにする
min_r = 10**9
min_r_problem = 0
for i in range(N):
    now_r = List[i][1]
    if min_r > now_r:
        min_r = now_r
        min_r_problem = i
second_contest = List[min_r_problem]

# 順にどちらのコンテストに入れるかを考えていく
for i in range(N):
    pf = pleasure(first_contest,List[i])
    ps = pleasure(second_contest,List[i])
    if (pf[1]-pf[0]+1) - (first_contest[1]-first_contest[0]+1) > (ps[1]-ps[0]+1) - (second_contest[1]-second_contest[0]+1):
        first_contest = pf
    else:
        second_contest = ps

ans1 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1


# 最長問題1つだけのコンテストをつくるパターン
max_len = 0
max_len_problem = 0
for i in range(N):
    now_len = List[i][1]-List[i][0]+1
    if max_len < now_len:
        max_len = now_len
        max_len_problem = i
first_contest = List[max_len_problem]

# 最長問題以外を片方のコンテストに入れる
second_contest = [1,10**9]
for i in range(N):
    if i == max_len_problem:
        continue
    second_contest = pleasure(second_contest,List[i])
        
ans2 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
def pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,-1]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

N = int(input())
List = [list(map(int,input().split())) for i in range(N)]

first_contest = List[0]

# 問題1に対して最も楽しめない問題をsecond_contestに回す
worst_problem = 0
worst_pleasure_value = 10**9
for i in range(N):
    now_pleasure = pleasure(List[0],List[i])
    now_pleasure_value = now_pleasure[1]-now_pleasure[0]+1
    if worst_pleasure_value > now_pleasure_value:
        worst_pleasure_value = now_pleasure_value
        worst_problem = i
second_contest = List[worst_problem]

# 順にどちらのコンテストに入れるかを考えていく
for i in range(N):
    pf = pleasure(first_contest,List[i])
    ps = pleasure(second_contest,List[i])
    if (pf[1]-pf[0]+1) - (first_contest[1]-first_contest[0]+1) > (ps[1]-ps[0]+1) - (second_contest[1]-second_contest[0]+1):
        first_contest = pf
    else:
        second_contest = ps

ans1 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1


# 問題2に対して最も楽しめない問題をsecond_contestに回す
worst_problem = 0
worst_pleasure_value = 10**9
for i in range(N):
    now_pleasure = pleasure(List[1],List[i])
    now_pleasure_value = now_pleasure[1]-now_pleasure[0]+1
    if worst_pleasure_value > now_pleasure_value:
        worst_pleasure_value = now_pleasure_value
        worst_problem = i
second_contest = List[worst_problem]

# 順にどちらのコンテストに入れるかを考えていく
for i in range(N):
    if i == 1:
        continue
    pf = pleasure(first_contest,List[i])
    ps = pleasure(second_contest,List[i])
    if (pf[1]-pf[0]+1) - (first_contest[1]-first_contest[0]+1) > (ps[1]-ps[0]+1) - (second_contest[1]-second_contest[0]+1):
        first_contest = pf
    else:
        second_contest = ps

ans2 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1


# 最長問題1つだけのコンテストをつくるパターン
max_len = 0
max_len_problem = 0
for i in range(N):
    now_len = List[i][1]-List[i][0]+1
    if max_len < now_len:
        max_len = now_len
        max_len_problem = i
first_contest = List[max_len_problem]

# 最長問題以外を片方のコンテストに入れる
second_contest = [1,10**9]
for i in range(N):
    if i == max_len_problem:
        continue
    second_contest = pleasure(second_contest,List[i])
        
ans3 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1

print(max(ans1,ans2,ans3))

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter
import numpy as np
read = sys.stdin.read


def main():
    N, *LR = map(int, read().split())

    LR = sorted(zip(*[iter(LR)] * 2), key=itemgetter(0))
    L, R = zip(*LR)

    left = 0
    right = 10 ** 19
    day1 = [0] * N

    for i in range(N - 1):
        if left < L[i]:
            left = L[i]
        if right > R[i]:
            right = R[i]
        day1[i] = right - left + 1

    day2 = [0] * N
    left = 0
    right = 10 ** 19
    for i in range(N - 1, 0, -1):
        if left < L[i]:
            left = L[i]
        if right > R[i]:
            right = R[i]
        day2[i] = right - left + 1

    day1 = np.array([0] + day1)
    day2 = np.array(day2 + [0])
    answer1 = (day1 + day2).max()

    answer2 = (np.array(R) - np.array(L) + 1).max()
    print(max(answer1, answer2))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter
import numpy as np
read = sys.stdin.read


def main():
    N, *LR = map(int, read().split())

    LR = sorted(zip(*[iter(LR)] * 2), key=itemgetter(0))
    L, R = zip(*LR)

    left = 0
    right = 10 ** 19
    day1 = [0] * N

    for i in range(N - 1):
        if left < L[i]:
            left = L[i]
        if right > R[i]:
            right = R[i]
        day1[i] = right - left + 1

    day2 = [0] * N
    left = 0
    right = 10 ** 19
    for i in range(N - 1, 0, -1):
        if left < L[i]:
            left = L[i]
        if right > R[i]:
            right = R[i]
        day2[i] = right - left + 1

    day1 = np.array([0] + day1)
    day2 = np.array(day2 + [0])
    answer1 = (day1 + day2).max()

    answer2 = (np.array(R) - np.array(L)).max()
    print(max(answer1, answer2))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,-1]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

N = int(input())
List = [list(map(int,input().split())) for i in range(N)]

first_contest = List[0]

# 問題1に対して最も楽しめない問題をsecond_contestに回す
worst_problem = 0
worst_pleasure_value = 10**9
for i in range(N):
    now_pleasure = pleasure(List[0],List[i])
    now_pleasure_value = now_pleasure[1]-now_pleasure[0]+1
    if worst_pleasure_value > now_pleasure_value:
        worst_pleasure_value = now_pleasure_value
        worst_problem = i
second_contest = List[worst_problem]

# 順にどちらのコンテストに入れるかを考えていく
for i in range(N):
    pf = pleasure(first_contest,List[i])
    ps = pleasure(second_contest,List[i])
    if (pf[1]-pf[0]+1) - (first_contest[1]-first_contest[0]+1) > (ps[1]-ps[0]+1) - (second_contest[1]-second_contest[0]+1):
        first_contest = pf
    else:
        second_contest = ps

ans1 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1


# 最長問題1つだけのコンテストをつくるパターン
max_len = 0
max_len_problem = 0
for i in range(N):
    now_len = List[i][1]-List[i][0]+1
    if max_len < now_len:
        max_len = now_len
        max_len_problem = i
first_contest = List[max_len_problem]

# 最長問題以外を片方のコンテストに入れる
second_contest = [1,10**9]
for i in range(N):
    if i == max_len_problem:
        continue
    second_contest = pleasure(second_contest,List[i])
        
ans2 = first_contest[1]-first_contest[0]+1+second_contest[1]-second_contest[0]+1

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()

def resolve():
    n=int(input())
    I=[tuple(map(int,input().split())) for _ in range(n)]
    I.sort(lambda x:x[1])
    forL=[0]*n
    forL[0]=I[0][0]
    backL=[0]*n
    backL[-1]=I[-1][0]
    for i in range(n-1):
        forL[i+1]=max(forL[i],I[i+1][0])
        backL[-i-2]=max(backL[-i-1],I[-i-2][0])

    def calc(i): # iは1からn-1まで
        score=0
        score+=max(I[0][1]-forL[i-1]+1,0)
        score+=max(I[i][1]-backL[i]+1,0)
        return score

    ans=max(calc(i) for i in range(1,n))

    # R-Lが最大のものだけ別に分ける
    w,j=max((I[i][1]-I[i][0],i) for i in range(n))
    L=0
    R=INF
    for i in range(n):
        if(i==j): continue
        L=max(L,I[i][0])
        R=min(R,I[i][1])
    print(max(ans,R-L+1+w+1))
resolve()

Submission status: Wrong Answer

Solution code:
import os
import sys
from collections import defaultdict as ddic, Counter, deque
from itertools import combinations, permutations, product
import bisect, heapq

FAST_INPUT = 0
if FAST_INPUT:
    from atexit import register
    from io import BytesIO
 
    sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
    sys.stdout = BytesIO()
    register(lambda: os.write(1, sys.stdout.getvalue()))
 
rr = lambda: sys.stdin.readline().rstrip('\r\n')
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
MOD = 10**9 + 7

DEBUG = 0
if DEBUG:
    import random
    random.seed(0)
    ri = random.randint

#####

def solve(N, A):
    A.sort(key = lambda (x, y): (x, -y))
    B = [None] * N
    my = A[-1][1]
    for i in xrange(N-1, -1, -1):
        x, y = A[i]
        if y < my: my = y
        B[i] = my
    topx = A[-1][0]

    ans = max(y-x for x,y in A)
    botx, boty = min(A, key = lambda (x, y): y)
    for i in xrange(N-1):
        x, y = A[i]
        if x > botx: botx = x
        if y < boty: boty = y
        cand = max(0, boty-botx+1) + max(0, B[i+1]-topx+1)
        if cand > ans: ans = cand
    return ans

#####

N = rri()
A = [rrm() for _ in xrange(N)]
print solve(N, A)

Submission status: Wrong Answer

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

NN = 17
MA = [0] * ((1<<NN+1)-1)
C = [0] * (1<<NN)

def update(a, x):
    i = (1<<NN) - 1 + a
    if x > 0:
        C[a] += 1
        if MA[i] == 0:
            MA[i] = a
    else:
        C[a] -= 1
        if C[a] == 0:
            MA[i] = 0
    while True:
        i = (i-1) // 2
        MA[i] = max(MA[2*i+1], MA[2*i+2])
        if i == 0:
            break

def rangemax(a, b):
    l = a + (1<<NN)
    r = b + (1<<NN)
    ma = -1
    while l < r:
        if l%2:
            ma = max(ma, MA[l-1])
            l += 1
        if r%2:
            r -= 1
            ma = max(ma, MA[r-1])
        l >>= 1
        r >>= 1
    return ma

N = int(input())
X = []
for _ in range(N):
    l, r = map(int, input().split())
    X.append((l, r))
X = sorted(X, key = lambda x: x[1])

ans = 0
for l, r in X:
    ans = max(ans, r-l+1)
X = sorted(X, key = lambda x: x[0])
BL, BR = X.pop()
L = [X[i][0] for i in range(N-1)]
D = {}
prev = -1
j = -1
for i in range(N-1):
    if L[i] != prev:
        j += 1
    else:
        prev = L[i]
    D[L[i]] = j
    update(j, 1)
X = sorted(X, key = lambda x: -x[1])
AL, AR = X.pop()
for l, r in X:
    if AR >= BL:
        ans = max(ans, min(AR,BR)-BL+r-l+2)
    AL = max(AL, l)
AL = L[rangemax(0, 1<<NN)]
if AR >= AL and BR >= BL:
    ans = max(ans, AR-AL+BR-BL+2)
for l, r in X:
    update(D[l], -1)
    BR = min(BR, r)
    AL = L[rangemax(0, 1<<NN)]
    if AR >= AL and BR >= BL:
        ans = max(ans, AR-AL+BR-BL+2)

print(ans)

Submission status: Wrong Answer

Solution code:
import os
import sys
from collections import defaultdict as ddic, Counter, deque
from itertools import combinations, permutations, product
import bisect, heapq

FAST_INPUT = 0
if FAST_INPUT:
    from atexit import register
    from io import BytesIO
 
    sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
    sys.stdout = BytesIO()
    register(lambda: os.write(1, sys.stdout.getvalue()))
 
rr = lambda: sys.stdin.readline().rstrip('\r\n')
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
MOD = 10**9 + 7

DEBUG = 0
if DEBUG:
    import random
    random.seed(0)
    ri = random.randint

#####

def solve(N, A):
    A.sort(key = lambda (x, y): (x, -y))
    B = [None] * N
    my = A[-1][1]
    for i in xrange(N-1, -1, -1):
        x, y = A[i]
        if y < my: my = y
        B[i] = my
    topx = A[-1][0]

    ans = max(y-x for x,y in A)
    botx, boty = A[0]
    for i in xrange(N-1):
        x, y = A[i]
        if x > botx: botx = x
        if y < boty: boty = y
        cand = max(0, boty-botx+1) + max(0, B[i+1]-topx+1)
        if cand > ans: ans = cand
    return ans

#####

N = rri()
A = [rrm() for _ in xrange(N)]
print solve(N, A)

Submission status: Wrong Answer

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

LR.sort()
L_1=0
L_2=0
R_1=10**9
R_2=10**9
l_1=[]
l_2=[]
for i in range(N-1):
  L_1=max(L_1,LR[i][0])
  R_1=min(R_1,LR[i][1])
  l_1.append(max(R_1-L_1+1,0))
  
for i in range(N-1,0,-1):
  L_2=max(L_2,LR[i][0])
  R_2=min(R_2,LR[i][1])
  l_2.append(max(R_2-L_2+1,0))
  

a=[]
l_2.reverse()
for i in range(N-1):
  a.append(l_1[i]+l_2[i])
  
print(max(a))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import sys
input = sys.stdin.readline
N = int(input())
LR = []
for _ in range(N):
    l, r = map(int, input().split())
    LR.append([l, r+1])
ans = 0
for l, r in LR:
    ans = max(ans, r - l)

LR.sort(key=itemgetter(1))

sch = 0
rr = 0
for l, r in LR:
    if l >= rr:
        rr = r
        sch += 1
#print(sch)
if sch >= 3:
    print(ans)
elif sch == 2:
    # ここは合ってる
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
            pl = l
    assert len(LR_) >= 2
    LR = LR_
    Ll, Lr = LR[0]
    Rl, Rr = LR[-1][0], LR[1][1]
    ans = max(ans, max(Rr-Rl, 0) + Lr-Ll)
    for i, (l, r) in enumerate(LR[1:-1], 1):
        Ll = l
        Rr = LR[i+1][1]
        ans = max(ans, max(Rr-Rl, 0) + max(Lr-Ll, 0))
    print(ans)
else:
    mid_l = 0
    mid_r = float("inf")
    for l, r in LR:
        mid_l = max(l, mid_l)
        mid_r = min(r, mid_r)
    for i in range(N):
        LR[i][1] -= mid_r-mid_l
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
            pl = l
    if not len(LR_) >= 2:
        print(ans + mid_r - mid_l)
        exit()
    assert mid_r - mid_l > 0
    assert LR_ == sorted(LR_)
    an = 0
    LR, LR_ = LR_, LR
    Ll, Lr = LR[0]
    Rl, Rr = LR[-1][0], LR[1][1]
    an = max(an, max(Rr-Rl, 0) + Lr-Ll)
    for i, (l, r) in enumerate(LR[1:-1], 1):
        Ll = l
        Rr = LR[i+1][1]
        an = max(an, max(Rr-Rl, 0) + max(Lr-Ll, 0))
    an += (mid_r - mid_l) * 2
    print(an)