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

    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
lr = []
L, R = [], []
for _ in range(N):
    l, r = map(int, input().split())
    lr.append((l, r))
    L.append(l)
    R.append(r)
lr.sort()
L.sort()
R.sort()
ans = 0
for l, r in lr:  
    m = L[-1]
    if L[-1] == l:
        m = L[-2]
    M = R[0]
    if R[0] == r:
        M = R[1]
    ans = max(ans, M-m+1+r-l+1)
tmp = [lr[0]]
for i in range(1, N):
    if lr[i][0] == lr[i][1]:
        continue
    else:
        tmp.append(lr[i])
lr = tmp
R = lr[-1][1]
tmp = [lr[-1]]
for l, r in lr[-2::-1]:
    if R<=r:
        continue
    tmp.append((l, r))
    R = r
lr = tmp[::-1]
ans1 = [0]
ans2 = [0]
R = lr[0][1]
for l, r in lr:
    if l>R:
        ans1.append(0)
    else:
        ans1.append(R-l+1)
L = lr[-1][0]
for l, r in lr[::-1]:
    if L>r:
        ans2.append(0)
    else:
        ans2.append(r-L+1)
for a, b in zip(ans1, ans2[::-1]):
    ans = max(ans, a+b)
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)

    questions.sort(key=lambda x: x[1], reverse=True)
    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

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]

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

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

print(result)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

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

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

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

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)

if n == 2:
    print(ls[0][1] - ls[0][0] + ls[1][1] - ls[1][0] + 2)
    exit()

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)
print(res)

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])
# 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:
# 一番長いLRが最低の楽しさ
# 一番長いLRと重なるのが一番少ないものはもう一つにする
# 一番長いLRが最低の楽しさ 一番長いLRが複数ある場合どうする？
# 一番長いLRと重なるのが一番少ないものはもう一つにする
N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
ma = 0
for i, (l, r) in enumerate(LR):
    if r-l > ma:
        ma = r-l+1
        i_ma = i
        left_ma = l
        right_ma = r
ma2 = ma
another = 10 ** 9
another_length = 0 #重なっていない長さ
LR.pop(i_ma)
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:
n = int(input())
lis = [list(map(int,input().split())) for i in range(n)]

lis.sort(key=lambda x:x[1],reverse=True)
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])
# 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

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)

if n == 2:
    print(ls[0][1] - ls[0][0] + ls[1][1] - ls[1][0])
    exit()

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)
print(res)

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],reverse=True)
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:
ans = 0
kukan = []
max_l = 0
min_r = 10**9
n = int(input())
for i in range(n):
    l,r = map(int, input().split())
    l,r = l-1,r-1
    max_l = max(max_l, l)
    min_r = min(min_r, r)
    kukan.append((l,r))
kukan = list(set(kukan))
kukan.sort()

if len(kukan) == 1:
    ans = 2*(min_r-max_l+1)
    print(ans)
    exit()

# maxとminが同じ時は？
# 最大区間を取ってくればいいだけ
min_r_l = None
l,r = kukan.pop()
max_l_r = r
for i in range(len(kukan)):
    if kukan[i][1] == min_r:
        min_r_l = kukan[i][0]
        kukan.pop(i)
        break

if min_r_l is None:
    ans = min_r-max_l+1 + max(r-l+1 for l, r in kukan)
    print(ans)
    exit()

# 累積和
# Lが小さい方からK個取ったときに、残りのRの最小値がほしい
cumsum_r = [float("inf")]
for i in reversed(range(len(kukan))):
    cumsum_r.append(min(cumsum_r[-1], kukan[i][1]))
cumsum_r = cumsum_r[::-1]

# 左 : min_r_l, min_r
# 右 : max_l, max_l_r

for i in range(len(kukan)+1):
    # Lに取らない時
    if i == 0:
        left_l = min_r_l
        right_r = min(max_l_r,cumsum_r[i])
    # 1個以上取る時
    else:
        left_l = max(min_r_l, kukan[i-1][0])
        right_r = min(max_l_r,cumsum_r[i])
    tmp = max(0, min_r-left_l+1) + max(0, right_r-max_l+1)
    ans = max(ans, tmp)

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)

    questions.sort(key=lambda x: x[1])
    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


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_k = []

    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_k.append(i)

    for k in range(len(Min_i_k)):
        Min_i = Min_i_k[k]

        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:
N = int(input())
LR = []
for i in range(N):
    L, R = map(int, input().split())
    LR.append([L, R])

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

Lmin = LR[0][0]
Lmax = LR[0][1]
Rmin = LR[-1][0]
Rmax = LR[-1][1]
lpre = Lmax - Lmin + 1
rpre = Rmax - Rmin + 1

for a in LR[1:-1]:
    lmin = max(Lmin, a[0])
    lmax = min(Lmax, a[1])

    rmin = max(Rmin, a[0])
    rmax = min(Rmax, a[1])

    lll = max(0, lmax - lmin + 1)
    rrr = max(0, rmax - rmin + 1)

    l = lll - lpre
    r = rrr - rpre

    if l >= r:
        Lmin = lmin
        Lmax = lmax
        lpre = lll
    
    else:
        Rmin = rmin
        Rmax = rmax
        rpre = rrr

