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:
# 一番長いLRが最低の楽しさ
# 一番長いLRと重なるのが一番少ないものはもう一つにする
# 一番長いLRが最低の楽しさ 一番長いLRが複数ある場合どうする？
# 一番長いLRと重なるのが一番少ないものはもう一つにする
N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
LR.sort(key=lambda x: x[1]-x[0], reverse=True)
ma = LR[0][1] - LR[0][0] + 1
left_ma = LR[0][0]
right_ma = LR[0][1]

ma2 = ma
another = 10 ** 9
another_length = 0 #重なっていない長さ
LR.pop(0)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length < ma + max(0, min(right_an, r) - max(left_an, l)):
        # anotherのグループにいれる
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        #print(right_an, left_an)
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))

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()
L.sort(key=itemgetter(1))
L.sort(key=itemgetter(2))
    

la = L[0][0]
ra = L[0][1]
lb = L[1][0]
rb = L[1][1]
ans = L[-1][2]+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(ans, max(0, ra-la+1) + max(0, rb-lb+1))


print(ans)

Submission status: Wrong Answer

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

# int(input())
# map(int, input().split())

# 2回のコンテストに問題を分ける、一回だけ出題
# すべての問題を解く参加者の人数の和を最大化

from time import time


def solve(n, e, time_counter):
    from operator import itemgetter
    e.sort(key=itemgetter(2, 0))
    # print(e)

    ans = 0

    min_ = set()
    for j in range(n):
        if e[j][2] == e[0][2]:
            min_.add(j)
        else:
            break

    max_ = []
    flg = False
    for j in range(n - 1, -1, -1):
        if e[j][2] == e[-1][2]:
            max_.append(j)
        else:
            if flg:
                break
            else:
                flg = True

    for mx in max_:
        for mn in min_:
            if mx == mn:
                continue
            if time() - time_counter >= 1.8:
                print(ans)
                exit()

            ll, lr, area_l = e[mx]  # 最大
            rl, rr, area_r = e[mn]  # 最小

            for i in range(n):
                if i in (mx, mn):
                    continue
                l, r, _ = e[i]

                sr = max(0, (rr - rl + 1))
                mr = max(0, (min(rr, r) - max(rl, l) + 1))
                diff_r = sr - mr

                sl = max(0, (lr - ll + 1))
                ml = max(0, (min(lr, r) - max(ll, l) + 1))
                diff_l = sl - ml

                if diff_r < diff_l:
                    rl = max(rl, l)
                    rr = min(rr, r)
                else:
                    ll = max(ll, l)
                    lr = min(lr, r)

            ans = max(ans, max(0, (lr - ll + 1)) + max(0, (rr - rl + 1)))
    print(ans)


if __name__ == '__main__':
    import sys

    time_counter = time()

    input = sys.stdin.readline
    n = int(input().rstrip())
    e = []
    for _ in range(n):
        lr = tuple(int(x) - 1 for x in input().rstrip().split())
        lr = lr + (lr[1] - lr[0] + 1,)
        e.append(lr)
    solve(n, e, time_counter)

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
ans2=l[1][1]-l[1][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)
lm=-1
rm=10**10
for i in range(n):
    if i==1:
        continue
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
ans2+=max(0,rm-lm+1)
ans3=max(0,min(l[0][1],l[1][1])-max(l[0][0],l[1][0])+1)
lm=-1
rm=10**10
for i in range(2,n):
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
ans3+=max(0,rm-lm+1)
ans4=0
l.sort()
lm=-1
rm=10**10
t=0
f=0
for i in range(n):
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
    if lm>=rm:
        f+=1
        lm=-1
        rm=10**10
        if f==2:
            break
    else:
        if f==0:
            t=max(rm-lm+1,t)
ans4=0
if f==1:
    ans4=t+rm-lm+1        
print(max(ans1,ans2,ans3,ans4))

Submission status: Wrong Answer

Solution code:
import bisect, collections, heapq, sys
input = sys.stdin.readline

