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:

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions
from operator import itemgetter


import sys
def input():
	return sys.stdin.readline().strip()


N = int(input())

LR = []
max_L = 0
min_R = 10**9 - 1
bunkatsu = 1
for i in range(N):
	L, R = list(map(int, input().split()))
	L -= 1
	R -= 1
	LR.append([L, R])
	max_L = max(max_L, L)
	min_R = min(min_R, R)
	if min_R < max_L:
		bunkatsu += 1
		min_R = R
		max_L = L

LR.sort()

if bunkatsu >= 3:
	max_length = 0
	for i in range(N):
		max_length = max(max_length, LR[i][1] - LR[i][0] + 1)
		print(max_length)
else:
	judge = 0
	if bunkatsu == 1:
		cut = min_R - max_L + 1
		for i in range(N):
			LR[i][1] -= cut
			if LR[i][1] < LR[i][0]:
				judge = 1
	max_length = 0
	for i in range(N):
		max_length = max(max_length, LR[i][1] - LR[i][0] + 1)
	if judge == 1:
		print(max_length + 2*cut)
		exit()
	maxmin = LR[-1][0]
	two = N - 1
	for i in range(N - 1):
		if LR[i][1] < maxmin:
			one = i
			break
	prob_rest = []
	one_L, one_R = LR[one]
	two_L, two_R = LR[two]
	for i in range(N - 1):
		if i == one:
			continue
		L, R = LR[i]
		if one_R < L:
			two_L = max(L, two_L)
			two_R = min(R, two_R)
		elif R < two_L:
			one_L = max(L, one_L)
			one_R = min(R, one_R)
		elif one_L <= L and R <= two_R:
			prob_rest.append(LR[i])
	min_R_list = deque()
	min_R_list.appendleft(two_R)
	for i in range(len(prob_rest) - 1, -1, -1):
		R = prob_rest[i][1]
		min_R_list.appendleft(min(min_R_list[-1], R))
	max_length2 = min_R_list[0] - one_R + 1
	for i in range(1, len(min_R_list)):
		max_length2 = max(max_length2, min_R_list[i] - prob_rest[i - 1][0] + 1)
	max_length2 -= two_L - one_R - 1
	if bunkatsu == 1:
		print(max(max_length2, max_length) + 2*cut)
	else:
		print(max(max_length2, max_length))

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[1])
g1 = [lr[0]]
keep = []
g2 = []
for i in range(1,n):
    if lr[i][0] > g1[0][1]:
        g2.append(lr[i])
    else:
        keep.append(lr[i])
def calc(g):
    r = min([item[1] for item in g])
    l = max([item[0] for item in g])
    return max(0, r-l + 1)

if g2:
    g1r = g1[0][1]
    g2l = max([item[0] for item in g2])
    for item in keep:
        if g1r - item[0] > item[1] - g2l:
            g1.append(item)
        else:
            g2.append(item)
    ans1 = calc(g1) + calc(g2)
    ans2 = max([item[1] - item[0] for item in lr])
    print(max(ans1, ans2))
else:
    lr.sort(key=lambda x: x[1]-x[0])
    ans = lr[-1][1] - lr[-1][0] + calc(lr[:-1]) + 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)

    questions.sort(key=lambda x: x[0])
    distances = [q[1]-q[0]+1 for q in questions]
    index_worst_fun = distances.index(min(distances))
    most_far_q = questions.pop(index_worst_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] + q[1] - most_fun_q[1])

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


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

B = inp[now_len]

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

    elif (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:
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(0,1-max(dp[i][0],lr[i+2][0])+min(dp[i][1],lr[i+2][1]))+max(0,1+dp[i][3]-dp[i][2])
	b=max(0,1-max(dp[i][2],lr[i+2][0])+min(dp[i][3],lr[i+2][1]))+max(0,1+dp[i][1]-dp[i][0])
	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=max(0,1-dp[n-2][0]+dp[n-2][1])+max(0,1-dp[n-2][2]+dp[n-2][3])

hikidashi=lr[0]
del lr[0]
lr.append(hikidashi)

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


print(max(tanoshisa,tanoshisa2))

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)), key=lambda x: x[0])


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

g1, g2, g3 = [], [], []
M = 0

