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 main():
    N = int(input())
    LR = [tuple(map(int, input().split())) for _ in range(N)]
    LR.sort(key=lambda x: x[1])
    l1, r1 = LR[0]
    l2, r2 = LR[-1]
    def ss(a, b, c, d):
        l, r = max(a, c), min(b, d)
        return r - l + 1 if l <= r else 0

    for l, r in LR[1:-1]:
        t1 = ss(l1, r1, l, r) + (r2 - l2)
        t2 = ss(l2, r2, l, r) + (r1 - l1)
        if t1 > t2:
            l1, r1 = max(l1, l), min(r1, r)
            if l1 > r1:
                l1 = r1 + 1
        else:
            l2, r2 = max(l2, l), min(r2, r)
            if l2 > r2:
                l2 = r2 + 1
    return r1 - l1 + r2 - l2 + 2


print(main())

Submission status: Wrong Answer

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

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

A.sort()

res = sorted([a[1] - a[0] for a in A])[-1]

Il = []
L = 1
R = 10**9

for i in range(N):
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Il.append(max(0, R - L + 1))

Ir = []
L = 1
R = 10**9

for i in range(N)[::-1]:
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Ir.append(max(0, R - L + 1))

Ir = Ir[::-1]

for i in range(N - 1):
    res = max(res, Il[i] + Ir[i + 1])

print(res)

Submission status: Wrong Answer

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

N = int(input())
A = [tuple(map(int, input().split())) for _ in range(N)]
A.sort()

Il = []
L = 1
R = 10**9

for i in range(N):
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Il.append(max(0, R - L + 1))

Ir = []
L = 1
R = 10**9

for i in range(N)[::-1]:
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Ir.append(max(0, R - L + 1))

Ir = Ir[::-1]
res = 0

for i in range(N - 1):
    res = max(res, Il[i] + Ir[i + 1])

print(res)

Submission status: Wrong Answer

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

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

Il = []
L = 1
R = 10**9

for i in range(N):
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Il.append(R - L + 1)

Ir = []
L = 1
R = 10**9

for i in range(N)[::-1]:
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Ir.append(R - L + 1)

Ir = Ir[::-1]
res = 0

for i in range(N - 1):
    res = max(res, Il[i] + Ir[i + 1])

print(res)

Submission status: Wrong Answer

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

N = int(input())
A = [tuple(map(int, input().split())) for _ in range(N)]
A.sort()

Il = []
L = 1
R = 10**9

for i in range(N):
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Il.append(R - L + 1)

Ir = []
L = 1
R = 10**9

for i in range(N)[::-1]:
    l, r = A[i]
    L = max(L, l)
    R = min(R, r)
    Ir.append(R - L + 1)

Ir = Ir[::-1]
res = 0

for i in range(N - 1):
    res = max(res, Il[i] + Ir[i + 1])

print(res)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

class SlidingWindowAggretgation:
    def __init__(self, default, f = min):
        self.default = default
        self.f = f
        self.front_stack = deque()
        self.back_stack = deque()

    def get(self):
        res = self.default
        if self.front_stack:
            res = self.f(res, self.front_stack[-1][1])
        if self.back_stack:
            res = self.f(res, self.back_stack[-1][1])
        return res

    def append(self, x):
        fx = x
        if self.back_stack:
            fx = self.f(self.back_stack[-1][1], x)
        self.back_stack.append((x, fx))

    def popleft(self):
        if not self.front_stack:
            x, fx = self.back_stack.pop()
            self.front_stack.append((x, x))
            while self.back_stack:
                x, fx = self.back_stack.pop()
                fx = self.f(x, self.front_stack[-1][1])
                self.front_stack.append((x, fx))
        self.front_stack.pop()

def solve():
    n = I()
    p = LIR(n)
    p.sort()
    maxl = max([l for (l,r) in p])
    minr = min([r for (l,r) in p])
    if [maxl, minr] in p:
        print(minr-maxl+1+max([r-l+1 for (l,r) in p if (l,r) != (maxl,minr)]))
        return
    swagll = SlidingWindowAggretgation(0,max)
    swaglr = SlidingWindowAggretgation(float("inf"),min)
    swagrl = SlidingWindowAggretgation(0,max)
    swagrr = SlidingWindowAggretgation(float("inf"),min)
    for l,r in p:
        swagrl.append(l)
        swagrr.append(r)
    ans = 0
    for l,r in p[:-1]:
        swagll.append(l)
        swaglr.append(r)
        swagrl.popleft()
        swagrr.popleft()
        ll = swagll.get()
        lr = swaglr.get()
        rl = swagrl.get()
        rr = swagrr.get()
        k = max(lr-ll+1,0)+max(rr-rl+1,0)
        if ans < k:
            ans = k
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