n = int(input())
problem_list = []
r = 10**9+1
p_list = []
for i in range(n):
    a, b = [ int(v) for v in input().split() ]
    p_list.append(a)
    p_list.append(b)
    problem_list.append([b, a])
problem_list.sort(reverse = True)
problem_list = [ [i[1],i[0]] for i in problem_list ]
problem_list.sort()
p_list.sort()
problem_list = [ ( bisect.bisect_left(p_list,i[0]), bisect.bisect_left(p_list,i[1]) )  for i in problem_list ]

left_list = [ 0 for i in range(2*n) ]
right_list = [ 0 for i in range(2*n) ]
for a, b in problem_list:
    left_list[a] += 1
    right_list[b] += 1

ans = 0
la = problem_list[-1][0]
ra = None
lenr = len(right_list) 
for i in range(lenr):
    if right_list[i] > 0:
        ra = i
        break
lb, rb = -1, 10**6+1

for a, b in problem_list[:-1]:
    
    left_list[a] -= 1
    right_list[b] -= 1
    lb = max(lb,a)
    rb = min(rb,b)

    if left_list[la] == 0:
        for i in range(1,2**n):
            if left_list[la+i] != 0:
                la += i
                break
    if right_list[ra] == 0:
        for i in range(1,2**n):
            if right_list[ra+i] != 0:
                ra += i
                break

    ans = max(ans,(max(p_list[rb]-p_list[lb]+1,0)+max(p_list[ra]-p_list[la]+1,0)))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = rils(n)
res = max(r - l + 1 for l, r in ls)

ls.sort(key=lambda x: x[1])
lmax0 = [ls[0][0]]
rmin0 = [ls[0][1]]
for i in range(1, n - 1):
    lmax0.append(max(lmax0[-1], ls[i][0]))
    rmin0.append(min(rmin0[-1], ls[i][1]))

lmax1 = [ls[-1][0]]
rmin1 = [ls[-1][1]]
for i in range(1, n - 1):
    lmax1.append(max(lmax1[-1], ls[n - i - 1][0]))
    rmin1.append(min(rmin1[-1], ls[n - i - 1][1]))

s0 = [max(0, r - l + 1) for l, r in zip(lmax0, rmin0)]
s1 = [max(0, r - l + 1) for l, r in zip(lmax1, rmin1)]
for x, y in zip(s0, reversed(s1)):
    res = max(res, x + y)

ls.sort(key=lambda x: (x[1], -x[0]))
lmax0 = [ls[0][0]]
rmin0 = [ls[0][1]]
for i in range(1, n - 1):
    lmax0.append(max(lmax0[-1], ls[i][0]))
    rmin0.append(min(rmin0[-1], ls[i][1]))

lmax1 = [ls[-1][0]]
rmin1 = [ls[-1][1]]
for i in range(1, n - 1):
    lmax1.append(max(lmax1[-1], ls[n - i - 1][0]))
    rmin1.append(min(rmin1[-1], ls[n - i - 1][1]))

s0 = [max(0, r - l + 1) for l, r in zip(lmax0, rmin0)]
s1 = [max(0, r - l + 1) for l, r in zip(lmax1, rmin1)]
for x, y in zip(s0, reversed(s1)):
    res = max(res, x + y)
print(res)

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)
maxi = 0
N = I()
LR =[[] for _ in range(N)]
LR2 =[]
for i in range(N):
    LR[i] = LI()
    C = LR[i][1] - LR[i][0]
    if C>maxlen:
        maxlen = C
        maxi = i
for i in range(N):
    if not i==maxi:
        LR2.append(LR[i])

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 live1[i-1][1]>LR[i][1]:
        rightend = LR[i][1]
    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])]

for i in range(N-2,-1,-1):
    if live2[i+1][1]<LR[i][0]:
        leftend = LR[i][0]
    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)
#print(leftend)
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)
####################################################
if ans==maxlen+1:
    LR2.sort()
    live3 = LR2[0]
    for i in range(1,N-1):
        live3 = [max(live3[0],LR2[i][0]),min(live3[1],LR2[i][1])]
    ansd = live3[1]-live3[0]+1
    ans = max(ansd,0)+ans