dif = []

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

if g2 == []:
    dif = sorted(dif, key=lambda x: x[0], reverse=True)
    if lr[dif[0][1]][0] == g1[0] or lr[dif[0][1]][1] == g1[1]:
        lr.pop(dif[0][1])
        g4 = []
        for i in range(n-1):
            l, r = lr[i][0], lr[i][1]
            if g4 == []:
                g4.append(l)
                g4.append(r)
            g4[0] = max(g4[0], l)
            g4[1] = min(g4[1], r)
        print(max(M + g1[1]-g1[0]+1, M + g4[1]-g4[0]+1))
    else:
        print(M + g1[1]-g1[0]+1)
elif g3 == []:
    print(max(M, g1[1]-g1[0]+1 + g2[1]-g2[0]+1))
else:
    g5, g6, g7 = [], [], []
    for i in range(n):
        l, r = lr[i][0], lr[i][1]
        if g5 == []:
            g5.append(l)
            g5.append(r)
        if r < g5[0] or l > g5[1]:
            if g6 == []:
                g6.append(l)
                g6.append(r)
            elif r < g6[0] or l > g6[1]:
                g7.append([l, r])
            else:
                g6[0] = max(g6[0], l)
                g6[1] = min(g6[1], r)
        else:
            g5[0] = max(g5[0], l)
            g5[1] = min(g5[1], r)
    if g7 == []:
        print(M+1)
    else:
        print(M)

Submission status: Wrong Answer

Solution code:
import sys
import collections
from builtins import min


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    lr = []
    for i in range(n):
        l, r = list(map(int, input().rstrip('\n').split()))
        lr.append([l, r, abs(l - r) + 1])
    lr.sort(key=lambda x: x[2])
    lr = collections.deque(lr)
    mlr = [[0] * 3 for _ in range(2)]

    l_r = lr.pop()
    mlr[0][0] = l_r[0]
    mlr[0][1] = l_r[1]
    mlr[0][2] = l_r[2]

    l_r = lr.popleft()
    mlr[1][0] = l_r[0]
    mlr[1][1] = l_r[1]
    mlr[1][2] = l_r[2]

    while lr:
        l_r = lr.popleft()
        ab1 = abs(max(l_r[0], mlr[0][0]) - min(l_r[1], mlr[0][1])) + 1
        ab2 = abs(max(l_r[0], mlr[1][0]) - min(l_r[1], mlr[1][1])) + 1
        if abs(ab1 - mlr[0][2]) > abs(ab2 - mlr[1][2]):
            mlr[1][0] = max(mlr[1][0], l_r[0])
            mlr[1][1] = min(mlr[1][1], l_r[1])
            mlr[1][2] = ab2
        elif abs(ab1 - mlr[0][2]) <= abs(ab2 - mlr[1][2]):
            mlr[0][0] = max(mlr[0][0], l_r[0])
            mlr[0][1] = min(mlr[0][1], l_r[1])
            mlr[0][2] = ab1
    print(mlr[0][2] + mlr[1][2])


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
LR = list(zip(m,m))

if N==2:
    L1,R1 = LR[0]
    L2,R2 = LR[1]
    answer = (R1-L1+1) + (R2-L2+1)
    print(answer)
    exit()

# 片方に集める場合
max_width = max(R-L+1 for L,R in LR)

LR.sort()

INF=10**18
# Rが最小のもの
i = -1
opt_R = INF
for n,(L,R) in enumerate(LR):
    if opt_R>=R:
        opt_R=R
        i=n
j = -1
opt_L = -INF
for n,(L,R) in enumerate(LR):
    if opt_L<=L:
        opt_L=L
        j=n

if i==j:
    L,R = LR[i]
    answer = max_width + (R-L+1)
    print(answer)
    exit()

L1,R1 = LR[i]; L2,R2 = LR[j]

LR = [(L,R) for n,(L,R) in enumerate(LR) if n != i and n != j]

LR.sort()

n = len(LR)
dpl = [(-INF,INF)]*(n+1)
dpr = [(-INF,INF)]*(n+1)

for i in range(n):
    a,b = dpl[i]
    L,R = LR[i]
    dpl[i+1] = (max(a,L),min(b,R))
