Problem p02661 (python)

Fixed submission:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False
# show_flg = True

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def main():
    N = I()
    A = []
    B = []
    ans = 0

    for i in range(N):
        a, b = MI()
        A.append(a)
        B.append(b)

    A.sort()
    B.sort()

    if N % 2 == 1:
        mid = N // 2
        print(B[mid] - A[mid] + 1)
    else:
        mid1 = N // 2
        mid2 = mid1 - 1
        r = B[mid1] + B[mid2]
        l = A[mid1] + A[mid2]
        print(r - l + 1)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False
# show_flg = True

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def main():
    N = I()
    AB = [None] * N
    BA = [None] * N
    ans = 0
    BA_B_IDX = 0
    BA_A_IDX = 1
    AB_A_IDX = 0
    AB_B_IDX = 1

    for i in range(N):
        a, b = MI()
        AB[i] = (a, b, i)
        BA[i] = (b, a, i)

    BA = sorted(BA, reverse=True)
    AB = sorted(AB)

    if N % 2 == 1:
        mid = N // 2
        mx = BA[mid][BA_B_IDX]
        mn = AB[mid][AB_A_IDX]

        # print(mx, mn, mid, AB)

        mn2 = AB[mid][AB_A_IDX]
        mx2 = BA[mid][AB_B_IDX]

        # print(mx2, mn2, mid, AB)

        if mn2 > mx or mn > mx2:
            print(1 / 0)
            ans = mx - mn + 1 + mx2 - mn2 + 1
            print(ans)
        else:
            print(max(mx, mx2) - min(mn, mn2) + 1)
    else:
        mid = N // 2 - 1
        mx = BA[mid][BA_B_IDX]
        mn = AB[mid][AB_A_IDX]

        # print(mx, mn, mid, AB)

        mn2 = AB[mid][AB_A_IDX]
        mx2 = BA[mid][AB_B_IDX]

        # print(mx2, mn2, mid, AB)

        if mn2 > mx or mn > mx2:
            print(1 / 0)
            ans = mx - mn + 1 + mx2 - mn2 + 1
            print(ans)
        else:
            print(max(mx, mx2) - min(mn, mn2) + 1)


if __name__ == '__main__':
    main()