#####################################################
LR.sort(key=lambda x:x[1])
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 live1[i-1][1]>LR[i][1]:
        rightend = LR[i][1]
    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])]

for i in range(N-2,-1,-1):
    if live2[i+1][1]<LR[i][0]:
        leftend = LR[i][0]
    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)
#print(leftend)
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),ans)



print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def answer():
    input = sys.stdin.readline
    N = int(input())
    questions = [list(map(int, input().split())) for _ in range(N)]

    questions.sort(key=lambda x: x[0], reverse=True)
    distances = [q[1]-q[0]+1 for q in questions]

    index_most_fun = distances.index(max(distances))
    most_fun_q = questions.pop(index_most_fun)

    def calc_overlap(q1, q2):
        return max(min(q1[1], q2[1]) - max(q1[0], q2[0]), 0)

    def calc_overlap_for_most_fun(q):
        return calc_overlap(q, most_fun_q)

    def calc_diff(q):
        return abs(q[0] - most_fun_q[0]) + abs(q[1] - most_fun_q[1])

    questions.sort(key=lambda x: x[0])
    diff_from_most_fun = list(map(calc_diff, questions))
    index_most_far = diff_from_most_fun.index(max(diff_from_most_fun))
    most_far_q = questions.pop(index_most_far)

    def calc_overlap_for_most_far(q):
        return calc_overlap(q, most_far_q)

    def calc_score(q):
        return max(q[1] - q[0] + 1, 0)

    fun = most_fun_q
    far = most_far_q
    score_fun = calc_score(fun)
    score_far = calc_score(far)

    for q in questions:
        diff_fun = score_fun - calc_overlap_for_most_fun(q)
        diff_far = score_far - calc_overlap_for_most_far(q)

        if diff_fun > diff_far:
            far = [max(q[0], far[0]), min(q[1], far[1])]
            score_far = calc_score(far)
        else:
            fun = [max(q[0], fun[0]), min(q[1], fun[1])]
            score_fun = calc_score(fun)

    print(score_fun + score_far)


if __name__ == '__main__':
    answer()

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[]]*N
max_n = 0
min_n = 10**9
for i in range(N):
    LR[i] = list(map(int, input().split()))
    min_n = min(min_n, LR[i][0])
    max_n = max(max_n, LR[i][1])

LR.sort(key=lambda x: x[1] - x[0], reverse=True)


set2 = [LR[0][0], LR[0][1]]
set1 = [LR[1][0], LR[1][1]]

#LR = sorted(LR[2:], key=lambda x: x[1]-x[0])

for i in range(2,N):
    if max((min(set1[1], LR[i][1]) - max(set1[0], LR[i][0]))+1, 0)+(set2[1]-set2[0]+1) >= max((min(set2[1], LR[i][1]) - max(set2[0], LR[i][0])+1), 0) + (set1[1]-set1[0]+1):
        set1 = [max(set1[0], LR[i][0]), min(set1[1], LR[i][1])]
    else:
        set2 = [max(set2[0], LR[i][0]), min(set2[1], LR[i][1])]

ans1 = max(set1[1] - set1[0]+1, 0) + max(set2[1]-set2[0]+1, 0)

print(ans1)

Submission status: Wrong Answer

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

sLR = sorted(LR, key=lambda x:(x[0], - x[1] + x[0]))  
L = [0] * N

l,r = sLR[0][0], sLR[0][1]
L[0] = r - l + 1
for itr, val in enumerate(sLR[1:]):
  l = max(l, val[0])
  r = min(r, val[1])
  if l > r:
    break
  else:
    L[itr + 1] = r - l + 1
  
sLR = sLR[::-1]
R = [0] * N

l,r = sLR[0][0], sLR[0][1]
R[0] = r - l + 1
for itr, val in enumerate(sLR[1:]):
  l = max(l, val[0])
  r = min(r, val[1])
  if l > r:
    break
  else:
    R[itr + 1] = r - l + 1

