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:
def f(li, new):
    l1, r1, d1 = li[0]
    l2, r2, d2 = li[1]
    l3, r3, d3 = new
    l = [l1, l2, l3]
    r = [r1, r2, r3]
    d = [d1, d2, d3]

    mn = float("inf")
    for i in range(3):
        new_l = max(l[i-1], l[i-2])
        new_r = min(r[i-1], r[i-2])
        new_d = max(0, new_r - new_l + 1)
        d_diff = d[i-1] + d[i-2] - new_d
        if d_diff < mn:
            mn = d_diff
            left = i
            new = [new_l, new_r, new_d]

    ret = [[l[left], r[left], d[left]], new]
    ret.sort(reverse=True, key=lambda x: x[2])
    return ret


def main():
    n = int(input())
    lr = [list(map(int, input().split())) for _ in range(n)]

    lrd = []
    for l, r in lr:
        lrd.append([l, r, r - l + 1])

    lrd.sort(reverse=True, key=lambda x: x[2])

    ans = [lrd[0], lrd[1]]

    for li in lrd[2:]:
        ans = f(ans, li)

    print(ans[0][2] + ans[1][2])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
maxlen = int(0)
N = I()
LR =[[] for _ in range(N)]
for i in range(N):
    LR[i] = LI()
    C = LR[i][1] - LR[i][0]
    if C>maxlen:
        maxlen = C

LR.sort()
rightend = LR[0][1]
leftend  = LR[N-1][0]

live1 = [[] for _ in range(N)]
live2 = [[] for _ in range(N)]
live1[0] = LR[0]
live2[N-1] = LR[N-1]
#print(rightend)
#print(leftend)

cur1 = -1; cur2 = -1
for i in range(1,N):
    if rightend<LR[i][0]:
        cur1 = i-1
        break
    live1[i] = [max(live1[i-1][0],LR[i][0]),min(live1[i-1][1],LR[i][1])]
    if live1[i-1][1]>LR[i][1]:
        rightend = LR[i][1]

for i in range(N-2,-1,-1):
    if leftend>LR[i][1]:
        cur2 = i+1
        break
    live2[i] = [max(live2[i+1][0],LR[i][0]),min(live2[i+1][1],LR[i][1])]
#print(cur1,cur2)
#print(live1)
#print(live2)

if cur1==-1:
    cur1=N-1
if cur2==-1:
    cur2=1
ansC = [0]*N
for i in range(cur2,cur1+1):
    ansC[i] = (live1[i-1][1]-live1[i-1][0]+1) + (live2[i][1]-live2[i][0]+1)

ans = max(max(ansC),maxlen+1)

print(ans)

Submission status: Wrong Answer

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


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

    LR = list(zip(*[iter(LR)] * 2))
    LR.sort()

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

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

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

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

    print(answer.max())


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ML = max(LR)[0]
mR = min(LR,key=lambda x:x[1])[1]

if max(LR) != [ML,mR]:
    LR.sort(key=lambda x:[x[1],x[0]])
    L = [LR[i][0] for i in range(N)]
    for i in range(N):
        r = LR[i][1]
        if ML <= r:
            break

    L_ = [0]*N
    for j in range(N):
        L_[j] = max(L[j],L_[j-1])

    ans = LR[i][1]-ML+1
    for j in range(max(i,1),N):
        ans = max(ans,LR[j][1]-ML+1+max(mR-L_[j-1]+1,0))

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    L,R = map(int,input().split())
    A.append([L,R,i])
B = sorted(A)
C = sorted(A, key=lambda x: x[1])
l0,l1,m0,m1 = B[N-1][0],B[N-1][1],C[0][0],C[0][1]
I,J = B[N-1][2],C[0][2]
M = 0
if I == J:
    for i in range(N):
        a1,a0 = A[i][1],A[i][0]
        if M <= a1-a0:
            M = a1-a0
    ans = m1-l0 + M+2