for i in range(n,0,-1):
    a,b = dpr[i]
    L,R = LR[i-1]
    dpr[i-1] = (max(a,L),min(b,R))

answer = 0
for (a,b),(c,d) in zip(dpl,dpr):
    l1 = max(L1,a); r1 = min(R1,b)
    l2 = max(L2,c); r2 = min(R2,d)
    x = max(0,r1-l1+1) + max(0,r2-l2+1)
    if answer < x:
        answer = x

if answer < max_width:
    answer = max_width

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[]]*N

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


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

set3 = [LR[0][0], LR[0][1]]
set4 = [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)


set1 = [set3[0], set3[1]]
set2 = [set4[0], set4[1]]
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])]

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

print(max(ans1, ans2))

Submission status: Wrong Answer

Solution code:
N = int(input())
questions = [list(map(int,input().split())) for _ in range(N)]
questions.sort()
print(questions)
ans = 0
for i in range(1,N - 1):

    left , right = max(questions[0][0] , questions[i][0]) , min(questions[0][1] , questions[i][1])
    s1 = right - left + 1
    left , right = max(questions[i + 1][0] , questions[N - 1][0]) , min(questions[i + 1][1] , questions[N - 1][1])
    s2 = right - left + 1

    tmp_ans = s1 + s2
    if tmp_ans > ans:
        ans = tmp_ans
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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

center = []
length = []
cmin_ind, cmax_ind = 0, 0
cmin = 100100100100
cmax = -1
i = 0
for li, ri in LR:
	centeri = (li+ri)/2.0
	lengthi = ri-li+1
	center.append(centeri)
	length.append(lengthi)

	if centeri<=cmin:
		if not (centeri==cmin and lengthi>length[cmin_ind]):
			cmin_ind = i
			cmin = centeri
	if centeri>=cmax:
		if not (centeri==cmin and lengthi>length[cmax_ind]):
			cmax_ind = i
			cmax = centeri

	i += 1



ans_min = max(length)
si = sorted(range(N), key=lambda k: center[k])

if cmin_ind==cmax_ind:
	cmin_ind = length.index(min(length))
	cmax_ind = length.index(max(length))

g1 = LR[cmin_ind]
g1c = center[cmin_ind]
g2 = LR[cmax_ind]
g2c = center[cmax_ind]

for i in si:
	if i==cmin_ind or i==cmax_ind: continue
	li, ri = LR[i]
	c1dif = min(max(0,li-g1[0])+max(0,g1[1]-ri), g1[1]-g1[0])
	c2dif = min(max(0,li-g2[0])+max(0,g2[1]-ri), g2[1]-g2[0])

	if c1dif<c2dif:
		g1[0], g1[1] = max(li,g1[0]), min(ri,g1[1])
	else:
		g2[0], g2[1] = max(li,g2[0]), min(ri,g2[1])

ans = g1[1]-g1[0]+1+g2[1]-g2[0]+1
ans = max(ans,ans_min)

print(ans)

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([i, L, R])


#print(N)
#print(part)

Data = []
for n, a, b in part:
    m = (a+b)*0.5
    v = b - a + 1
    Data.append([n ,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 = []

st2 = []
st2_max = []
for i in range(1, N):
    st2.append(Data1[-i][3])
    st2_max.append(max(st2))
#print(st2_max)
ls2 = []
ls2_min = []
for i in range(1, N):
    ls2.append(Data1[-i][4])
    ls2_min.append(min(ls2))
#print(ls2_min)


for j in range(M):
    p = Data1[j][0] - 1
    #print(p)
    s = part[p][1]
    l = part[p][2]
    if st < s:
        st = s
    if ls > l:
        ls = l
    st22 = st2_max[N-j-2]
    ls22 = ls2_min[N-j-2]
    #print(st22, ls22)
    vic = (ls - st + 1) + (ls22 - st22 + 1) 
    win.append(vic)

print(max(win))

Submission status: Wrong Answer

Solution code:
def f(li, new):
    tmp_li = [li[0], li[1], new]
    #tmp_li.sort(reverse=True, key=lambda x: x[2])
    l1, r1, d1 = tmp_li[0]
    l2, r2, d2 = tmp_li[1]
    l3, r3, d3 = tmp_li[2]
    l = [l1, l2, l3]
    r = [r1, r2, r3]
    d = [d1, d2, d3]

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

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


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

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

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

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

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

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], 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
#
# 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

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

    l, r, _ = e[0]
    ll = max(ll, l)
    lr = min(lr, r)

    for i in range(1, n - 1):
        l, r, _ = e[i]
        if rl == -1:
            rl = l
            rr = r
        else:
            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)))

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

    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)

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

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

    for i in range(1, n - 1):
        l, r, _ = e[i]
        if time() - time_counter >= 1.8:
            print(ans)
            exit()

        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:
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))]
	lr_max[-1] = 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)

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

	print(ans)

