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] - 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:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

L,R = two_int()
C1_Ls=L
C1_Rs=R

c1 = C1_Rs - C1_Ls+1

problem_list = {}
C2_Ls=[]
C2_Rs=[]
for i in range(N-1):
    L,R = two_int()
    C2_Ls.append(L)
    C2_Rs.append(R)
    problem_list[i] = [L,R] 

C2_Ls = sorted(C2_Ls)
C2_Rs = sorted(C2_Rs)
c2 = max(C2_Rs[0] - C2_Ls[-1]+1, 0)

c1, c2

temp = c2+c1
for k,(L,R) in problem_list.items():
    c2_r_i=0
    c2_l_i=1
    
    # 2回目のcontestのレンジを決めてるものなら
#     if C2_Rs[0] == R or C2_Ls[-1]==L:
    if C2_Rs[0] == R:
        c2_r_i = 1
    if C2_Ls[-1] == L:
        c2_l_i = 2

    c1 = max((min(C1_Rs, R)- max(C1_Ls, L))+1, 0)
    c2 = 0
    try:
        c2 = max(C2_Rs[c2_r_i] - C2_Ls[-1*c2_l_i]+1, 0)
    except:
        break
    if temp <=  c1+c2:
        temp = c1+c2

        C1_Rs = min(C1_Rs, R)
        C1_Ls = max(C1_Ls, L)
        C2_Ls.remove(L)
        C2_Rs.remove(R)
print(temp)

Submission status: Wrong Answer

Solution code:
import sys
import heapq
m=10**9
n = int(input())
bd = [[0,0] for _ in range(n)]
minr = 10**9+1
secminr = 10**9 +1
minrInd = -1
maxl = 0
maxlInd = -1
maxrange = 0
secmax = 0
thmax = 0
maxi = -1
seci = -1
thi = -1
queue = []
for i in range(n):
    l, r = map(int, sys.stdin.readline().split())
    if l > maxl:
        maxl = l
        maxlInd = i
    if r < minr:
        minr = r
        minrInd = i
    if r-l > maxrange:
        thmax = secmax
        secmax = maxrange
        maxrange = r-l
        thi = seci
        seci = maxi
        maxi = i
    elif  r-l > secmax:
        thmax = secmax
        secmax = r-l
        thi = seci
        seci = i
    elif  r-l > thmax:
        thmax = r-l
        thi = i
    heapq.heappush(queue,(-1 * l, i))


    bd[i][0] = l
    bd[i][1] = r
if maxlInd == minrInd:
    print(max(0, minr - maxl + maxrange + 2 ))
    exit()
tmpmax = 0
if maxi != maxlInd:
    if maxi != minrInd:
        tmpmax = minr - maxl + maxrange + 2
    elif seci != maxlInd and seci != -1:
        tmpmax = minr - maxl + secmax + 2
    elif thi != -1:
        tmpmax = minr - maxl + thmax + 2
elif seci != minrInd and seci != -1:
    tmpmax = minr - maxl + secmax + 2
elif thi != -1:
    tmpmax = minr - maxl + thmax + 2

lb1 = bd[minrInd][0]
rb1 = bd[minrInd][1]
lb2 = bd[maxlInd][0]
rb2 = bd[maxlInd][1]
for j in range(n):
    _, i = heapq.heappop(queue)
    if bd[i][0] <= lb1:
        continue
    elif bd[i][1] >= rb2:
        continue
    elif rb1 - bd[i][0] + rb2 - lb2 > rb1 - lb1 + bd[i][1] - lb2:
        lb1 = bd[i][0]
    else:
        rb2 = bd[i][1]

print(max(tmpmax, rb1 - lb1 + rb2 - lb2 + 2))

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)

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

for i in range(2, N, 2):
    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]
        
for i in range(3, N, 2):
    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))
        