else:
    for i in range(N):
        if i == I or i == J:
            continue
        a1,a0 = A[i][1],A[i][0]
        if M <= a1-a0:
            M = a1-a0
    ans1 = m1-l0 + M
    a,b = l1-l0,m1-m0
    for i in range(N):
        if i == I or i == J:
            continue
        a1,a0 = A[i][1],A[i][0]
        if min([l1,a1])-l0 + m1-m0>l1-l0 + m1-max([m0,a0]):
            l1 = min([l1,a1])
        else:
            m0 = max([m0,a0])
    ans2 = l1-l0+m1-m0
    ans = max([ans1+2,ans2+2,0])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#AGC40 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))


n = I()
lr = [LI() for _ in range(n)]
L = sorted(lr,key = lambda x:(x[0],x[1]))
R = sorted(lr,key = lambda x:(x[1],x[0]))
#print(L,R)
hr = []
for i,[l,r] in enumerate(L[1:]):
    heappush(hr,(r,i))
tmp = 0
if L[0][1] - L[0][0] <= 0:
    tmp = -1
if hr[0][0] - L[-1][0] <= 0:
    tmp -= 1
ans1 = (L[0][1] - L[0][0]) + max(0,hr[0][0] - L[-1][0]) + 2 + tmp
m = L[0][1]
for i,[l,r] in enumerate(L[1:-1]):
    if r < m:
        m = r
    while hr:
        M,j = heappop(hr)
        if j > i:
            break
    #print(ans1,m,l,M,L[-1][0])
    tmp = 0
    if m - l <= 0:
        tmp = -1
    if M - L[-1][0] <= 0:
        tmp -= 1
    ans1 = max(ans1,max(0,m - l) + max(0,M - L[-1][0]) + 2 + tmp)
hl = []
for i,[l,r] in enumerate(R[1:]):
    heappush(hl,(-l,i))
tmp = 0
if R[0][1] - R[0][0] <= 0:
    tmp = -1
if R[1][1] - (-hl[0][0]) <= 0:
    tmp -= 1
ans2 = (R[0][1] - R[0][0]) + max(0,R[1][1] - (-hl[0][0])) + 2 + tmp
m = R[0][0]
for i,[l,r] in enumerate(R[1:-1]):
    m = max(m,l)
    while hl:
        M,j = heappop(hl)
        if j > i:
            break
    tmp = 0
    if R[0][1] - m <= 0:
        tmp = -1
    if R[i+1][1] - abs(M) <= 0:
        tmp -= 1
    ans2 = max(ans2,max(0,R[0][1] - m) + max(0,R[i+1][1] - abs(M)) + 2 + tmp)
    #print(ans2,R[0][1],m,R[i+1][1],abs(M))
print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
M = []
max_i = 0
max_lr = 0
for i in range(N):
    L, R = map(int, input().split())
    LR = R - L + 1
    if LR > max_lr:
        max_lr = LR
        max_i = i
    M.append((L, R, LR))

M.sort()
minR1 = [0]*N
minR2 = [0]*N

tmp = M[0][1]
minR1[0] = tmp
for i in range(1, N):
    tmp = min(M[i][1], tmp)
    minR1[i] = tmp

tmp = M[-1][1]
minR2[-1] = tmp
for i in range(N-2, -1, -1):
    tmp = min(M[i][1], tmp)
    minR2[i] = tmp


ans = 0
for i in range(0, N-1):
    L1 = M[i][0]
    L2 = M[-1][0]
    R1 = minR1[i]
    R2 = minR2[i+1]
    ans = max(ans, R1-L1+1 + R2-L2+1)
    
for i in range(N):
    if i == max_i:
        pass
    L1 = max(L1, M[i][0])
    R1 = min(R1, M[i][1])
ans = max(ans, R1-L1+1 + max_lr)

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
l=sorted(l,key=lambda x: x[0]-x[1])
ans1=l[0][1]-l[0][0]+1
lm=-1
rm=10**10
for i in range(1,n):
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
ans1+=max(0,rm-lm+1)
print(ans1)