ans = 0  
for l, r in zip(L[:-1], R[::-1][1:]):
  ans = max(ans, l + r) 

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[]]*N
max_n = 0
min_n = 10**9
for i in range(N):
    LR[i] = list(map(int, input().split()))
    min_n = min(min_n, LR[i][0])
    max_n = max(max_n, LR[i][1])

LR.sort(key=lambda x: x[1] - x[0], reverse=True)


set2 = [LR[0][0], LR[0][1]]
set1 = [LR[1][0], LR[1][1]]

#LR = sorted(LR[2:], key=lambda x: x[1]-x[0])

for i in range(N-2):
    if max((min(set1[1], LR[i][1]) - max(set1[0], LR[i][0]))+1, 0)+(set2[1]-set2[0]+1) >= max((min(set2[1], LR[i][1]) - max(set2[0], LR[i][0])+1), 0) + (set1[1]-set1[0]+1):
        set1 = [max(set1[0], LR[i][0]), min(set1[1], LR[i][1])]
    else:
        set2 = [max(set2[0], LR[i][0]), min(set2[1], LR[i][1])]

ans1 = max(set1[1] - set1[0]+1, 0) + max(set2[1]-set2[0]+1, 0)

print(ans1)

Submission status: Wrong Answer

Solution code:
import sys
inputs = sys.stdin.readlines()
N = int(inputs[0])
LRs = [list(map(int, x.split())) for x in inputs[1:]]

LRs = sorted(LRs, key=lambda x:sum(x))

rets = []
for i in range(1, N):
    firsts = list(range(i))
    seconds = list(range(i, N))
    section1 = [max((LRs[i][0] for i in firsts)), min((LRs[i][1] for i in firsts))]
    section2 = [max((LRs[i][0] for i in seconds)), min((LRs[i][1] for i in seconds))]
    score = max(section1[1]-section1[0]+1, 0) + max(section2[1]-section2[0]+1, 0)
        
    rets.append((firsts, seconds, score))
print(max(rets, key=lambda x:x[-1]))

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()
L.sort(key=itemgetter(2))
    

la = L[0][0]
ra = L[0][1]
lb = L[1][0]
rb = L[1][1]
ans = L[-1][2]+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(ans, max(0, ra-la+1) + max(0, rb-lb+1))


print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

import bisect, collections, heapq, sys
input = sys.stdin.readline

n = int(input())
problem_list = []
r = 10**9+1
p_list = []
for i in range(n):
    a, b = [ int(v) for v in input().split() ]
    p_list.append(a)
    p_list.append(b)
    problem_list.append([b, a])
problem_list.sort(reverse = True)
problem_list = [ [i[1],i[0]] for i in problem_list ]

p_list.sort()
problem_list = [ ( bisect.bisect_left(p_list,i[0]), bisect.bisect_left(p_list,i[1]) )  for i in problem_list ]

left_list = [ 0 for i in range(2*n) ]
right_list = [ 0 for i in range(2*n) ]
for a, b in problem_list:
    left_list[a] += 1
    right_list[b] += 1

ans = 0
la = problem_list[-1][0]
ra = None
lenr = len(right_list) 
for i in range(lenr):
    if right_list[i] > 0:
        ra = i
        break
lb, rb = -1, 10**6+1