la = L[0][0]
ra = L[0][1]
lb = L[-1][0]
rb = L[-1][1]
prev = N
for i in range(2, N):
    if N+1-prev > N//2:
        i = N+1-prev
        prev = i
    else:
        i = i//2 + 1
        prev = i
    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

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)

    overlap_for_most_fun = list(map(calc_overlap_for_most_fun, questions))
    index_most_far = overlap_for_most_fun.index(min(overlap_for_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:
import sys
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))
P = 10**9
N = int(input())
LR = [ inpl() for _ in range(N) ]
LR.sort()

K = 0
mlist = []
for i in range(N):
    l, r = LR[i]
    k = r -l + 1
    if k == K:
        mlist.append(i)
    elif k > K:
        mlist = [i]
        K = k
U = []
r = P
for i in range(N):
    l = LR[i][0]
    r = min(r, LR[i][1])
    U.append((l,r))
D = [None]*N
l = LR[-1][0]
for i in range(N):
    r = min(r, LR[N-1-i][1])
    D[N-1-i] = (l,r)

ans = K
for m in mlist:
    if m == 0:
        score = K + max(0, D[1][1]-D[1][0]+1)
    elif m == N-1:
        score = K + max(0, U[-2][1]-U[-2][0]+1)
    else:
        score = K + max(0, min(U[m-1][1],D[m+1][1]) - D[m+1][0] + 1)
    if score > ans:
        ans = score

LR2 = [LR[0]]
for i in range(1,N):
    if LR2[-1][1] < LR[i][1]:
        LR2.append(LR[i])
    else:
        LR2[-1] = LR[i]
M = len(LR2)
if M < 2:
    print(ans)
    exit()

R1 = LR2[0][1]
L2 = LR2[-1][0]
for i in range(M-1):
    L1 = LR2[i][0]
    R2 = LR2[i+1][1]
    score = max(0, R1-L1+1) + max(0, R2-L2+1)
    if score > ans:
        ans = score

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回のコンテストに問題を分ける、一回だけ出題
# すべての問題を解く参加者の人数の和を最大化


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

    ll, lr, area_l = e[-1]  # 最大
    rl, rr, area_r = e[0]  # 最小

    if area_l == area_r:
        for i in range(1, n - 1):
            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)

    else:
        for i in range(1, n - 2):
            l, r, _ = e[i]
            rl = max(rl, l)
            rr = min(rr, r)

        l, r, _ = e[n - 2]
        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)

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


if __name__ == '__main__':
    import sys

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

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:
    length = [0] * len(info_)

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

    can = [max(length)]

    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


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_length = 0
Max = 0
Max_i_j = []
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_j.append(i)

for j in range(len(Max_i_j)):

    Max_i = Max_i_j[j]

    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]):
            break

    if Max_length < leng(left[0], left[1]) + leng(right[0], right[1]):
        Max_length = leng(left[0], left[1]) + leng(right[0], right[1])

print(max(Max_length,Max))

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:
def main():
    N, *LR = map(int, open(0).read().split())
    LR = sorted(zip(*[iter(LR)] * 2))

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


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

    B.pop()

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
p = []
max_p = 0
max_i = 0
for i in range(n):
  l, r = map(int, input().split())
  p.append([l, r])
  if max_p < r - l:
    max_p = r - l
    max_i = i
min_p = 0
min_i = 0
for i in range(n):
  dif = min(p[max_i][0] - p[i][0], 0) + min(p[i][1] - p[max_i][1], 0)
  if dif < min_p:
    min_p = dif
    min_i = i
g1 = []
g2 = []
max_l = p[max_i][0]
max_r = p[max_i][1]
min_l = p[min_i][0]
min_r = p[min_i][1]
g1.append(p[max_i])
g2.append(p[min_i])
for i in range(n):
  if i == max_i or i == min_i:
    continue
  if min(max_l - p[i][0], 0) + min(p[i][1] - max_r, 0) < min(min_l - p[i][0], 0) + min(p[i][1] - min_r, 0):
    g2.append(p[i])
  else:
    g1.append(p[i])