lcnt = max(0, Lmax - Lmin + 1)
rcnt = max(0, Rmax - Rmin + 1)
print(lcnt + rcnt)

Submission status: Wrong Answer

Solution code:
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 - 1):
        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, 0, -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:
n=int(input())
lr=[]
for i in range(n):
	lr.append(list(map(int,input().split())))

dp=[[0,0,0,0] for k in range(n)]
dp[0]=[lr[0][0],lr[0][1],lr[1][0],lr[1][1]]
for i in range(n-2):
	a=-max(dp[i][0],lr[i+2][0])+min(dp[i][1],lr[i+2][1])+dp[i][3]-dp[i][2]+2
	b=-max(dp[i][2],lr[i+2][0])+min(dp[i][3],lr[i+2][1])+dp[i][1]-dp[i][0]+2
	if a>b:
		dp[i+1][0]=max(dp[i][0],lr[i+2][0])
		dp[i+1][1]=min(dp[i][1],lr[i+2][1])
		dp[i+1][2]=dp[i][2]
		dp[i+1][3]=dp[i][3]
	else:
		dp[i+1][0]=dp[i][0]
		dp[i+1][1]=dp[i][1]
		dp[i+1][2]=max(dp[i][2],lr[i+2][0])
		dp[i+1][3]=min(dp[i][3],lr[i+2][1])
tanoshisa=-dp[n-2][0]+dp[n-2][1]-dp[n-2][2]+dp[n-2][3]+2
print(tanoshisa)

Submission status: Wrong Answer

Solution code:
N = int(input())
part = []
for i in range(N):
    L, R = [int(j) for j in input().split()]
    part.append([L, R])
#print(N)
#print(part)

Data = []
cnt = 0
for a, b in part:
    cnt += 1
    m = (a+b)*0.5
    v = b - a + 1
    Data.append([cnt ,m, v, a, b])
#print(Data)

# [[門], [平均], [解答者], [始], [終]]
Data1 = sorted(Data, key=lambda x: x[1], reverse=True)
#print(Data1)

M = len(Data1)//2

st = 0
st2 = 0
ls = 1000000000
ls2 = 1000000000
vic = 0
cnt2 = 0
win = []
for j in range(M):
    p = Data1[j][0] - 1
    #print(p)
    s = part[p][0]
    l = part[p][1]
    if st < s:
        st = s
    if ls > l:
        ls = l
    st2 = max([k[3] for k in Data1[j+1:len(Data1)]])
    ls2 = min([q[4] for q in Data1[j+1:len(Data1)]])
    vic = (ls - st + 1) + (ls2 - st2 + 1) 
    win.append(vic)

print(max(win))

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(n):
    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(n):
    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()
lmax0 = [ls[0][0]]
rmin0 = [ls[0][1]]
for i in range(n):
    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(n):
    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
n = int(input())
lm, rm = 0, 10**10
seg = []
for _ in range(n):
	l, r = map(int, input().split())
	seg.append([l, r])
	lm = max(lm, l)
	rm = min(rm, r)

#print(lm, rm)
fm, lt = 1, 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 = 0
	for i in range(len(amb)):
		ans = max(ans, rm-amb[i][0]+1 + lr_max[i+1]-lm+1)

	print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
# 一番長いLRが最低の楽しさ
# 一番長いLRと重なるのが一番少ないものはもう一つにする
# 一番長いLRが最低の楽しさ 一番長いLRが複数ある場合どうする？
# 一番長いLRと重なるのが一番少ないものはもう一つにする
N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
ma = 0
for i, (l, r) in enumerate(LR):
    if r-l > ma:
        ma = r-l+1
        i_ma = i
        left_ma = l
        right_ma = r
another = 10 ** 9
another_length = 0 #重なっていない長さ
LR.pop(i_ma)
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(point)

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+1) + maxrange + 1 )
    exit()
tmpmax = 0
if maxi != maxlInd:
    if maxi != minrInd:
        tmpmax = max(0, minr - maxl+1) + maxrange + 1
    elif seci != maxlInd and seci != -1:
        tmpmax = max(0, minr - maxl+1) + secmax + 1
    elif thi != -1:
        tmpmax = max(0, minr - maxl+1) + thmax + 1
elif seci != minrInd and seci != -1:
    tmpmax = max(0, minr - maxl+1) + secmax + 1
elif thi != -1:
    tmpmax = max(0, minr - maxl +1) + thmax + 1

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 max(0, rb1 - bd[i][0]+1) + max(0, rb2 - lb2+1) > max(0, rb1 - lb1+1) + max(0, bd[i][1] - lb2+1):
        lb1 = bd[i][0]
    else:
        rb2 = bd[i][1]

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

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([a,b])
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 = right_list.index(1)
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)