class SlidingWindowAggretgation:
    def __init__(self, default, f = min):
        self.default = default
        self.f = f
        self.front_stack = deque()
        self.back_stack = deque()

    def get(self):
        res = self.default
        if self.front_stack:
            res = self.f(res, self.front_stack[-1][1])
        if self.back_stack:
            res = self.f(res, self.back_stack[-1][1])
        return res

    def append(self, x):
        fx = x
        if self.back_stack:
            fx = self.f(self.back_stack[-1][1], x)
        self.back_stack.append((x, fx))

    def popleft(self):
        if not self.front_stack:
            x, fx = self.back_stack.pop()
            self.front_stack.append((x, x))
            while self.back_stack:
                x, fx = self.back_stack.pop()
                fx = self.f(x, self.front_stack[-1][1])
                self.front_stack.append((x, fx))
        self.front_stack.pop()

def solve():
    n = I()
    p = LIR(n)
    p.sort()
    swagll = SlidingWindowAggretgation(0,max)
    swaglr = SlidingWindowAggretgation(float("inf"),min)
    swagrl = SlidingWindowAggretgation(0,max)
    swagrr = SlidingWindowAggretgation(float("inf"),min)
    for l,r in p:
        swagrl.append(l)
        swagrr.append(r)
    ans = 0
    for l,r in p[:-1]:
        swagll.append(l)
        swaglr.append(r)
        swagrl.popleft()
        swagrr.popleft()
        ll = swagll.get()
        lr = swaglr.get()
        rl = swagrl.get()
        rr = swagrr.get()
        k = max(lr-ll+1,0)+max(rr-rl+1,0)
        if ans < k:
            ans = k
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

class SlidingWindowAggretgation:
    def __init__(self, default, f = min):
        self.default = default
        self.f = f
        self.front_stack = deque()
        self.back_stack = deque()

    def get(self):
        res = self.default
        if self.front_stack:
            res = self.f(res, self.front_stack[-1][1])
        if self.back_stack:
            res = self.f(res, self.back_stack[-1][1])
        return res

    def append(self, x):
        fx = x
        if self.back_stack:
            fx = self.f(self.back_stack[-1][1], x)
        self.back_stack.append((x, fx))

    def popleft(self):
        if not self.front_stack:
            x, fx = self.back_stack.pop()
            self.front_stack.append((x, x))
            while self.back_stack:
                x, fx = self.back_stack.pop()
                fx = self.f(x, self.front_stack[-1][1])
                self.front_stack.append((x, fx))
        self.front_stack.pop()

def solve():
    n = I()
    p = LIR(n)
    maxl = max([l for (l,r) in p])
    minr = min([r for (l,r) in p])
    p.sort(key = lambda x:(max(minr-x[0]+1,0),-max(x[1]-maxl+1,0)))
    for l,r in p:
        if (l,r) == (maxl,minr):
            ans = r-l+1+max([r-l+1 for (l,r) in p])
            print(ans)
            return
    swagll = SlidingWindowAggretgation(0,max)
    swaglr = SlidingWindowAggretgation(float("inf"),min)
    swagrl = SlidingWindowAggretgation(0,max)
    swagrr = SlidingWindowAggretgation(float("inf"),min)
    for l,r in p:
        swagrl.append(l)
        swagrr.append(r)
    ans = 0
    for l,r in p[:-1]:
        swagll.append(l)
        swaglr.append(r)
        swagrl.popleft()
        swagrr.popleft()
        ll = swagll.get()
        lr = swaglr.get()
        rl = swagrl.get()
        rr = swagrr.get()
        k = max(lr-ll+1,0)+max(rr-rl+1,0)
        if ans < k:
            ans = k
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