g1_l = 0
g1_r = 1001001001
g2_l = 0
g2_r = 1001001001
for i in range(len(g1)):
  if g1[i][0] > g1_l:
    g1_l = g1[i][0]
  if g1[i][1] < g1_r:
    g1_r = g1[i][1]
for i in range(len(g2)):
  if g2[i][0] > g2_l:
    g2_l = g2[i][0]
  if g2[i][1] < g2_r:
    g2_r = g2[i][1]
s_g1 = max(g1_r - g1_l + 1, 0)
s_g2 = max(g2_r - g2_l + 1, 0)
ans = s_g1 + s_g2
print(ans)

Submission status: Wrong Answer

Solution code:
def calc(L):
    if len(L) == 0:
       return -10 ** 18

    left = 0
    right = 10 ** 9
    for l in L:
        left = max(left, l[0])
        right = min(right, l[1])

    return max(0, right - left + 1)


def f():
    N = int(input())
    W = sorted(list(([tuple(map(int, input().split())) for _ in range(N)])))
    sorted(W, key=lambda x: x[1], reverse=True)

    val_left = [0] * N  # W[:k]
    left = 0
    right = 10 ** 9
    for k in range(N):
        left = max(left, W[k][0])
        right = min(right, W[k][1])
        if left > right:
            break
        val_left[k] = right - left + 1

    val_right = [0] * N  # W[k:]
    left = 0
    right = 10 ** 9
    for k in range(N - 1, -1, -1):
        left = max(left, W[k][0])
        right = min(right, W[k][1])
        if left > right:
            break
        val_right[k - 1] = right - left + 1

    res = 0
    for k in range(N):
        res = max(res, val_left[k] + val_right[k])
    print(res)


if __name__ == '__main__':
    f()

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
inp = []
for k in range(N):
    L, R = map(int, sys.stdin.readline().split())
    inp.append([L, R])

A = inp[0]
B = None
now = float("inf")
now_len = None
for k in range(1, N):
    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(1, N):
    if k == now_len:
        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 (A[1] - A[0] + 1) - (mna - mxa + 1) > (B[1] - B[0] + 1) - (mnb - mxb + 1):
        B = [mxb, mnb]
    else:
        A = [mxa, mna]

print((A[1] - A[0] + 1) + (B[1] - B[0] + 1))

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)
print(max(ans1,ans2,ans3))

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):
        a1,a0 = B[i][1],B[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:
N = int(input())
lr = [list(map(int, input().split())) for _ in range(N)]
lr.sort()
lr2 = [lr[0]]
for i in range(1, N):
    if lr[i][0] == lr[i-1][0]:
        continue
    lr2.append(lr[i])
R = lr2[-1][1]
lr1 = [lr2[-1]]
for l, r in lr2[-2::-1]:
    if R<=r:
        continue
    lr1.append([l, r])
    R = r
lr1 = list(reversed(lr1))
ans1 = [0]
ans2 = [0]
R = lr1[0][1]
for l, r in lr1:
    if l>R:
        ans1.append(0)
    else:
        ans1.append(R-l+1)
L = lr1[-1][0]
for l, r in lr1[::-1]:
    if L>r:
        ans2.append(0)
    else:
        ans2.append(r-L+1)
m = 0
L, R = [], []
index = []
for i in range(N):
    l, r = lr[i]
    L.append(l)
    R.append(r)
    if m<r-l:
        index = [i]
        m = r-l
    elif m == r-l:
        index.append(i)
L.sort()
R.sort()
for i in index:
    l, r = lr[i]
    if L[-1] == l:
        l  =  L[-2]
    else:
        l = L[-1]
    if R[0] == r:
        r = R[1]
    else:
        r = R[0]
ans = m+1+r-l+1
for a, b in zip(ans1, ans2[::-1]):
    ans = max(ans, a+b)
print(ans)

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])


