Problem:


Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Runtime Error

Solution code:

N,M=map(int,input().split())
l=[list(map(int,input().split())) 


inter_l = set([i for i in range(1,N+1)])

for i in range(len(l)):
 if i < len(l)-1:
  min_l = l[i][0]
  max_l = l[i][-1]
  min_next_l = l[i+1][0]
  max_next_l = l[i+1][1]
  l_arr=set([i for i in range(min_l,max_l+1)])
  next_l_arr=set([i for i in range(min_next_l,max_next_l+1)])
  inter_l = next_l_arr & l_arr & inter_l
  
print(len(inter_l))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
L, R = map(int, input().split())
for i in range(M-1):
  l, r = map(int, input().split())
print(R-l+1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
L, R = map(int, input().split())
ans = set(list(range(L, R+1)))
for i in range(M-1):
  L, R = map(int, input().split())
  ans = ans & set(list(range(L, R+1)))
  if len(ans) == 0:
    print(0)
print(len(ansN, M = map(int, input().split())
L = [0] * M
R = [0] * M
L, R = map(int, input().split())
for i in range(M-1):
  l, r = map(int, input().split())
print(R-l+1)

Submission status: Runtime Error

Solution code:
import sys
N,M = map(int,input().split())
L = []
R = []

LL = L[0]
RR = R[0]
for i in range(1,M):
    if L[i]>RR or LL>R[i]:
        print(0)
        sys.exit()
    else :
        LL = max(LL,L[i])
        RR = min(RR,R[i]) 
print(RR-LL+1)

Submission status: Runtime Error

Solution code:
def intersect(a, b, c, d):
    if b <= c or d <= a:
        return None
    else:
        li = sorted([a, b, c, d])
        return li[1], li[2]
        
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(tuple(map(int, input().split())))
    
a, b = LR[0]
for lr in LR:
    a, b = intersect(a, b, *lr)
    
print(b-a + 1)

Submission status: Runtime Error

Solution code:
l_lst, r_lst = [], []
for i in range(m):
  l,r = map(int, input().split())
  l_lst.append(l)
  r_lst.append(r)
res = min(r_lst)-max(l_lst)+1
if res > 0:
  print(res)
else:
  print(0)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
ID = [0] * N
N = {i for i in range(1, N + 1)}
for i in range(M):
    L[i], R[i] = map(int, input().split())
    ID[i] = {j for j in range(L[i], R[i] + 1)}

for i in range(M):
    N = N & ID[i]

print(len(N))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
L=100000
R=100000
for i in range(m):
  l=int(input())
  r=int(input())
  L=max(L,l)
  R=min(R,r)
print(max(0,R-L+1))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
lst=[]
L=100000
R=100000
for i in range(m):
  l=int(input())
  r=int(input())
  L=max(L,l)
  R=min(R,r)
print(max(0,R-L+1))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
lst=[]
for i in range(m):
  l=int(input())
  r=int(input())
  for j in range(l,r+1):
    lst.append(j)
print(len(set(lst)))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
lst=[]
for i in range(n):
  l=int(input())
  r=int(input())
  for j in range(l,r+1):
    lst.append(j)
print(len(set(lst)))

Submission status: Runtime Error

Solution code:
cannot understand the q

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
list_open = []
for i in range(m):
  list_open.append(list((int(i) for i in input().split())))
dict_open = {}
for i in range(m):
  for j in range(list_open[i][0], list_open[i][1] + 1):
    try:
      dict_open[j] += 1
    else:
      dict_open[j] = 1
print(list(dict_open.values()).count(m))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
list_open = []
for i in range(m):
  list_open.append(list((int(i) for i in input().split())))
dict_open = {}
for i in range(m):
  for j in range(list_open[i][0], list_open[i][1] + 1):
    try:
      dict_open[j] += 1
    else:
      dict_open[j] = 1
print(list(dict_open.values()).count(m)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L = []*M
R = []*M
for i in range(M):
  L[i], R[i] = map(int, input().split())
  
ans = min(R)-(max(L)-1)

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
lis = list(map(int,input().split()))

ans = "Yes"
m = 0
for i in range(n):
	if m - lis[i] > 1:
		ans = "No"
		break
	else:
		m = max(m,lis[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int,(input().spit()))
L = []
R = []

for i in range(m):
	l,r = map(int(input().spit()))
	L.append(l)
	R.append(r)

ans = min(R) - max(L) + 1

if ans >0:
	print(ans)
else:
	print(0)

Submission status: Runtime Error

Solution code:
n,m = map(int(input().spit()))
L = []
R = []

for i in range(m):
	l,r = map(int(input().spit()))
	L.append(l)
	R.append(r)

ans = min(R) - max(L) + 1

if ans >0:
	print(ans)
else:
	print(0)

Submission status: Runtime Error

Solution code:
n,m = map(int,(input().split())
          
l=[]
for i in range(m):
	l,r = map(int,input().split())
    li = range(l,r)

for i in li:
	print(i)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
s = {}
for i in range(m):
  l,r = map(int,input().split())
  for j in range(l,r+1):
    l = []
    l.append(i)
    newset = set(l)
  s = s&newset
print(len(s))

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
min_list = []
max_list = []
for i in range(k):
    j, l = list(map(int, input().split())):
    min_list.append(j)
    max_list.append(l)


print(max(0, min(max_list) - max(min_list) + 1))

Submission status: Runtime Error

Solution code:
import numpy as np

N, M= (int(i) for i in input().split())  
lr = [[int(i) for i in input().split()] for i in range(M)] 	
a = np.zeros([M,N])
for i in range(M):
	a[i, lr[i][0]-1:lr[i][1]] = 1
b = list(np.all(a == 1, axis=0))
print(b.count(True))

Submission status: Runtime Error

Solution code:
import numpy as np

N, M= (int(i) for i in input().split())  
lr = [[int(i) for i in input().split()] for i in range(M)] 	
a = np.zeros([M,N])
for i in range(M):
	a[i, lr[i][0]-1:lr[i][1]] = 1
b = list(np.all(a == 1, axis=0))
print(sum(b))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
X = [list(map,int(input().split())) for i in range(M)]

keymax = 10**6
keymin = 0
for i in range(M):
	if X[i][0] > keymin:
		keymin = X[i][0]
	if X[i][1] < keymax:
		keymax = X[i][1]

print(keymax-keymin+1)

Submission status: Runtime Error

Solution code:
n,m=[int(i) for i in input().split()]
a=[]
for i in range(m):
    l,r=[int(i) for i in input().split()]
    a.append(set(range(l,r+1)))
for i in range(m):
  a[0]=a[0] & a[1]
print(len(a))

Submission status: Runtime Error

Solution code:
def main():

    N,M=map(int,input().split())
    #undivList=[map(int,(input().split())) for _ in range(N)]
    undivList=[]
    for _ in range(M):
        l=map(int,input().split())
        undivList +=l
    atmax=max(undivList[0::2])
    atmin=min(undivList[1::2])
    
    if atmax-atmin < 0:
		print(0)
    else:
		print(abs(atmax-atmin)+1)

    


if __name__== "__main__":
    main()

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
L = []
R = []
for _ in range(M):
    l,r = map(int, input().split())
    L.append(l)
    R.append(r)
        
R_ = list(m for m in R if m > max(L))
print(min(R_)-max(L)+1)

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
L = []
R = []
for _ in range(M):
    l,r = map(int, input().split())
    L.append(l)
    R.append(r)
for m in R:
    if m < max(L):
        R.remove(m)
        
R_ = list(m for m in R if m > max(L))
print(min(R_)-max(L)+1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
l = []
r = []
for _ in range(M):
  s, t = map(int, input().split())
  l.append(s)
  r.append(t)
  
lm = max(l)
rm = min(r)
print(max[rm - lm + 1, 0])

Submission status: Runtime Error

Solution code:
a = input().split()
a = [int(i) for i in a]

card_list = [int(i + 1) for i in range(a[0])]

for i in range(a[1]):
    b = input().split()
    b = [int(i) for i in b]
    if b[0] > card_list[0]:
        index = card_list.index(b[0])
        card_list = card_list[index:]
    if b[1] < card_list[-1]:
        index = card_list.index(b[-1])
        card_list = card_list[:index + 1]

print(len(card_list))

Submission status: Runtime Error

Solution code:
a = input().split()
a = [int(i) for i in a]

card_list = [int(i + 1) for i in range(a[0])]

for i in range(a[1]):
    b = input().split()
    b = [int(i) for i in b]
    if b[0] < card_list[0]:
        b[0] = card_list[0]
    if b[1] > card_list[-1]:
        b[1] = card_list[-1]
    gate_list = [int(i) for i in range(b[0], b[1] + 1)]
    card_list = list(set(gate_list) & set(card_list))

print(len(card_list))

Submission status: Runtime Error

Solution code:
N, M = list(map(int, input().split()))
L, R = [], []
for i in range(M):
    L[i], R[i] = list(map(int, input().split()))

ans = min(R) - max(L) + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)] ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)


N, M = LI()
LR = []

for i in range(M):
    LR.append(LI())

#低い方と高い方を求める
lower = None
found = False
for i in range(1,N+1):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        lower = i
        found = True

if lower == None:
    print(0)
    exit()
#  print('lower', lower)
if lower == N:
    print(1)
    exit()

upper = None
found = False
for i in reversed(range(lower+1, N+1)):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        upper = i
        found = True
if upper == None:
    upper = lower
#  print('upper', upper)
print(upper - lower + 1)

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)


N, M = LI()
LR = []

for i in range(M):
    LR.append(LI())

#低い方と高い方を求める
lower = None
found = False
for i in range(1,N+1):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        lower = i
        found = True

#  print('lower', lower)
if lower == N:
    print(1)
    exit()

upper = None
found = False
for i in reversed(range(lower+1, N+1)):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        upper = i
        found = True
if upper == None:
    upper = lower
#  print('upper', upper)
print(upper - lower + 1)

Submission status: Runtime Error

Solution code:
N,M=(int(x) for x in int(input().split()))
L=0
R=N
for i in range(M):
  Li,Ri=(int(x) for x in int(input().split()))
  L=Li if Li>L 
  R=Ri if Ri<R
print(Ri-Li+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
ls = []
rs = []

for _ in range(m):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)

l_max = max(l)
r_min = min(r)
master_card_num = r_min - l_max

if master_card_num < 0:
    print(0)
else:
    print(master_card_num + 1)

Submission status: Runtime Error

Solution code:
    n,m=map(int,input().split())
    maxl=1
    minr=n
    while m:
      l,r=map(int,input().split())
      maxl=max(maxl,l)
      minr=min(minr,r)
      m-=1
    print(max(0,minr-maxl+1))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
maxl=1
minr=N
while m:
  l,r=map(int,input().split())
  maxl=max(maxl,l)
  minr=min(minr,r)
  m-=1
print(max(0,maxl-minr+1))

Submission status: Runtime Error

Solution code:
import sys
from sys import stdin
import numpy as np

N, M = map(int, input().split())
L, R - list(), list()
for i in range(M):
  l,r = stdin.readline().rstrip().split()
  L.append(int(l))
  R.append(int(r))

inputs = np.vstack(L, R).T
  
count = 0
for i in range(1, N+1):
  out = (inputs[:, 0] >= i) & (inputs[:, 1] <= i)
  if out.all():
    count += 0
  
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
n,m = map(int, input().split())
ls = []
for i in range(m):
    l,r = map(int, input().split())
    tmp = list(range(l,r+1))
    ls.append(tmp)
binary = [0] * n
ls = [flatten for inner in ls for flatten in inner]
for i in ls:
    binary[i] += 1
print(binary.count(m))

Submission status: Runtime Error

Solution code:
import numpy as np
n,m = map(int, input().split())
ls = []
for i in range(m):
    l,r = map(int, input().split())
    tmp = list(range(l,r+1))
    ls.append(tmp)
binary = [0] * n

for i in ls:
    for j in i:
        binary[j] += 1
print(binary.count(m))

Submission status: Runtime Error

Solution code:
import numpy as np
n,m = map(int, input().split())
ls = []
for i in range(m):
    l,r = map(int, input().split())
    tmp = list(range(l,r+1))
    ls.append(tmp)

print(ls)

binary = [0] * n

for i in ls:
    for j in i:
        binary[j] += 1
print(binary.count(m))

Submission status: Runtime Error

Solution code:
#C
N, M = map(int, input().split())
Table = [list(map(int, input().split())) for _ in range(M)]
Table.sort(key=lambda x:x[0])
Table.sort(key=lambda x:x[1])
#print(f'Table:{Table}')
first = Table[0][0]
end = Table[0][1]
Table.pop(0)

#print(f'first:{first} end:{end}')
for task in Table:
    #print(f'task:{task}')
    num = task[0]
    #print(f'num:{num}')
    if num <= end:
        first = num
    else:
        ans = 0
        break

if ans == 0:
    print(ans)
else:
    print(end-first+1)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
L=[0]*M
R=[0]*M
for i in range(M):
    L[i],R[i]=map(int,input().split())
def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right
L=quick_sort(L)
R=quick_sort(R)
a=L[M-1]
b=R[0]
if a>b:
  print("0")
else:
  print(b-a+1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
left=0
right=N

for i in range(N):
    L, R = map(int, input().split())
    if L > left:
        left = L
    if R < right:
        right = R

if left <= right:
    print(right-left+1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
left=0
right=0

for i in range(N):
    L, R = map(int, input().split())
    if L > left:
        left = L
    if R < left:
        right = R

if left <= right:
    print(right-left+1)
else:
    print(0)

Submission status: Runtime Error

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

left = 0
right = n

for i in li):
  left = max(i[0], left)
  right = min(i[1], right)
  
print(right - left + 1)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

using namespace std;

#define min_3(a, b, c) min(a, min(b, c))
typedef long long ll;
const int mod = 1e9 + 7;

int main()
{
  int n, m;
  cin >> n >> m;
  int l = 0, r = 100000;
  vector<int> throuth(n + 1, 0);
  for(int i = 0; i < m; i++){
    int left, right;
    cin >> left >> right;
    l = max(l, left);
    r = min(r, right);
  }
  cout << r - l + 1 << endl;
}

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
l = 1
r = n
for i in range(m):
  li, ri = map(int, input().split())
  l = max(l, li)
  r = min(r, ri)
print(max(l - r + 1), 0)

Submission status: Runtime Error

Solution code:

from collections import deque

N, M = map(int, input().split())

cards = deque()

for i in range(N):
    cards.append(i+1)


for i in range(M):
    Li, Ri = map(int, input().split())
    L = cards.popleft()
    if Li <= L:
        cards.appendleft(L)
    else:
        while Li > L + 1 and len(cards) > 0:
            L = cards.popleft()

    R = cards.pop()
    if Ri >= R:
        cards.append(R)
    else:
        while Ri < R - 1 and len(cards) > 0:
            R = cards.pop()

print(len(cards))

Submission status: Runtime Error

Solution code:

from collections import deque

N, M = map(int, input().split())

cards = deque()

for i in range(N):
    cards.append(i+1)


for i in range(M):
    Li, Ri = map(int, input().split())
    L = cards.popleft()
    if Li <= L:
        cards.appendleft(L)
    else:
        while Li > L + 1:
            L = cards.popleft()

    R = cards.pop()
    if Ri >= R:
        cards.append(R)
    else:
        while Ri < R - 1:
            R = cards.pop()

print(len(cards))

Submission status: Runtime Error

Solution code:
n,m=map(int, input().split())
lr=[]
def calc_bit(l,r):
    return "0b"+"0"*(l-1)+"1"*(r-l+1)+"0"*(10**5-r)
ans="0b"+"1"*(10**5)
for i in range(m):
    lr=map(int, input().split())
    ans=bin(int(ans,0)&int(calc_bit(lr[0],lr[1]),0))
ans=str(ans)
print(ans.count("1"))

Submission status: Runtime Error

Solution code:
import numpy as np
#n,m=4,2
n,m=map(int, input().split())
mat=np.zeros((m,n))
#lr=[[1,3],[2,4]]
lr=[]
for i in range(m):
    lr.append(list(map(int, input().split())))
    mat[i][lr[i][0]-1:lr[i][1]]=1
ans=0
#print(mat)
for i in range(n):
    mat_col=mat[:,i]
    if 0 not in mat_col:
        ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
N_M = list(map(int, input().split()))
Lock_min = []
Lock_max = []
for i in range(N_M[1]):
    ADD = list(map(int,input().split()))
    Lock_min.append(ADD[0])
    Lock_max.append(ADD[1])
if min(Lock_max) - max(Lock_min) >= 1:
        print(min(Lock_max) - max(Lock_min))
 else:
        print(0)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
count=0
x=[]
for i in range(m):
  l,r=map(int,input().split())
  x.append([l,r])
  
for j in range(1,n+1):
  for k in range(m):
    if x[k,0]<=j:
      if x[k,1]>=j:
        count+=1
        
print(count)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
count=0
for j in range(1,n+1):
  for i in range(m):
    l,r=map(int,input().split())
    if l<=j:
      if j<=r:
        count+=1
print(count)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
gates = [tuple(map(int, input().split())) for _ in range(M)]

lmax = max(gates, key=lambda x: x[0])[0]
rmin = min(gates, key=lambda x: x[1])[1]

print(max(rmin-lmax+1), 0)

Submission status: Runtime Error

Solution code:
import sys


scan = sys.stdin.readline


def main():
  n, m = map(int, scan().rstrip().split())
  l = 0
  r = n
  
  for _ in range(m):
    l_i, r_i = map(int, scan().rstrip().split())
    l = max(l, l_i)
    r = min(r, r_i)
    
  cards = r - l + 1 if r > l else 0
  
  print(cards)
  

if __name__ = "__main__":
  main()

Submission status: Runtime Error

Solution code:
    import sys
     
    def main():
        sinputl = sys.stdin.readline
     
        num_card, num_gate = [int(x) for x in sinputl().split()]
        input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]
     
        under_limit = [x[0] for x in input]
        over_limit = [x[1] for x in input]
     
        print(max(min(over_limit) - max(under_limit) + 1, 0))
     
     
    if __name__ == "__main__":
        main()

Submission status: Runtime Error

Solution code:
n, m = list(map(int, input().split()))
def cutterl(l, lst) :
    if lst == [] :
        return []
    elif lst[0] < l :
        return cutterl(l, lst[1:])
    else :
        return lst
def cutterr(r, lst) :
    if lst == [] :
        return []
    elif r < lst[-1] :
        return cutterr(r, lst[:-1])
    else :
        return lst
nlist = list(map(lambda x : x + 1, range(n)))
llist=[]
rlist=[]
for i in range(m) :
    l, r = list(map(int, input().split()))
    llist.append(l)
    rlist.append(r)
nlist = cutterl(max(llist), nlist)
nlist = cutterr(min(rlist), nlist)
print(len(nlist))

Submission status: Runtime Error

Solution code:
n, m = list(map(int, input().split()))
def cutterl(l, lst) :
    if lst == [] :
        return []
    elif lst[0] < l :
        return cutterl(l, lst[1:])
    else :
        return lst
def cutterr(r, lst) :
    if lst == [] :
        return []
    elif r < lst[-1] :
        return cutterr(r, lst[:-1])
    else :
        return lst
nlist = list(map(lambda x : x + 1, range(n)))
for i in range(m) :
    l, r = list(map(int, input().split()))
    nlist = cutterl(l, nlist)
    nlist = cutterr(r, nlist)
print(len(nlist))

Submission status: Runtime Error

Solution code:
minlist=[]
maxlist=[]

n,m=map(int,input().split())
for i in range(m):
  l,r=list(map(int,input().split()))
  minlist.append(l)
  if r>0:
    maxlist.append(r)
ans=0
dif=max(minlist)-min(maxlist)
if(diff>=0):
  ans=diff+1
else:
  ans=0
print(ans)

Submission status: Runtime Error

Solution code:
minlist=[]
maxlist=[]

n,m=map(int,input().split())
for i in range(m):
  l,r=list(map(int,input().split()))
  minlist.append(l)
  if r>0:
    maxlist.append(r)
ans=0
dif=max(linlist)-min(maxlist)
if(diff>=0):
  ans=diff+1
else:
  ans=0
print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
for i in range(m):
  l,r=list(map(int,input().split()))
  minlist.append(l)
  if r>0:
    maxlist.append(r)
ans=0
dif=max(linlist)-min(maxlist)
if(diff>=0):
  ans=diff+1
else:
  ans=0
print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
for i in range(m):
  l,r=list(map(int,input().split()))
ans=0
dif=max(l)-min(r)
if(diff>=0):
  ans=diff+1
else:
  ans=0
print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
for i in range(m):
  l,r=list(map(int,input().split()))
ans=0
dif=max(l)-min(r)
if(diff>=0):
  ans=diff+1
else:
  ans=0

Submission status: Runtime Error

Solution code:
#n idカード　m ゲート
n,m = map(int, input().split())
maxlist = []
minlist = []

for i in range(m):
	#l　ゲートの下限　r ゲートの上限
	l , r = list(map(int, input().split()))
	minlist.append(l)
	if r > 0:
		maxlist.append(r)

minid = max(minlist)
maxid = min(maxlist)
okgate = maxid - minid + 1

if okegate >= 0:
	print(okgate)
else:
	print(0)

Submission status: Runtime Error

Solution code:
def solve():
    maxCards, gates = map(int, input().split())
    gateRanges = [tuple(map(int, input().split())) for i in range(gates)]

    for i, permitRange in enumerate(gateRanges):
        if i == 0:
            activeBegin, activeEnd = permitRange
            continue
        begin, end = permitRange
        if not (begin <= activeEnd and end >= activeBegin):
            print(0)
            break
        else:
            activeBegin, activeEnd = max(activeBegin, begin), min(activeEnd, end)
    else:
        if (maxCards < activeBegin):
            return print(0)
        ans = min(activeEnd, maxCards) - activeBegin + 1
    print(ans)
solve()

Submission status: Runtime Error

Solution code:
def solve():
    maxCards, gates = map(int, input().split())
    gateRanges = [tuple(map(int, input().split())) for i in range(gates)]

    for i, permitRange in enumerate(gateRanges):
        if i == 0:
            activeBegin, activeEnd = permitRange
            continue
        begin, end = permitRange
        if not (begin <= activeEnd and end >= activeBegin):
            print(0)
            break
        else:
            activeBegin, activeEnd = max(activeBegin, begin), min(activeEnd, end)
    else:
        if (maxCards < activeBegin):
            return print(0)
        print(activeBegin, activeEnd)
        ans = min(activeEnd, maxCards) - activeBegin + 1
    print(ans)
solve()

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
l=[]
r=[]
for i in range(N):
  a,b=map(int,input().split())
  l.append(a)
  r.append(b)
  
  
ans=min(r)-max(l)+1
print(ans)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
l=[]
r=[]
for i in range(M):
  a,b=map(int,input().split())
  l.append(a)
  r.append(b)
  
  
ans=min(b)-max(a)+1
print(ans)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
l=[]
r=[]
for i in range(N):
  a,b=map(int,input().split())
  l.append(a)
  r.append(b)
  
  
ans=min(b)-max(a)+1
print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
mis,mas=[],[]
for _ in range(m-1):
  tmp=list(map(int,input().split()))
  mis.append(tmp[0])
  mas.append(tmp[1])
ans=max(mas)-min(mis)
print(ans if ans>0 else 0)

Submission status: Runtime Error

Solution code:
from itertools import accumurate
N,M=map(int,input().split())
L = [0]*(N+1)
for _ in range(M):
  l,r = map(lambda x:int(x)-1,input().split())
  L[l]+=1
  L[r+1]+=1

L = list(accumurate(L))
print(L.count(M))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
count = 0
for i in range(M):
    a,b = map(int,input().split())
    if count == 0:
        L = a
        R = b
        count = 1
    else:
        if L < a:
            L =a
            if L>R:
                break
        if R > b:
            R = b
            if R < L:
                break
if M == i+1:
    print(max-min+1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
count = 0
for i in range(M):
    a,b = map(int,input().split())
    if count == 0:
        L = a
        R = b
        count = 1
    else:
        if L < a and a<R:
            L =a
        else:
            break
        if R > b and L < b:
            R = b
        else:
            break
if M == i+1:
    print(max-min+1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
count = 0
for i in range(M):
    a,b = map(int,input().split())
    if count == 0:
        min = a
        max = b
        count = 1
    else:
        if min < a && a<max:
            min =a
        if max > b && b >min:
            max = b
if M == i+1:
    print(max-min+1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
count = 0
for i in range(M):
    a,b = map(int,input().split())
    if count == 0:
        min = a
        max = b
        count = 1
    else:
        if min < a && a<max:
            min =a
        else:
            break
    if max > b && b >min:
        max = b
    else:
            break
if M == i+1:
    print(max-min+1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
count = 0
for i in range(M):
    a,b = map(int,input().split())
    if count == 0:
        min = a
        max = b
        count = 1
    else:
        if min < a && a<max:
            min =a
        else:
            break
        if max > b && b >min:
            max = b
        else:
            break
if M == i+1:
    print(max-min+1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
L=[]
R=[]
for i in range(m):
    l,r=map(int,input().split())
    L[i],R[i]=l,r
print(abs(max(L)-min(R)+1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())

answer = list(range(1, N+1))

for i in range(M):
  L, R = map(int, input().split())
  if L < answer[0]:
    L = answer[0]
  if R > answer[-1]:
    R = answer[-1]
  answer = list(range(L, R+1))

if len(answer) > 0:
  print(len(answer))
else:
  print(0)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())

allL = 1
allR = N
for L, R in map(int, input().split()) in range(M):
  allL = max(allL, L)
  allR = min(allR, R)

print(max(allR - allL + 1, 0))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
gates = []
for i in range(m):
    gates.append(list(map(int, input().split())))
candidate = [1, n]
for gate in gates:
    if candidate[0] < gate[0]:
        candidate[0] = gate[0]
    if gate[1] < candidate[1]:
        candidate[1] = gate[1]
if candidate[1] == canditate[0]:
    print(0)
else:
    print(candidate[1] - candidate[0] + 1)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
s=[]
l= 0
r = n
for i in range(m):
    ln,rn=map(int,input().split())
    if ln>l:
        l=ln
    if rn<r:
        r=rn
print(rn-ln+1)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = []
R = []
for i in range(1,M+1):
  x,y = map(int,input().split())
  L.append(L_val)
  R.append(R_val)
ans = max(min(R) - max(L) + 1,0)
print(ans)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = []
R = []
for i in range(M+1):
  L_val,R_val = map(int,input().split())
  L.append(L_val)
  R.append(R_val)
ans = max(min(R) - max(L) + 1,0)
print(ans)

Submission status: Runtime Error

Solution code:
N, M = [int(x) for x in input().split()]

LR = []
for _ in range(M):
  LR.append([int(x) for x in input().split()])

print(sum(check(n) for n in range(1, N+1)))
def check(n):
  return 1 if all(l <= n <=r for l, r in range LR) else 0

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
res = [0,10**5]
for i in ragne(m):
    l, r = map(int, input().split())
    res[0] = max(res[0], l)
    res[1] = min(res[1], r)

print(max(res[1]-res[0]+1,0))