Submission status: Wrong Answer

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

    lr.sort(key=lambda x: (x[1] - x[0],x[1]))
    box0 = [lr[0][0], lr[0][1]]
    box1 = [lr[1][0], lr[1][1]]
    m=max(box0[1]-box0[0]+1,0)+max(box1[1]-box1[0]+1,0)
    for i in range(2, n):
        a1 = max(min(lr[i][1], box0[1]) - max(lr[i][0], box0[0])+1, 0) + max(box1[1] - box1[0]+1, 0)
        a2 = max(min(lr[i][1], box1[1]) - max(lr[i][0], box1[0])+1, 0) + max(box0[1] - box0[0]+1, 0)
        a3 = max(min(box0[1], box1[1]) - max(box0[0], box1[0])+1, 0) + max(lr[i][1] - lr[i][0]+1, 0)
        m = max(a1, a2, a3)
        if a1 == m:
            box0[0] = max(lr[i][0], box0[0])
            box0[1] = min(lr[i][1], box0[1])
        elif a2 == m:
            box1[0] = max(lr[i][0], box1[0])
            box1[1] = min(lr[i][1], box1[1])
        else:
            box0[0] = max(box0[0], box1[0])
            box0[1] = min(box0[1], box1[1])
            box1[0] = lr[i][0]
            box1[1] = lr[i][1]
    print(m)
main()

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(0,1-max(dp[i][0],lr[i+2][0])+min(dp[i][1],lr[i+2][1]))+max(0,1+dp[i][3]-dp[i][2])
	b=max(0,1-max(dp[i][2],lr[i+2][0])+min(dp[i][3],lr[i+2][1]))+max(0,1+dp[i][1]-dp[i][0])
	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=max(0,1-dp[n-2][0]+dp[n-2][1])+max(0,1-dp[n-2][2]+dp[n-2][3])+2

hikidashi=lr[0]
del lr[0]
lr.append(hikidashi)

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


print(max(tanoshisa,tanoshisa2))

Submission status: Wrong Answer

Solution code:
def f(li, new):
    tmp_li = [li[0], li[1], new]
    #tmp_li.sort(reverse=True, key=lambda x: x[2])
    l1, r1, d1 = tmp_li[0]
    l2, r2, d2 = tmp_li[1]
    l3, r3, d3 = tmp_li[2]
    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)
        new_d = 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:
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][0]:
        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:



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

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

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

                
#    Lsorted = sorted(sorted(LR,key=lambda x:x[1],reverse=True),key=lambda x:[0],reverse=True)
#    minR = Lmax[1]
#    for i in range(1,N):
#        nowmaxL = min(Lsorted[i][0],Rmin[0])
#        maxscore =max(maxscore, max(minR-Lmax[0]+1,0) + max(Rmin[1]-nowmaxL+1,0))
#        minR = max(Lsorted[i][1],minR)

    print(maxscore)

Submission status: Wrong Answer

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

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

first_contest = List[0]

# 問題1に対して最も楽しめない問題をsecond_contestに回す
worst_problem = 0
worst_pleasure_value = 100000000
for i in range(N):
    now_pleasure = pleasure(List[0],List[i])
    now_pleasure_value = now_pleasure[1]-now_pleasure[0]+1
    if worst_pleasure_value > now_pleasure_value:
        worst_pleasure_value = now_pleasure_value
        worst_problem = i
    if worst_pleasure_value == 0: #楽しさ0だったら探索終了
        break
second_contest = List[worst_problem]

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