Submission status: Wrong Answer

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

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

mR = float("inf")
ML = -float("inf")
indmR = 0
indML = 0
lenmR = 0
lenML = 0
for i in range(n):
    if mR > A[i][1] or (mR == A[i][1] and lenmR >= A[i][1]-A[i][0]+1):
        lenmR = A[i][1]-A[i][0]+1
        indmR = i
        mR = A[i][1]
    if ML < A[i][0] or (ML == A[i][0] and lenML > A[i][1]-A[i][0]+1):
        lenML = A[i][1]-A[i][0]+1
        indML = i
        ML = A[i][0]

ans = 0
lenmax2 = 0
for i in range(n):
    if i != indML and i != indmR:
        lenmax2 = max(lenmax2, A[i][1]-A[i][0]+1)

ans = max(0, mR-ML+1) + lenmax2
# print("ans",ans)

if indML != indmR:
    
    aa =[[ML, A[indML][1]], [A[indmR][0], mR]]
    aa_h = [[j for j in i] for i in aa]

    if indmR > indML:
        A.pop(indmR)
        A.pop(indML)
    else:
        A.pop(indML)
        A.pop(indmR)
    # print(A)
    # print(aa)
    
    l = 0
    r = aa[1][1]-aa[1][0]+aa[0][1]-aa[0][0]+2
    m = (l+r)//2
    while r-l>1:
        aa = [[j for j in i] for i in aa_h]
        for i in range(len(A)):
            # print(aa_h[0][1] , A[i][1] , m)
            if aa_h[0][1] - A[i][1] > m:
                aa[1][0] = max(aa[1][0], A[i][0])
            else:
                aa[0][1] = min(aa[0][1], A[i][1])
        # print("aa",aa,m)
        if aa_h[0][1] - aa[0][1] + aa[1][0] - aa_h[1][0] > m:
            l = m
        else:
            r = m
        m = (l+r)//2

    # print("ans", l+1)
    aa = [[j for j in i] for i in aa_h]
    ans = max(ans, aa[1][1]-aa[1][0]+aa[0][1]-aa[0][0]+2-(l+1))
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    非再帰
    segment tree
    '''

    def __init__(self, n, func, init=float('inf')):
        '''
        n->配列の長さ
        func:func(a,b)->val,　func=minだとRMQになる
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(n-1).bit_length()
        self.init = init
        self.data = [init]*(2*self.n)
        self.func = func

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def build(self):
        '''
        setの後に一斉更新
        '''
        for k in reversed(range(self.n-1)):
            self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

        while k > 0:
            k = (k-1)//2
            self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])

    def query(self, l, r):
        '''
        [l,r)のfuncを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.init
        while L < R:
            if R & 1:
                R -= 1
                ret = self.func(ret, self.data[R-1])
            if L & 1:
                ret = self.func(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


N = int(input())
L = [list(map(int, input().split())) for _ in range(N)]
L = sorted(L, key=lambda x: x[0])

Seg_L = SegmentTree(N, lambda a, b: max(a, b), init=0)
Seg_R = SegmentTree(N, lambda a, b: min(a, b), init=float('inf'))

for i, q in enumerate(L):
    l, r = q[0], q[1]
    Seg_L.set(i, l)
    Seg_R.set(i, r)
Seg_L.build()
Seg_R.build()
cand = []
for pivot in range(1, N):
    low_1, high_1 = Seg_L.query(0, pivot), Seg_R.query(0, pivot)
    low_2, high_2 = Seg_L.query(pivot, N), Seg_R.query(pivot, N)
    ans = high_1+high_2-low_1-low_2+2
    cand.append(ans)
ans1 = max(cand)

max_d = max([r-l for l, r in L])
min_d = min([r-l for l, r in L])
ans2 = max_d-min_d
print(max(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
l=sorted(l,key=lambda x: x[0]-x[1])
ans1=l[0][1]-l[0][0]+1
lm=-1
rm=10**10
for i in range(1,n):
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
ans1+=max(0,rm-lm+1)

Submission status: Wrong Answer

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

ML = max(LR)[0]
mR = min(LR,key=lambda x:x[1])[1]

if max(LR) != [ML,mR]:
    LR.sort(key=lambda x:[x[1],x[0]])
    L = [LR[i][0] for i in range(N)]
    for i in range(N):
        r = LR[i][1]
        if ML <= r:
            break

    L_ = [0]*N
    for j in range(N):
        L_[j] = max(L[j],L_[j-1])

    ans = 0
    for j in range(max(i,1),N):
        ans = max(ans,LR[j][1]-ML+1+mR-L_[j-1]+1)

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

print(ans)

Submission status: Wrong Answer

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

lis.sort(key=lambda x:x[1])
lis.sort(key=lambda x:x[0])
  #print(left)

min_right = [0 for i in range(n)]
now = 10 ** 9
for i in range(n):
    now = min(now,lis[i][1])
    min_right[i] = now


right_max_left = lis[-1][0]
right_min_right = lis[-1][1]
left_max_left = 0
left_min_right = 0

ans = 0

for i in range(1,n):
    left_max_left = lis[-(i+1)][0]
    left_min_right = min_right[-(i+1)]

    ans = max(ans,max(0,left_min_right-left_max_left+1)+max(0,right_min_right-right_max_left+1))
  #    print(left_max_left,left_min_right,right_max_left,right_min_right)

    right_min_right = min(right_min_right,lis[-(i+1)][1])
    right_max_left = max(right_max_left,lis[-(i+1)][0])


lis.sort(key=lambda x: x[0])
lis.sort(key=lambda x: x[1], reverse=True)
# print(list)

max_left = [0 for i in range(n)]
now = 0
for i in range(n):
    now = max(now, lis[i][0])
    max_left[i] = now

right_max_left = 0
right_min_right = 0
left_max_left = lis[-1][0]
left_min_right = lis[-1][1]


for i in range(1, n):
    right_min_right = lis[-(i + 1)][1]
    right_max_left = max_left[-(i + 1)]

    ans = max(ans, max(0, left_min_right - left_max_left + 1) + max(0, right_min_right - right_max_left + 1))
    #    print(left_max_left,left_min_right,right_max_left,right_min_right)

    left_min_right = min(left_min_right, lis[-(i + 1)][1])
    left_max_left = max(left_max_left, lis[-(i + 1)][0])

print(ans)

Submission status: Wrong Answer

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


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

    LR = list(zip(*[iter(LR)] * 2))
    LR.sort()

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

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

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

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

    print(answer.max())

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

Lsorted = sorted(LR,key = lambda x: x[0],reverse=True)
Rsorted = sorted(LR,key = lambda x: x[1])

Lmax = Lsorted[0]
Rmin = Rsorted[0]
if Lmax[0]==Rmin[0] and Lmax[1]==Rmin[1]:
    LRdif = [LR[i][1]-LR[i][0]+1 for i in range(N)]
    LRdif.sort()
    print(Lmax[1]-Lmax[0]+1+LRdif[-1])
else:
#    M1 = Lmax[1]
#    M2 = Rmin[0]
#    for l,r in LR:
#        if r-Lmax[0] > Rmin[1]-l:
#            M1 = max(min(r,M1),Lmax[0]-1)
#        else:
#            M2 = min(max(l,M2),Rmin[1]+1)
#    print(M1-Lmax[0]+1 + Rmin[1]-M2+1)
    Rsorted = sorted(sorted(LR,key=lambda x:x[0]),key=lambda x:[1])
    maxL = Rmin[0]
    maxscore = 0
    for i in range(N-1):
        if Rsorted[i][2]!=Lmax[2]:
            maxL = max(Rsorted[i][0],maxL)
            nowminR = Rsorted[i+1][1]
            maxscore =max(maxscore, max(nowminR-Lmax[0]+1,0) + max(Rmin[1]-maxL+1,0))
        else:
            break
                
#    Lsorted = sorted(sorted(LR,key=lambda x:x[1],reverse=True),key=lambda x:[0],reverse=True)
#    minR = Lmax[1]
#    for i in range(1,N):
#        nowmaxL = min(Lsorted[i][0],Rmin[0])
#        maxscore =max(maxscore, max(minR-Lmax[0]+1,0) + max(Rmin[1]-nowmaxL+1,0))
#        minR = max(Lsorted[i][1],minR)

    print(maxscore)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    L,R = map(int,input().split())
    A.append([L,R,i])
B = sorted(A)
C = sorted(A, key=lambda x: x[1])
l0,l1,m0,m1 = B[N-1][0],B[N-1][1],C[0][0],C[0][1]
I,J = B[N-1][2],C[0][2]
M = 0
if I == J:
    for i in range(N):
        a1,a0 = A[i][1],A[i][0]
        if M <= a1-a0:
            M = a1-a0
    ans = m1-l0 + M+2
else:
    for i in range(N):
        if i == I or i == J:
            continue
        a1,a0 = A[i][1],A[i][0]
        if M <= a1-a0:
            M = a1-a0
    ans1 = m1-l0 + M
    a,b = l1-l0,m1-m0
    for i in range(N):
        if i == I or i == J:
            continue
        a1,a0 = A[i][1],A[i][0]
        if min([l1,a1])-max([l0,a0]) > min([m1,a1])-max([m0,a0]):
            l1 = min([l1,a1])
            l0 = max([l0,a0])
        else:
            m1 = min([m1,a1])
            m0 = max([m0,a0])
    ans2 = l1-l0+m1-m0
    ans = max([ans1,ans2]) +2
print(ans)

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[0]
    c2 = LR[-1]
#    print(LR, c1, c2)
    for i in range(1, len(LR)-1):
        x = LR[i]
        tmp1 = f(c1, x)
        tmp2 = f(c2, x)
        a = c1[2] + c2[2]
        if abs(a - (tmp1[2] + c2[2])) < abs(a - (c2[2] + tmp2[2])):
            c1 = tmp1
        else:
            c2 = tmp2
#        print("c1/c2", c1, c2)
    print(c1[2]+c2[2])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
lr=[[0]*2 for _ in range(n)]
for i in range(n):
    lr[i] = list( map(int,input().split()) )
sortedlr = sorted(lr,key=lambda x:x[0])


c1right=sortedlr[0][1]
C1L=[]
C1R=[]

c2right=sortedlr[-1][1]
c2left =sortedlr[-1][0]
C2L=[c2left]*(n-1)
C2R=[]
for i in range(1,n):
    c1left=sortedlr[i-1][0]
    c1right=min(c1right,sortedlr[i-1][1])
#    print(c1left,c1right)
    C1L.append(c1left)
    C1R.append(c1right)

    c2right=min(c2right,sortedlr[n-i][1])
#    print(c2left,c2right)
    C2R.append(c2right)
#    print()
C2R.reverse()

print( max( C1R[i]-C1L[i]+1 + C2R[i]-C2L[i]+1 for i in range(len(C1R))) )

Submission status: Wrong Answer

Solution code:
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(int,input().split())))
raw.sort(key= lambda x: x[1])
EF = []
for i in range(n):
  EF.append(raw[i][1])
SL1 = [raw[0][0]]
for i in range(1,n):
  SL1.append(max(SL1[-1],raw[i][0]))
SL2 = [raw[-1][0]]
for i in range(1,n):
  SL2.append(max(SL2[-1],raw[n-i-1][0]))
SL2.reverse()
#print(EF)
#print(SL1)
#print(SL2)
can = []
for i in range(n-1):
  tmp = max(EF[0]-SL1[i]+1,0) + max(EF[i+1]-SL2[i+1]+1,0)
  can.append(tmp)
#print(can)
print(max(can))

Submission status: Wrong Answer

Solution code:
n = int(input())

l1, r1 = map(int, input().split())
g1 = [l1, r1]
g2 = []
g3 = []
M = r1 - l1 + 1

for i in range(n-1):
    l, r = map(int, input().split())
    M = max(M, r-l+1)
    if r <= g1[0] or l >= g1[1]:
        if g2 == []:
            g2.append(l)
            g2.append(r)
        elif r <= g2[0] or l >= g2[1]:
            g3.append([l, r])
        else:
            g2[0] = max(g2[0], l)
            g2[1] = min(g2[1], r)
    else:
        g1[0] = max(g1[0], l)
        g1[1] = min(g1[1], r)

if g2 == []:
    print(M + g1[1]-g1[0]+1)
elif g3 == []:
    print(max(M, g1[1]-g1[0]+1 + g2[1]-g2[0]+1))
else:
    print(M)

Submission status: Wrong Answer

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

list.sort(key=lambda x:x[1])
list.sort(key=lambda x:x[0])
  #print(left)

min_right = [0 for i in range(n)]
now = 10 ** 9
for i in range(n):
    now = min(now,list[i][1])
    min_right[i] = now


right_max_left = list[-1][0]
right_min_right = list[-1][1]
left_max_left = 0
left_min_right = 0

ans = 0

for i in range(1,n):
    left_max_left = list[-(i+1)][0]
    left_min_right = min_right[-(i+1)]

    ans = max(ans,max(0,left_min_right-left_max_left+1)+max(0,right_min_right-right_max_left+1))
  #    print(left_max_left,left_min_right,right_max_left,right_min_right)

    right_min_right = min(right_min_right,list[-(i+1)][1])
    right_max_left = max(right_max_left,list[-(i+1)][0])


list.sort(key=lambda x: x[0])
list.sort(key=lambda x: x[1], reverse=True)
# print(list)

max_left = [0 for i in range(n)]
now = 0
for i in range(n):
    now = max(now, list[i][0])
    max_left[i] = now

right_max_left = 0
right_min_right = 0
left_max_left = list[-1][0]
left_min_right = list[-1][1]


for i in range(1, n):
    right_min_right = list[-(i + 1)][1]
    right_max_left = max_left[-(i + 1)]

    ans = max(ans, max(0, left_min_right - left_max_left + 1) + max(0, right_min_right - right_max_left + 1))
    #    print(left_max_left,left_min_right,right_max_left,right_min_right)

    left_min_right = min(left_min_right, list[-(i + 1)][1])
    left_max_left = max(left_max_left, list[-(i + 1)][0])

print(ans)

Submission status: Wrong Answer

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

ls.sort(key = lambda x:x[0])

ans_ls = [0] * (n-1)

for i in range(n-1):
    p1 = ls[i][0]
    p2 = 1001001001001001
    p3 = ls[i+1][0]
    p4 = 1001001001001001
    for k in ls[:i+1]:
        p2 = min(p2, k[1])
    ans_ls[i] += max(p2 - p1 + 1, 0)

    for k in ls[i+1:]:
        p4 = min(p4, k[1])
    ans_ls[i] += max(p4 - p3 + 1, 0)

print(max(ans_ls))

Submission status: Wrong Answer

Solution code:
import sys

def length(L1,L2,R1,R2):
    L = max(L1,L2)
    R = min(R1,R2)

    return R - L + 1

def leng(L,R):
    return R - L + 1

N = int(input())
L = [0] * N
R = [0] * N
Max = 0
Max_i = 0
for i in range(N):
    L[i], R[i] = map(int, input().split())
    if Max < R[i] - L[i] + 1:
        Max = R[i] - L[i] + 1
        Max_i = i

Min = 10**9
Min_i = 0

for i in range(N):
    if Min > (min(R[Max_i],R[i]) - max(L[Max_i],L[i])):
        Min = min(R[Max_i],R[i]) - max(L[Max_i],L[i])
        Min_i = i

left = [L[Max_i],R[Max_i]]
right = [L[Min_i],R[Min_i]]

for i in range(N):
    if length(L[i],left[0],R[i],left[1]) + leng(right[0],right[1]) > leng(left[0],left[1]) + length(L[i],right[0],R[i],right[1]):
        left[0] = max(left[0],L[i])
        left[1] = min(left[1],R[i])

    else:
        right[0] = max(right[0],L[i])
        right[1] = min(right[1],R[i])

    if Max > leng(left[0],left[1]) + leng(right[0],right[1]):
        print(Max)
        sys.exit()

print(leng(left[0],left[1]) + leng(right[0],right[1]))

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
maxlen = int(0)
N = I()
LR =[[] for _ in range(N)]
for i in range(N):
    LR[i] = LI()
    C = LR[i][1] - LR[i][0]
    if C>maxlen:
        maxlen = C

LR.sort()
rightend = LR[0][1]
leftend  = LR[N-1][0]

live1 = [[] for _ in range(N)]
live2 = [[] for _ in range(N)]
live1[0] = LR[0]
live2[N-1] = LR[N-1]
#print(rightend)
#print(leftend)

cur1 = -1; cur2 = -1
for i in range(1,N):
    if rightend<LR[i][0]:
        cur1 = i-1
        break
    live1[i] = [max(live1[i-1][0],LR[i][0]),min(live1[i-1][1],LR[i][1])]
    if live1[i-1][1]>LR[i][1]:
        rightend = LR[i][1]

for i in range(N-2,-1,-1):
    if leftend>LR[i][1]:
        cur2 = i+1
        break
    live2[i] = [max(live2[i+1][0],LR[i][0]),min(live2[i+1][1],LR[i][1])]
#print(cur1,cur2)
#print(live1)
#print(live2)

if cur1==-1:
    cur1=N-2
if cur2==-1:
    cur2=1
ansC = [0]*N
for i in range(cur2,cur1+2):
    ansC[i] = (live1[i-1][1]-live1[i-1][0]+1) + (live2[i][1]-live2[i][0]+1)

ans = max(max(ansC),maxlen+1)

print(ans)

Submission status: Wrong Answer

Solution code:

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

Lsorted = sorted(LR,key = lambda x: x[0],reverse=True)
Rsorted = sorted(LR,key = lambda x: x[1])

Lmax = Lsorted[0]
Rmin = Rsorted[0]
if Lmax[0]==Rmin[0] and Lmax[1]==Rmin[1]:
    LRdif = [LR[i][1]-LR[i][0]+1 for i in range(N)]
    LRdif.sort()
    print(Lmax[1]-Lmax[0]+1+LRdif[-1])
else:
#    M1 = Lmax[1]
#    M2 = Rmin[0]
#    for l,r in LR:
#        if r-Lmax[0] > Rmin[1]-l:
#            M1 = max(min(r,M1),Lmax[0]-1)
#        else:
#            M2 = min(max(l,M2),Rmin[1]+1)
#    print(M1-Lmax[0]+1 + Rmin[1]-M2+1)
    Rsorted = sorted(sorted(LR,key=lambda x:x[0]),key=lambda x:[1])
    maxL = Rmin[0]
    maxscore = 0
    for i in range(N):
        if Rsorted[i][2]!=Lmax[2]:
            if i!=N-1:
                maxL = max(Rsorted[i][0],maxL)
                nowminR = min(Rsorted[i+1][1],Lmax[1])
                maxscore =max(maxscore, max(nowminR-Lmax[0]+1,0) + max(Rmin[1]-maxL+1,0))
            else:
                maxL = max(Rsorted[i][0],maxL)
                maxscore =max(maxscore, max(nowminR-Lmax[0]+1,0) + max(Rmin[1]-maxL+1,0))
                
#    Lsorted = sorted(sorted(LR,key=lambda x:x[1],reverse=True),key=lambda x:[0],reverse=True)
#    minR = Lmax[1]
#    for i in range(1,N):
#        nowmaxL = min(Lsorted[i][0],Rmin[0])
#        maxscore =max(maxscore, max(minR-Lmax[0]+1,0) + max(Rmin[1]-nowmaxL+1,0))
#        minR = max(Lsorted[i][1],minR)

    print(maxscore)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10**9)
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    L=[0]*N
    R=[0]*N
    for i in range(N):
        L[i],R[i]=map(int,input().split())
    si=np.argsort(L)
    sj=np.argsort(R)
    A=[L[si[0]],R[si[0]]]
    B=[L[sj[N-1]],R[sj[N-1]]]
    for i in si:
        a=[max(A[0],L[i]),min(A[1],R[i])]
        b=[max(B[0],L[i]),min(B[1],R[i])]
        if a[1]-a[0]+B[1]-B[0] > b[1]-b[0]+A[1]-A[0]:
            A=a
        else:
            B=b
    print(A[1]-A[0]+B[1]-B[0]+2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N)]

for i in range(N):
    L[i].append(L[i][1]-L[i][0])
    
L.sort(key=itemgetter(2))

la = L[0][0]
ra = L[0][1]
lb = L[1][0]
rb = L[1][1]

for i in range(2, N):
    ta = max(0, min(ra, L[i][1])-max(la, L[i][0])+1) + rb-lb+1
    tb = max(0, min(rb, L[i][1])-max(lb, L[i][0])+1) + ra-la+1
    tc = max(0, min(ra, rb)-max(la,lb)+1) + L[i][1]-L[i][0]+1
    if max(ta, tb, tc) == ta:
        la = max(la, L[i][0])
        ra = min(ra, L[i][1])
    elif max(ta, tb, tc) == tb:
        lb = max(lb, L[i][0])
        rb = min(rb, L[i][1])
    else:
        la = max(la,lb)
        ra = min(ra, rb)
        lb = L[i][0]
        rb = L[i][1]
        
ans = max(0, ra-la+1) + max(0, rb-lb+1)


print(ans)

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, n = l1, r1, -10**9
    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[0]
    c2 = LR[-1]
#    print(LR, c1, c2)
    for i in range(1, len(LR)-1):
        x = LR[i]
        tmp1 = f(c1, x)
        tmp2 = f(c2, x)
        a = c1[2] + c2[2]
        if abs(a - (tmp1[2] + c2[2])) < abs(a - (c2[2] + tmp2[2])):
            c1 = tmp1
        else:
            c2 = tmp2
#        print("c1/c2", c1, c2)
    print(c1[2]+c2[2])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


cand = []
L = sorted(L, key=lambda x: x[0])
for pivot in range(1, N):
    low_1, high_1 = L[0]
    for i in range(pivot):
        low_1 = max(low_1, L[i][0])
        high_1 = min(high_1, L[i][1])
    low_2, high_2 = L[-1]
    for i in range(pivot, N):
        low_2 = max(low_2, L[i][0])
        high_2 = min(high_2, L[i][1])
    p_1=max(high_1-low_1,0)
    p_2 = max(high_2-low_2,0)
    ans = p_1+p_2+2
    cand.append(ans)
print(max(cand))

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)]
lr.sort(key=lambda x:x[0])
lr.sort(key=lambda x:x[1])
mae=[]
l=0
r=10**9
for i in range(n):
    ll,rr=lr[i]
    l=max(ll,l)
    r=min(rr,r)
    mae.append(r-l+1)

usi=[]
l=0
r=10**9+1
for i in range(n-1,-1,-1):
    ll,rr=lr[i]
    l=max(ll,l)
    r=min(rr,r)
    usi.append(r-l+1)
usi.reverse()

ans=0

for i in range(n-1):
    ans=max(mae[i]+usi[i+1],ans)
ans=max([ans,max(mae),max(usi)])

haba=0
for i in range(n):
    l,r=lr[i]
    haba=max(haba,r-l+1)
l=0
r=10**9+1
for i in range(n-1,-1,-1):
    ll,rr=lr[i]
    if rr-ll+1==haba:
        continue
    l=max(ll,l)
    r=min(rr,r)
ans=max(ans,r-l+1)

print(ans)