class SlidingWindowAggretgation:
    def __init__(self, default, f = min):
        self.default = default
        self.f = f
        self.front_stack = deque()
        self.back_stack = deque()

    def get(self):
        res = self.default
        if self.front_stack:
            res = self.f(res, self.front_stack[-1][1])
        if self.back_stack:
            res = self.f(res, self.back_stack[-1][1])
        return res

    def append(self, x):
        fx = x
        if self.back_stack:
            fx = self.f(self.back_stack[-1][1], x)
        self.back_stack.append((x, fx))

    def popleft(self):
        if not self.front_stack:
            x, fx = self.back_stack.pop()
            self.front_stack.append((x, x))
            while self.back_stack:
                x, fx = self.back_stack.pop()
                fx = self.f(x, self.front_stack[-1][1])
                self.front_stack.append((x, fx))
        self.front_stack.pop()

def solve():
    n = I()
    p = LIR(n)
    p.sort(key = lambda x:(x[0],-x[1]))
    maxl = max([l for (l,r) in p])
    minr = min([r for (l,r) in p])
    for l,r in p:
        if (l,r) == (maxl,minr):
            ans = r-l+1+max([r-l+1 for (l,r) in p])
            print(ans)
            return
    swagll = SlidingWindowAggretgation(0,max)
    swaglr = SlidingWindowAggretgation(float("inf"),min)
    swagrl = SlidingWindowAggretgation(0,max)
    swagrr = SlidingWindowAggretgation(float("inf"),min)
    for l,r in p:
        swagrl.append(l)
        swagrr.append(r)
    ans = 0
    for l,r in p[:-1]:
        swagll.append(l)
        swaglr.append(r)
        swagrl.popleft()
        swagrr.popleft()
        ll = swagll.get()
        lr = swaglr.get()
        rl = swagrl.get()
        rr = swagrr.get()
        k = max(lr-ll+1,0)+max(rr-rl+1,0)
        if ans < k:
            ans = k
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

class SlidingWindowAggretgation:
    def __init__(self, default, f = min):
        self.default = default
        self.f = f
        self.front_stack = deque()
        self.back_stack = deque()

    def get(self):
        res = self.default
        if self.front_stack:
            res = self.f(res, self.front_stack[-1][1])
        if self.back_stack:
            res = self.f(res, self.back_stack[-1][1])
        return res

    def append(self, x):
        fx = x
        if self.back_stack:
            fx = self.f(self.back_stack[-1][1], x)
        self.back_stack.append((x, fx))

    def popleft(self):
        if not self.front_stack:
            x, fx = self.back_stack.pop()
            self.front_stack.append((x, x))
            while self.back_stack:
                x, fx = self.back_stack.pop()
                fx = self.f(x, self.front_stack[-1][1])
                self.front_stack.append((x, fx))
        self.front_stack.pop()

def solve():
    n = I()
    p = LIR(n)
    p.sort(key = lambda x:(x[1],-x[0]))
    swagll = SlidingWindowAggretgation(0,max)
    swaglr = SlidingWindowAggretgation(float("inf"),min)
    swagrl = SlidingWindowAggretgation(0,max)
    swagrr = SlidingWindowAggretgation(float("inf"),min)
    for l,r in p:
        swagrl.append(l)
        swagrr.append(r)
    ans = 0
    for l,r in p[:-1]:
        swagll.append(l)
        swaglr.append(r)
        swagrl.popleft()
        swagrr.popleft()
        ll = swagll.get()
        lr = swaglr.get()
        rl = swagrl.get()
        rr = swagrr.get()
        k = max(lr-ll+1,0)+max(rr-rl+1,0)
        if ans < k:
            ans = k
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

class SlidingWindowAggretgation:
    def __init__(self, default, f = min):
        self.default = default
        self.f = f
        self.front_stack = deque()
        self.back_stack = deque()

    def get(self):
        res = self.default
        if self.front_stack:
            res = self.f(res, self.front_stack[-1][1])
        if self.back_stack:
            res = self.f(res, self.back_stack[-1][1])
        return res

    def append(self, x):
        fx = x
        if self.back_stack:
            fx = self.f(self.back_stack[-1][1], x)
        self.back_stack.append((x, fx))

    def popleft(self):
        if not self.front_stack:
            x, fx = self.back_stack.pop()
            self.front_stack.append((x, x))
            while self.back_stack:
                x, fx = self.back_stack.pop()
                fx = self.f(x, self.front_stack[-1][1])
                self.front_stack.append((x, fx))
        self.front_stack.pop()