for a, b in problem_list[:-1]:
    
    left_list[a] -= 1
    right_list[b] -= 1
    lb = max(lb,a)
    rb = min(rb,b)

    if left_list[la] == 0:
        for i in range(1,2**n):
            if left_list[la+i] != 0:
                la += i
                break
    if right_list[ra] == 0:
        for i in range(1,2**n):
            if right_list[ra+i] != 0:
                ra += i
                break

    ans = max(ans,(max(p_list[rb]-p_list[lb]+1,0)+max(p_list[ra]-p_list[la]+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)]
lr.sort(key = lambda x: (x[0], x[1]))
#print(lr)
dpx = [0] * N
dpy = [0] * N
dpx[0] = lr[0][1] - lr[0][0] + 1
def check(x, y):
  if x[1] >= y[0]:
    return [max(x[0], y[0]), min(x[1], y[1])]
  else: return [-1, -1]
ran = [0, 10 ** 9]
for i in range(N - 1, -1, -1):
  ran = check(lr[i], ran)
  dpy[i] = ran[1] - ran[0] + 1
  #print(lr[i])
ran = lr[0]
for i in range(N):
  ran = check(lr[i], ran)
  dpx[i] = ran[1] - ran[0] + 1
  #print(lr[i])
#print(dpx, dpy)
res = 0
for i in range(N - 1):
  res = max(dpx[i] + dpy[i + 1], res)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
lm, rm = 0, 10**10
fm, lt = 10**9, 1
seg = []
for _ in range(n):
	l, r = map(int, input().split())
	seg.append([l, r])
	lm = max(lm, l)
	rm = min(rm, r)
	fm = min(fm, l)
	lt = max(lt, r)

#print(lm, rm)
#fm, lt = , 10**9
amb = []
for l, r in seg:
	if rm < l and r < lm:
		print(0)
		sys.exit()
	elif lm == l and rm == r:
		print(rm-lm+1 + max([x[1]-x[0]+1 for x in seg]))
		sys.exit()
	elif rm < l:
		lt = min(lt, r)
	elif r < lm:
		fm = max(fm, l)
	else:
		amb.append([l, r])

#print(fm, lt)

if not amb:
	print(rm-fm+1 + lt-lm+1)

else:
	#print(amb)
	#print(rm, lm)
	amb.sort(key=lambda x: (x[0], x[1]))

	lr_max = [10**10 for _ in range(len(amb))] + [lt]
	lr_max[-2] = amb[-1][1]
	for i in range(len(amb)-2, -1, -1):
		lr_max[i] = min(lr_max[i+1], amb[i][1])

	#print(lr_max)

	ans = rm-fm+1 + lr_max[0]-lm+1
	for i in range(len(amb)):
		ans = max(ans, rm-max(fm, amb[i][0])+1 + lr_max[i+1]-lm+1)

	print(ans)

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)



M = sorted(M, key=lambda X: X[1], reverse= True)


maxL1 = [0]*N
maxL2 = [0]*N

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

tmp = M[-1][0]
maxL2[-1] = tmp
for i in range(N-2, -1, -1):
    tmp = max(M[i][0], tmp)
    maxL2[i] = tmp

for i in range(0, N-1):

    R1 = M[i][1]
    R2 = M[-1][1]
    L1 = maxL1[i]
    L2 = maxL2[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:
import sys

def answer():
    input = sys.stdin.readline
    N = int(input())
    questions = [list(map(int, input().split())) for _ in range(N)]

    questions.sort(key=lambda x: x[0])
    distances = [q[1]-q[0]+1 for q in questions]

    index_most_fun = distances.index(max(distances))
    most_fun_q = questions.pop(index_most_fun)

    def calc_overlap(q1, q2):
        return max(min(q1[1], q2[1]) - max(q1[0], q2[0]), 0)

    def calc_overlap_for_most_fun(q):
        return calc_overlap(q, most_fun_q)

    def calc_diff(q):
        return abs(q[0] - most_fun_q[0]) + abs(q[1] - most_fun_q[1])

    diff_from_most_fun = list(map(calc_diff, questions))
    index_most_far = diff_from_most_fun.index(max(diff_from_most_fun))
    most_far_q = questions.pop(index_most_far)

    def calc_overlap_for_most_far(q):
        return calc_overlap(q, most_far_q)

    def calc_score(q):
        return max(q[1] - q[0] + 1, 0)

    fun = most_fun_q
    far = most_far_q
    score_fun = calc_score(fun)
    score_far = calc_score(far)

    for q in questions:
        diff_fun = score_fun - calc_overlap_for_most_fun(q)
        diff_far = score_far - calc_overlap_for_most_far(q)

        if diff_fun > diff_far:
            far = [max(q[0], far[0]), min(q[1], far[1])]
            score_far = calc_score(far)
        else:
            fun = [max(q[0], fun[0]), min(q[1], fun[1])]
            score_fun = calc_score(fun)

    print(score_fun + score_far)


if __name__ == '__main__':
    answer()

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:
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
        pl = l
    if len(LR_) >= 2:
        LR, LR_ = 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))
        #LR, LR_ = LR_, LR

    LR.sort(key=itemgetter(0))
    Lr = float("inf")
    Ls = []
    for l, r in LR[:-1]:
        Ll = l
        Lr = min(Lr, r)
        Ls.append(Lr-Ll)
    Rs = []
    Rl, Rr = 0, float("inf")
    for l, r in LR[:0:-1]:
        Rl = max(Rl, l)
        Rr = min(Rr, r)
        Rs.append(Rr - Rl)
    Rs.reverse()
    for l, r in zip(Ls, Rs):
        assert l>0 and r>0
        ans = max(ans, l+r)

    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