C1L=[sortedlr[i-1][0] for i in range(1,n)]
C2L=[sortedlr[-1][0]]*(n-1)

c1right=sortedlr[0][1]
c2right=sortedlr[-1][1]
C1R=[]
C2R=[]
for i in range(1,n):
    c1right=min(c1right,sortedlr[i-1][1])
    C1R.append(c1right)

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
lrlr = [0, -1e10, 0, -1e10]
for i in range(n):
    l, r = map(int, input().split())
    LRLR = [max(lrlr[0],l), min(abs(lrlr[1]),r), max(lrlr[2],l), min(abs(lrlr[3]),r)]
    p1 = LRLR[1]-LRLR[0]+1 + lrlr[3]-lrlr[2]+1
    p2 = lrlr[1]-lrlr[0]+1 + LRLR[3]-LRLR[2]+1
    if p1>p2:
        lrlr[0] = LRLR[0]
        lrlr[1] = LRLR[1]
    else:
        lrlr[2] = LRLR[2]
        lrlr[3] = LRLR[3]
print(lrlr[1]-lrlr[0]+lrlr[3]-lrlr[2]+2)

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=False, 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:
from itertools import accumulate

N = int(input())
LR = [tuple(map(int, input().split())) for _ in range(N)]
LR.sort(key=lambda x: x[1], reverse=True)
LR.sort(key=lambda x: x[0])

L = [LR[i][0] for i in range(N)]
R = [LR[i][1] for i in range(N)]

maxL1 = list(accumulate(L, lambda x, y: max(x, y)))
maxL2 = list(accumulate(L[::-1], lambda x, y: max(x, y)))[::-1]
minR1 = list(accumulate(R[::-1], lambda x, y: min(x, y)))[::-1]
minR2 = list(accumulate(R, lambda x, y: min(x, y)))

ans = 0
for i in range(1, N):
    tmp = 0
    if minR2[i-1] - maxL1[i-1] >= 0:
        tmp += minR2[i-1] - maxL1[i-1] + 1
    if minR1[i] - maxL2[i] >= 0:
        tmp += minR1[i] - maxL2[i] + 1
    ans = max(ans, tmp)
print(ans)

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 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)

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 = [[]]*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)
# set1 = {i for i in range(min_n, max_n+1)}
# set2 = {i for i in range(min_n, max_n+1)}
# for i in range(N):
#     tmp = {k for k in range(LR[i][0], LR[i][1]+1)}
#     if len(set1 & tmp) >= len(set2 & tmp):
#         set1 &= tmp
#     else:
#         set2 &= tmp

# print(len(set1) + len(set2))


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

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 = set1[1] - set1[0]+1 + set2[1]-set2[0]+1


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

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])]

ans2 = set1[1] - set1[0]+1 + set2[1]-set2[0]+1


print(max(ans1, ans2))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

N = int(input())
LR = [tuple(map(int, input().split())) for _ in range(N)]
LR.sort(key=lambda x: x[1], reverse=True)
LR.sort(key=lambda x: x[0])

L = [LR[i][0] - 450000000 for i in range(N)]
R = [LR[i][1] - 450000000 for i in range(N)]

maxL1 = list(accumulate(L, lambda x, y: max(x, y)))
maxL2 = list(accumulate(L[::-1], lambda x, y: max(x, y)))[::-1]
minR1 = list(accumulate(R[::-1], lambda x, y: min(x, y)))[::-1]
minR2 = list(accumulate(R, lambda x, y: min(x, y)))

ans = 0
for i in range(1, N):
    tmp = 0
    if minR2[i-1] - maxL1[i-1] >= 0:
        tmp += minR2[i-1] - maxL1[i-1] + 1
    if minR1[i] - maxL2[i] >= 0:
        tmp += minR1[i] - maxL2[i] + 1
    ans = max(ans, tmp)
print(ans)

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")
    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):
        ans = max(ans, l+r)
    print(ans)