def solve():
    n = I()
    p = LIR(n)
    p.sort(key = lambda x:(x[0],-x[1]))
    swagll = SlidingWindowAggretgation(0,max)
    swaglr = SlidingWindowAggretgation(float("inf"),min)
    swagrl = SlidingWindowAggretgation(0,max)
    swagrr = SlidingWindowAggretgation(float("inf"),min)
    for l,r in p:
        swagrl.append(l)
        swagrr.append(r)
    ans = 0
    for l,r in p[:-1]:
        swagll.append(l)
        swaglr.append(r)
        swagrl.popleft()
        swagrr.popleft()
        ll = swagll.get()
        lr = swaglr.get()
        rl = swagrl.get()
        rr = swagrr.get()
        k = max(lr-ll+1,0)+max(rr-rl+1,0)
        if ans < k:
            ans = k
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N, *X = map(int, open(0).read().split())
inf = []
L = 0
R = float('inf')
M = 0
for l,r in zip(*[iter(X)]*2):
  r += 1
  inf += [(l,r)]
  L = max(L,l)
  R = min(R,r)
  M = max(M,r-l)

a = []
b = []

for i in range(N):
  a.append((max(inf[i][1]-L,0),i))
  b.append((max(R-inf[i][0],0),i))
a.sort()
ls = []
m = float('inf')
for i in range(N):
  c,j = a[i]
  m = min(m,b[j][0])
  ls += [m]
n = 0
for i in range(1,N):
  n = max(n,a[i][0]+ls[i-1])
ans = max(n,M+R-L)
print(ans)

Submission status: Wrong Answer

Solution code:
N, *X = map(int, open(0).read().split())
inf = []
L = 0
R = float('inf')
M = 0
for l,r in zip(*[iter(X)]*2):
  r += 1
  inf += [(l,r)]
  L = max(L,l)
  R = min(R,r)
  M = max(M,r-l)

a = []
b = []

for i in range(N):
  a.append(((max(inf[i][1]-L,0)),i))
  b.append(((max(R-inf[i][0],0)),i))
a.sort()
ls = []
m = float('inf')
for i in range(N):
  c,j = a[i]
  m = min(m,b[j][0])
  ls += [m]
n = 0
for i in range(1,N):
  n = max(n,a[i][0]+ls[i-1])
ans = max(n,M+R-L)
print(ans)

Submission status: Wrong Answer

Solution code:
N, *L = map(int, open(0).read().split())
inf = []
ls = []
rs = []
fl = 0
fr = float('inf')
for i,x in enumerate(zip(*[iter(L)]*2)):
  l,r = x
  ls.append((l,i))
  rs.append((r,i))
  inf.append((l,r))
ls.sort()
rs.sort(reverse=True)
m = 0
used = [False]*N
l1 = -1
l2 = -2
r1 = -1
r2 = -2
while True:
  while l1>=-N:
    i = ls[l1][1]
    if not used[i]:
      break
    l1 -= 1
  else:
    break
  while r1>=-N:
    i = rs[r1][1]
    if not used[i]:
      break
    r1 -= 1
  else:
    break
  while l2>=-N:
    i = ls[l2][1]
    if (not used[i]) and l1>l2:
      break
    l2 -= 1
  else:
    break
  while r2>=-N:
    i = rs[r2][1]
    if (not used[i]) and r1>r2:
      break
    r2 -= 1
  else:
    break
  x = rs[r1][0]-ls[l1][0]+1
  fa = m+x
  
  ll, i = ls[l1]
  rr, j= rs[r1]
  if i==j:
    lr = inf[i][1]
    a,b = max(ll,fl),min(lr,fr)
    p,q = a,b
    lm = b-a+1
    lx = rs[r1][0]-ls[l2][0]+1
    la = lm+lx
    rm = lm
    ra = la
  else:
    lr = inf[i][1]
    a = max(ll,fl)
    b = min(lr,fr)
    lm = b-a+1
    lx = rs[r1][0]-ls[l2][0]+1
    la = lm+lx
  
    rl = inf[j][0]
    p = max(rl,fl)
    q = min(rr,fr)
    rm = q-p+1
    rx = rs[r2][0]-ls[l1][0]+1
    ra = rm+rx
  if la>ra:
    if la>fa:
      fl = a
      fr = b
      m = lm
      used[i] = True
    else:
      break
  else:
    if ra>fa:
      fl = p
      fr = q
      m = rm
      used[j] = True
    else:
      break