import bisect, collections, heapq, sys
input = sys.stdin.readline

n = int(input())
problem_list = []
r = 10**9+1
p_list = []
for i in range(n):
    a, b = [ int(v) for v in input().split() ]
    p_list.append(a)
    p_list.append(b)
    problem_list.append([b, a])
problem_list.sort(reverse = True)
problem_list = [ [i[1],i[0]] for i in problem_list ]
print(problem_list)
p_list.sort()
problem_list = [ ( bisect.bisect_left(p_list,i[0]), bisect.bisect_left(p_list,i[1]) )  for i in problem_list ]

left_list = [ 0 for i in range(2*n) ]
right_list = [ 0 for i in range(2*n) ]
for a, b in problem_list:
    left_list[a] += 1
    right_list[b] += 1

ans = 0
la = problem_list[-1][0]
ra = None
lenr = len(right_list) 
for i in range(lenr):
    if right_list[i] > 0:
        ra = i
        break
lb, rb = -1, 10**6+1

for a, b in problem_list[:-1]:
    
    left_list[a] -= 1
    right_list[b] -= 1
    lb = max(lb,a)
    rb = min(rb,b)

    if left_list[la] == 0:
        for i in range(1,2**n):
            if left_list[la+i] != 0:
                la += i
                break
    if right_list[ra] == 0:
        for i in range(1,2**n):
            if right_list[ra+i] != 0:
                ra += i
                break

    ans = max(ans,(max(p_list[rb]-p_list[lb]+1,0)+max(p_list[ra]-p_list[la]+1,0)))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
mG = [tuple(map(int, input().split())) for _ in range(N)]

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

# print(mG)###

vA1 = []
vL1 = []
vR1 = []
L = -1
R = 10**9 + 1

for l,r in mG[:-1]:
    L = max(L, l)
    R = min(R, r)
    vL1.append(L)
    vR1.append(R)
    vA1.append(R-L+1)

# print(vA1)###

vA2 = []
vL2 = []
vR2 = []
L = -1
R = 10**9 + 1

for l,r in mG[:0:-1]:
    L = max(L, l)
    R = min(R, r)
    vL2.append(L)
    vR2.append(R)
    vA2.append(R-L+1)

# print(vA2)###

# print(max(a+b for a,b in zip(vA1, vA2[::-1])))
M1 = max(a+b for a,b in zip(vA1, vA2[::-1]))

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

# print(mG)###

vA1 = []
vL1 = []
vR1 = []
L = -1
R = 10**9 + 1

for l,r in mG[:-1]:
    L = max(L, l)
    R = min(R, r)
    vL1.append(L)
    vR1.append(R)
    vA1.append(R-L+1)

# print(vA1)###

vA2 = []
vL2 = []
vR2 = []
L = -1
R = 10**9 + 1

for l,r in mG[:0:-1]:
    L = max(L, l)
    R = min(R, r)
    vL2.append(L)
    vR2.append(R)
    vA2.append(R-L+1)

# print(vA2)###

# print(max(a+b for a,b in zip(vA1, vA2[::-1])))
M2 = max(a+b for a,b in zip(vA1, vA2[::-1]))

print(max(M1, M2))

Submission status: Wrong Answer

Solution code:
N, *LR = map(int, open(0).read().split())
LR = sorted((r - l + 1, r, l) for l, r in zip(*[iter(LR)] * 2))

_, min_r, max_l = LR[0]
A = []
for _, r, l in LR:
    max_l, min_r = max(max_l, l), min(min_r, r)
    A.append(max(0, min_r - max_l + 1))

_, min_r, max_l = LR[-1]
B = []
for _, r, l in reversed(LR):
    max_l, min_r = max(max_l, l), min(min_r, r)
    B.append(max(0, min_r - max_l + 1))

B.pop()

print(max(a + b for a, b in zip(A, reversed(B))))

Submission status: Wrong Answer

Solution code:
N = int(input())

info = [0] * N

for i in range(N):
    L,R = map(int,input().split())
    info[i] = [L,R]

info.sort()

x = info[-1][1]
stay = [1] * N

for i in range(2,N+1):
    if info[-i][1] < x:
        x = info[-i][1]
    else:
        stay[-i] = 0

info_ = []

for i in range(N):
    if stay[i] == 1:
        info_.append(info[i])

if len(info_) >= 2:
    alone = [0] * len(info_)

    for i in range(1,len(info_)-1):
        alone[i] = max(0,info_[0][1] - info_[-1][0] + 1)+ (info_[i][1] - info_[i][0] + 1)

    can = [max(alone)]

    for i in range(len(info_)-1):
        can.append(max(0,info_[0][1] - info_[i][0] + 1) + max(0,info_[i+1][1] - info_[-1][0] + 1))

    print(max(can))

else:
    length = [0] * N

    for i in range(N):
        length[i] = info[i][1] - info[i][0] + 1
    
    length_ = info_[0][1] - info_[0][0] + 1

    print(length_ + max(length))

Submission status: Wrong Answer

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

# int(input())
# map(int, input().split())

# 2回のコンテストに問題を分ける、一回だけ出題
# すべての問題を解く参加者の人数の和を最大化

from time import time


def solve(n, e, time_counter):
    from operator import itemgetter
    e.sort(key=itemgetter(2, 0))
    # print(e)

    ans = 0

    min_ = set()
    for j in range(n):
        if e[j][2] == e[0][2]:
            min_.add(j)
        else:
            break

    max_ = set()
    for j in range(n - 1, -1, -1):
        if e[j][2] == e[-1][2]:
            max_.add(j)
        else:
            break

    for mx in max_:
        for mn in min_:
            if mx == mn:
                continue
            if time() - time_counter >= 1.8:
                print(ans)
                exit()

            ll, lr, area_l = e[mx]  # 最大
            rl, rr, area_r = e[mn]  # 最小

            for i in range(n):
                if i in (mx, mn):
                    continue
                l, r, _ = e[i]

                sr = max(0, (rr - rl + 1))
                mr = max(0, (min(rr, r) - max(rl, l) + 1))
                diff_r = sr - mr

                sl = max(0, (lr - ll + 1))
                ml = max(0, (min(lr, r) - max(ll, l) + 1))
                diff_l = sl - ml

                if diff_r < diff_l:
                    rl = max(rl, l)
                    rr = min(rr, r)
                else:
                    ll = max(ll, l)
                    lr = min(lr, r)

            ans = max(ans, max(0, (lr - ll + 1)) + max(0, (rr - rl + 1)))
    print(ans)


if __name__ == '__main__':
    import sys

    time_counter = time()

    input = sys.stdin.readline
    n = int(input().rstrip())
    e = []
    for _ in range(n):
        lr = tuple(int(x) - 1 for x in input().rstrip().split())
        lr = lr + (lr[1] - lr[0] + 1,)
        e.append(lr)
    solve(n, e, time_counter)