first = [0,float('inf')]
second = [0,float('inf')]
for i,flag in enumerate(used):
  if flag:
    first[0] = max(first[0], inf[i][0])
    first[1] = min(first[1], inf[i][1])
  else:
    second[0] = max(second[0], inf[i][0])
    second[1] = min(second[1], inf[i][1])
if float('inf') in first or float('inf') in second:
  print(0)
else:
  ans = first[1]-first[0]+1 + second[1]-second[0]+1
  print(max(0,ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 15 22:01:38 2019

@author: aozora
"""
N, *L = map(int, open(0).read().split())
inf = []
ls = []
rs = []
fl = 0
fr = float('inf')
for i,x in enumerate(zip(*[iter(L)]*2)):
  l,r = x
  ls.append((l,i))
  rs.append((r,i))
  inf.append((l,r))
ls.sort()
rs.sort(reverse=True)
m = 0
x = rs[-1][0]-ls[-1][0]
used = [False]*N
l1 = -1
l2 = -2
r1 = -1
r2 = -2
while True:
  while l1>=-N:
    i = ls[l1][1]
    if not used[i]:
      break
    l1 -= 1
  else:
    break
  while r1>=-N:
    i = rs[r1][1]
    if not used[i]:
      break
    r1 -= 1
  else:
    break
  while l2>=-N:
    i = ls[l2][1]
    if (not used[i]) and l1>l2:
      break
    l2 -= 1
  else:
    break
  while r2>=-N:
    i = rs[r2][1]
    if (not used[i]) and r1>r2:
      break
    r2 -= 1
  else:
    break
  x = rs[r1][0]-ls[l1][0]+1
  fa = m+x
  
  ll, i = ls[l1]
  lr = inf[i][1]
  a = max(ll,fl)
  b = min(lr,fr)
  lm = b-a+1
  lx = rs[r1][0]-ls[l2][0]+1
  la = lm+lx
  
  rr, j = rs[r1]
  rl = inf[j][0]
  p = max(rl,fl)
  q = min(rr,fr)
  rm = q-p+1
  rx = rs[r2][0]-ls[l1][0]+1
  ra = rm+rx
  if la>ra:
    if la>fa:
      fl = a
      fr = b
      m = lm
      used[i] = True
      l1 -= 1
    else:
      break
  else:
    if ra>fa:
      fl = p
      fr = q
      m = rm
      used[j] = True
      r1 -= 1
    else:
      break
first = [0,float('inf')]
second = [0,float('inf')]
for i,flag in enumerate(used):
  if flag:
    first[0] = max(first[0], inf[i][0])
    first[1] = min(first[1], inf[i][1])
  else:
    second[0] = max(second[0], inf[i][0])
    second[1] = min(second[1], inf[i][1])
if float('inf') in first or float('inf') in second:
  print(0)
else:
  ans = first[1]-first[0]+1 + second[1]-second[0]+1
  print(max(0,ans))

Submission status: Wrong Answer

Solution code:
N, *L = map(int, open(0).read().split())
inf = []
ls = []
rs = []
fl = 0
fr = float('inf')
for i,x in enumerate(zip(*[iter(L)]*2)):
  l,r = x
  ls.append((l,i))
  rs.append((r,i))
  inf.append((l,r))
ls.sort()
rs.sort(reverse=True)
m = 0
x = rs[-1][0]-ls[-1][0]
used = [False]*N
l1 = -1
l2 = -2
r1 = -1
r2 = -2
while True:
  while l1>=-N:
    i = ls[l1][1]
    if not used[i]:
      break
    l1 -= 1
  else:
    break
  while r1>=-N:
    i = rs[r1][1]
    if not used[i]:
      break
    r1 -= 1
  else:
    break
  while l2>=-N:
    i = ls[l2][1]
    if (not used[i]) and l1>l2:
      break
    l2 -= 1
  else:
    break
  while r2>=-N:
    i = ls[r2][1]
    if (not used[i]) and r1>r2:
      break
    r2 -= 1
  else:
    break
  x = rs[r1][0]-ls[l1][0]+1
  fa = m+x
  
  ll, i = ls[l1]
  lr = inf[i][1]
  a = max(ll,fl)
  b = min(lr,fr)
  lm = b-a+1
  lx = rs[r1][0]-ls[l2][0]+1
  la = lm+lx
  
  rr, j = rs[r1]
  rl = inf[j][0]
  p = max(rl,fl)
  q = min(rr,fr)
  rm = q-p+1
  rx = rs[r2][0]-ls[l1][0]+1
  ra = rm+rx
  if la>ra:
    if la>fa:
      fl = a
      fr = b
      m = lm
      used[i] = True
      l1 -= 1
    else:
      break
  else:
    if ra>fa:
      fl = p
      fr = q
      m = rm
      used[j] = True
      r1 -= 1
    else:
      break
first = [0,float('inf')]
second = [0,float('inf')]
for i,flag in enumerate(used):
  if flag:
    first[0] = max(first[0], inf[i][0])
    first[1] = min(first[1], inf[i][1])
  else:
    second[0] = max(second[0], inf[i][0])
    second[1] = min(second[1], inf[i][1])
if float('inf') in first or float('inf') in second:
  print(0)
else:
  ans = first[1]-first[0]+1 + second[1]-second[0]+1
  print(max(0,ans))

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



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

value=max([R-L for L,R in LR])
#LR=sorted(LR,key=lambda x:x[1])
LR=sorted(LR,key=lambda x:x[0]*10**9+x[1])
a=2*(10**9)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]
maxLlst=[0 for i in range(N)]
maxLlst[0]=LR[0][0]
for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
for i in range(1,N):
    maxLlst[i]=min(minRlst[i-1],LR[i][0])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
maxR=max([R for L,R in LR])
for i in range(0,N-1):
    L,R=LR[i]
    a=max(R-L,0)
    b=max(maxR-LR[-1][0],0)
    value=max(value,a+b)
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



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

value=max([R-L for L,R in LR])
#LR=sorted(LR,key=lambda x:x[1])
LR=sorted(LR,key=lambda x:x[0]*10**9+x[1])
a=2*(10**9)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]

for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    a=max(R-L,0)
    b=max(LR[0][1]-LR[-1][0],0)
    value=max(value,a+b)
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



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

value=max([R-L for L,R in LR])
#LR=sorted(LR,key=lambda x:x[1])
LR=sorted(LR,key=lambda x:x[0]*10**9+x[1])
a=2*(10**9)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]

for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    a=max(R-L,0)
    b=max(LR[-1][0]-LR[0][0],0)
    value=max(value,a+b)
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



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

value=max([R-L for L,R in LR])
#LR=sorted(LR,key=lambda x:x[1])
LR=sorted(LR,key=lambda x:x[0]*10**9+x[1])
a=2*(10**9)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]

for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



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

value=max([R-L for L,R in LR])
LR=sorted(LR,key=lambda x:x[1])
LR=sorted(LR,key=lambda x:x[0])
a=2*(10**9)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]

for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



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

value=max([R-L for L,R in LR])
LR=sorted(LR,key=lambda x:x[1])
LR=sorted(LR,key=lambda x:x[0])
print(LR)
a=2*(10**9)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]

for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)

Submission status: Wrong Answer

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *



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

value=max([R-L for L,R in LR])

LR=sorted(LR,key=lambda x:x[0])
a=2*(10**9)
minRlst=[a for i in range(N)]
minRlst[-1]=LR[-1][1]

for i in range(N-2,-1,-1):
    minRlst[i]=min(minRlst[i+1],LR[i][1])
L1,R1=LR[0]
L2,R2=LR[-1][0],minRlst[1]
for i in range(0,N-1):
    L,R=LR[i]
    L1=L
    R1=min(R1,R)
    R2=minRlst[i+1]
    a=max(R1-L1,0)
    b=max(R2-L2,0)
    value=max(value,a+b)
print(value)