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: Accepted

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

bottom=1
top=N

for i in range(M):
    if L[i] > bottom:
        bottom = L[i]
    if R[i] < top:
        top = R[i]

if top >= bottom:
    ans = top-bottom+1
else:
    ans = 0
print(ans)

Submission status: Accepted

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

maxL = float("-inf")
minR = float("inf")

for i in range(m):
    l, r = [int(i) for i in input().split()]
    maxL, minR = max(maxL, l), min(minR, r)

print(max(0, minR - maxL + 1))

Submission status: Accepted

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

Lmax = 1
Rmin = N
for line in sys.stdin:  # much faster than input
    L, R = map(int, line.split())
    Lmax = max(L, Lmax)
    Rmin = min(R, Rmin)

print(max(0, Rmin - Lmax + 1))

Submission status: Accepted

Solution code:
import numpy as np

n,m = map(int,input().split())
count = 0
Li = []
Ri = []
for i in range(m):
  l,r = map(int,input().split())
  Li.append(l)
  Ri.append(r)
  
LM = max(Li)
RM = min(Ri)

print(np.maximum(RM - LM +1, 0))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
count = 0
Li = []
Ri = []
for i in range(m):
    l,r = map(int,input().split())
    Li.append(l)
    Ri.append(r)

Limax = max(Li)
Rimin = min(Ri)

for j in range(1,n+1):
    if Limax<=j<=Rimin:
        count+=1
    else:
        pass
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    cards = [0] * (n + 1)
    for _ in range(m):
        l, r = map(int, input().split())
        cards[l - 1] += 1
        cards[r] -= 1
    x = 0
    res = 0
    for c in cards[:-1]:
        x += c
        if x == m:
            res += 1
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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]

if keymax >= keymin:
	print(keymax-keymin+1)
else:
	print("0")

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
x = []
y = []
for i in range(M):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
z = min(y)-max(x)
if z < 0:
    print(0)
elif z+1 >= N:
    print(N)
else:
    print(z+1)

Submission status: Accepted

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

lower, upper = 1, n

for _ in range(m):
    a,b=map(int,input().split())
    lower=max(lower, a)
    upper=min(upper, b)

print(max(upper-lower+1, 0))

Submission status: Accepted

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

l = []
r = []
for i in range(m):
    tmp_l,tmp_r = map(int,input().split())
    l.append(tmp_l)
    r.append(tmp_r)

ll = l[0]
rr = r[0]

for i in range(1,m):
    if ll < l[i]:
        ll = l[i]
    if rr > r[i]:
        rr = r[i]

cnt = rr-ll+1
if cnt >= 1:
    print(cnt)
else:
    print(0)

Submission status: Accepted

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

N, M = [int(item) for item in input().split()]

L, R = [0]*M, [0]*M
for i in range(M):
    L[i], R[i] = [int(item) for item in input().split()]

print(max(min(R)-max(L)+1, 0))

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split()]
values = [list(map(lambda x:int(x), input().split())) for x in range(m)]
max = -1
min = values[0][1]

for (left, right) in values:
    if(right < min):
        min = right
    if(left > max):
        max = left

if(max > min):
    print(0)
else:
    print(min - max + 1)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(M):
    l, r = [int(x) for x in input().split(" ")]
    L.append(l - 1)
    R.append(r - 1)

L_max = max(L)
R_min = min(R)

count = R_min - L_max + 1 if R_min - L_max >= 0 else 0
print(count)

Submission status: Accepted

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

for i in range(m):
    r,l = map(int,input().split())
    if i==0:
        x = r
        y = l
    else:
        if r>x:
            x = r
        if l<y:
            y = l

if y>=x:
    print(y-x+1)
else:
    print(0)

Submission status: Accepted

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

a = max(L)
b = min(R)

ans = max(0, b-a+1)
print(ans)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
ma = 0
mi = 10 ** 9
for i in range(b):
    c, d = map(int, input().split())
    ma = max(ma, c)
    mi = min(mi, d)
if ma > mi:
    print(0)
else:
    print(abs(ma - mi) + 1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lll = []
rrr = []
for _ in range(m):
  lr = list(map(int, input().split()))
  lll.append(lr[0])
  rrr.append(lr[1])
maxl = max(lll)
minr = min(rrr)
count = 0
for i in range(1,n+1):
  if maxl <= i <= minr:
    count += 1
print(count)

Submission status: Accepted

Solution code:
n, m = [int(_) for _ in input().split()]

ls = []
rs = []
for i in range(m):
    lr = [int(_) for _ in input().split()]
    ls.append(lr[0])
    rs.append(lr[1])

print(max(min(rs) - max(ls) + 1, 0))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
ans,L,R=0,0,N
for i in range(M):
  Ltmp,Rtmp=map(int,input().split())
  if L<Ltmp:
    L=Ltmp
  if R>Rtmp:
    R=Rtmp
if L<=R:
  ans=R-L+1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#ABC127C
import sys

hoge = input().split()
n=int(hoge[0])
m=int(hoge[1])

l=[]
r=[]

for i in range(0,m):
	tmp = input().split()
	hoge = list(map(lambda a: int(a), tmp))
	l.append(hoge[0])
	r.append(hoge[1])

maxl=l[0]
minr=r[0]
for i in range(1,m):
	maxl=max(maxl,l[i])
	minr=min(minr,r[i])

minr=min(minr,n)
if(minr<maxl):
	print(0)
else:
	print(minr-maxl+1)

Submission status: Accepted

Solution code:
# 解き直し

n, m = map(int, input().split())

arr = [0 for _ in range(n+2)]
for _ in range(m):
  l, r = map(int, input().split())
  arr[l] += 1
  arr[r+1] -= 1

for i in range(n):
  arr[i+1] += arr[i]

count = 0
for ai in arr:
  if ai == m:
    count += 1

print(count)

Submission status: Accepted

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

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

card2canopen_dict = {i: True for i in range(1, n+1)}

# 重ね合わせの区間を調べれば良い気がする
kukan_min, kukan_max = 1, n

for i, lr in enumerate(lr_list):
    l, r = lr[0], lr[1]
    if l >= kukan_min:
        kukan_min = l
    if r <= kukan_max:
        kukan_max = r

if kukan_max < kukan_min:
    ans = 0
else:
    ans = len(range(kukan_min, kukan_max+1))

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

N,M = map(int,stdin.readline().rstrip().split())
lList = []
rList = []
for i in range(M):
    l,r = map(int,stdin.readline().rstrip().split())
    lList.append(l)
    rList.append(r)
lMax = max(lList)
rMin = min(rList)
ans = rMin - lMax +1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
L0, R0 = map(int,input().split())
for i in range(m-1):
    l, r = map(int,input().split())
    L0 = max(L0,l)
    R0 = min(R0,r)

if L0 <= R0:
    print(R0-L0+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
low = high = 0
lr = [tuple(map(int, input().split())) for _ in range(m)]
l = [x[0] for x in lr]
r = [x[1] for x in lr]
print(max(0, min(r) - max(l) + 1))

Submission status: Accepted

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

for i in range(M):
    l,r = map(int,input().split())
    L.append(l-1)
    R.append(r)

x = min(R) - max(L)
if x>=0:
    print(x)
else:
    print(0)

Submission status: Accepted

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

LMAX = 1
RMIN = n

for i in range(m):
    l, r = map(int, input().split())
    LMAX = max(LMAX, l)
    RMIN = min(RMIN, r)

if LMAX <= RMIN:
    print(RMIN - LMAX + 1)
else:
    print(0)

Submission status: Accepted

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:
        print(0)
    else:
        print(abs(atmax-atmin)+1)



if __name__== "__main__":
    main()

Submission status: Accepted

Solution code:
N,M= map(int,input().split())
LRlis = [list(map(int, input().split())) for i in range(M)]
maxL = 0
minR = 10**5
for i in range(M):
  maxL = max(LRlis[i][0],maxL)
  minR = min(LRlis[i][1],minR)

ans = minR-maxL    
if ans>=0:
  print(ans+1)
else:
  print(0)

Submission status: Accepted

Solution code:
N,M = [int(s) for s in input().split()]
l=1
r=N
for i in range(M):
  x,y = [int(s) for s in input().split()]
  l=max([x,l])
  r=min([y,r])
print(max([r-l+1,0]))

Submission status: Accepted

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

for i in range(m):
    l,r = map(int, input().split())
    L.append(l)
    R.append(r)
if min(R) - max(L) + 1 >= 0:
    print(min(R) - max(L) + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = 0
d = 10 ** 9
for i in range(b):
    e, f = map(int, input().split())
    c = max(c, e)
    d = min(d, f)
print(d-c+1 if c <= d else 0)

Submission status: Accepted

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

L = [0] * M
R = [0] * M

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

print(max(0, min(R) - max(L) + 1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)

#s[i] is linked with t[len(s)-i-1]
def main():
	n,m = map(int,input().split())
	l,r = 1,n
	for i in range(m):
		t,e = map(int,input().split())
		if(l<t):
			l=t
		if(r>e):
			r=e
	print(0 if r-l+1<0 else r-l+1)

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
min_ = 0
max_ = 100001
for i in range(m):
  li = list(map(int,input().split()))
  
  if min(li) >= min_ :
    min_ = min(li)
  if max(li) <= max_:
    max_ = max(li)
if min_ > max_:
    print(0)
else:
    print(max_ - min_ + 1)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
# print(n,m)
i=0
mini=0
maxi=0
while(i<m):
	if(i==0):
		mini,maxi=map(int,input().split())
			
	else:
		mint,maxt=map(int,input().split())
		mini=max(mini,mint)
		maxi=min(maxi,maxt)

	i+=1
t=maxi-mini+1
if(t<0):
	print(0)
else:
	print(t)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
l,r = map(int,input().split())
k = min(r,N)
for i in range(M-1):
    ll,rr = map(int,input().split())
    if ll>k or rr<l:
        print(0)
        exit()
    if ll >l : l = ll
    if rr < k : k = rr

print(k-l+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())

for _ in range(m-1):
  templ, tempr = map(int, input().split())
  l = max(l, templ)
  r = min(r, tempr)

if l <= r:
  print(r-l+1)
else:
  print(0)

Submission status: Accepted

Solution code:
import sys
from itertools import product

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

Lmax = 1
Rmin = N

for i in range(M):
    L,R = list(map(int, input().split()))
    Lmax = max(Lmax, L)
    Rmin = min(Rmin, R)

if Lmax <= Rmin:
    print(Rmin-Lmax+1)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
max_l = 0
min_r = 10 ** 9
for _ in range(M):
    l, r = map(int,input().split())
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r
print(max(min_r-max_l+1, 0))

Submission status: Accepted

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

left = -1
right = 1e9

for i in range(M):
    left = max(left, L[i])
    right = min(right, R[i])

ans = right - left + 1
ans = max(ans, 0)
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
info=[]
for i in range(m):
    info.append(list(map(int,input().split())))
start,goal=info[0][0],info[0][1]
c=0

for i in range(1,m):
    start=max(start,info[i][0])
    goal=min(goal,info[i][1])
    if start>goal:
        c=1
        break
    
if c==0:
    print(goal-start+1)
else:
    print(0)

Submission status: Accepted

Solution code:
def main22():
    data = input().split()
    num = []
    for i in range(2):
        num.append(int(data[i]))
    gate = []
    for i in range(num[1]):
        karin = []
        karid = input().split()
        for j in range(2):
            karin.append(int(karid[j]))
        gate.append(karin)
    und = 1
    upp = num[0]
    for i in range(num[1]):
        if und < gate[i][0]:
            und = gate[i][0]
        if upp > gate[i][1]:
            upp = gate[i][1]
    result = upp - und + 1
    if result < 0:
        print(0)
    else:
        print(result)
        
if __name__ == "__main__":
    main22()

Submission status: Accepted

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

L_max = max(L)
R_min = min(R)
if R_min >= L_max:
	print(R_min - L_max +1)
else:
	print(0)

Submission status: Accepted

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


for i in range(M):
    l, r = map(int, input().split())
    if L < l:
        L = l
    if r < R:
        R = r
if L <= R:
    ans = R - L + 1
else:
    ans = 0
# print("---------")
# print(L)
# print(R)
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = []
R = []
for _ in range(M):
    tmp1,tmp2 = map(int,input().split())
    L.append(tmp1)
    R.append(tmp2)

max_L = L[0]
min_R = R[0]
for i in range(1,M):
    max_L = max(max_L, L[i])
    min_R = min(min_R, R[i])

if min_R >= max_L:
    res = min_R - max_L + 1
else:
    res = 0
print(res)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
LL = []
RR = []
ans = 0

for i in range(M):
    L, R = list(map(int, input().split()))
    LL.append(L)
    RR.append(R)

ans = max(min(RR) - max(LL) + 1, 0)
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
p = [1,n]
for i in range(0,m):
    lr = list(map(int,input().split()))
    p = [max(p[0],lr[0]),min(p[1],lr[1])]
if p[0]>p[1]:
    print(0)
else:
    print(p[1]-p[0]+1)

Submission status: Accepted

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: Accepted

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

L, R = [], []
for i in range(M):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)

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

Submission status: Accepted

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

for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
L_max = max(L)
L_min = min(R)

for i in range(N):
    if L_max <= i+1 <= L_min:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
id = sorted([list(map(int,input().split())) for _ in range(m)])
maxl = id[0][0]
minr = id[0][1]
for i in range(1,len(id)):
    maxl = max([maxl, id[i][0]])
    minr = min([minr, id[i][1]])

print(max([minr-maxl+1,0]))

Submission status: Accepted

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

gates = [list(map(int, input().split())) for _ in range(m)]
up = min([i[1] for i in gates])
down = max([i[0] for i in gates])


if up >= down:
    print(up-down+1)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
(start, end) = (0, 100000)
for _ in range(M):
    s, e = map(int, input().split())
    start = max((start, s))
    end = min((end, e))
print(end - start + 1 if start <= end else 0)

Submission status: Accepted

Solution code:
N,M=map(int, input().split())
max_l=0
min_r=10**5+1
for i in range(M):
  l,r=map(int, input().split())
  if max_l < l:
    max_l = l
  if r < min_r:
    min_r = r
print(max(min_r - max_l + 1, 0))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l, r = 1, n
for i in range(m):
    li, ri = map(int, input().split())
    if ri<l or r<li:
        print(0)
        exit()
    if l<=li and ri<=r:
        l, r = li, ri
    elif l<=ri<=r:
        r = ri
    elif l<=li<=r:
        l = li
    else:
        continue

print(r-l+1)

Submission status: Accepted

Solution code:
n, m = (int(i) for i in input().split())  
gate = []
for i in range(m):
  line =  [int(i) for i in input().split()] 
  gate.append(line)
L = []
R = []
for i in range(m):
  L.append(gate[i][0])
  R.append(gate[i][1])
R_min = min(R)
L_max = max(L)
n = R_min - L_max
if n < 0:
  print(0)
else:
  print(n+1)

Submission status: Accepted

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


L, R = map(int, input().split())
for _ in range(M-1):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)
ans = max(R - L + 1, 0)
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np


def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    seq = np.zeros(N, dtype=int)
    for _ in range(M):
        l, r = map(int, input().split())
        seq[l-1] += 1
        if r < N:
            seq[r] -= 1
    csum = np.cumsum(seq)
    ans = np.add.reduce(np.where(csum == M, 1, 0))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

l, r = [], []
for _ in range(m):
    ll, rr = map(int, input().split())
    l.append(ll)
    r.append(rr)

print(max(0, min(r) - max(l) + 1))

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
L, R = [None] * M, [None] * M
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: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
l, r = 0, 100000
for _ in range(M):
    L, R = list(map(int, input().split()))
    if L > l:
        l = L
    if R < r:
        r = R

ans = r - l + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# master ID cardの数を求める問題
# 単純に考えてNまいのIDcardに対して、それが全てを通過できるか考えると、
# 計算量はO(N*M)かかるので10^5*10^5で10^10となり、通らない
# 番目の ID カードのうちどれか 1 枚を持っていれば通過できます。
# とのことから、i 番目のゲートを通るIDカードは連続で並んでいるので、
# Mのゲートに対して、開始点(Li)と終了点(Ri)のmax,minをとれば良い
n,m = map(int,input().split())
max_l = 0
min_r = n
for i in range(m):
  l,r = map(int,input().split())
  if max_l <  l:
    max_l = l
  if min_r > r:
    min_r = r
ans = 0
if min_r - max_l +1 > 0:
  ans = min_r - max_l +1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
c=0
d=0
for i in range(m):
    c,d=map(int,input().split())
    a.append(c)
    b.append(d)
c=max(a)
d=min(b)
if(c>d):
    print('0')
elif(d-c+1<=n):
    print('{0}'.format(d-c+1))
else:
    print('{0}'.format(n))

Submission status: Accepted

Solution code:
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

import numpy as np  # 1.8.2

def main():
    N, M = i2nn()
    LR = np.array(ii2nnn(M))
    L = LR[:,0]
    R = LR[:,1]
    n = np.min(R) - np.max(L) + 1
    n = max(n, 0)
    print(n)

main()

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
l, r = [], []

for _ in range(m):
    a, b = list(map(int, input().split()))
    l.append(a)
    r.append(b)

if min(r) < max(l):
    print("0")
else:
    print(min(r) - max(l) + 1)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = []
R = []
for i in range(M):
    tmp = list(map(int, input().split()))
#    print(tmp)
    L.append(tmp[0])
    R.append(tmp[1])


kukan = [max(L), min(R)]
sa = kukan[1] - kukan[0] +1
if sa > 0:
    print(sa)
else:
    print("0")

Submission status: Accepted

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

max_L = max([ i[0] for i in gate])
min_R = min([ i[1] for i in gate])
ans = 0
if max_L > min_R:
	ans = 0
else:
	ans = min_R - max_L + 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(num_card, num_gate, inputs):
    gate_card = list(map(lambda x: string_to_int(x), inputs))
    min_card = 1
    max_card = num_card
    for d in gate_card:
        if d[0] > min_card:
            min_card = d[0]
        if d[1] < max_card:
            max_card = d[1]

    if max_card - min_card >= 0:
        return max_card - min_card + 1
    return 0


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(M))
    print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y:
		return x
	return y
def larger(x, y):
	if x > y:
		return x
	return y

N, M = map(int, input().split())
left = 0
right = N+1
for i in range(0, M):
	L, R = map(int, input().split())
	left = larger(left, L)
	right = smaller(right, R)
if right < left:
	print(0)
else:
	print(right-left+1)

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split()]
min_num = 0
max_num = n

for x in range(m):
    tmp_min, tmp_max = map(int, input().split())
    min_num = max(min_num, tmp_min)
    max_num = min(max_num, tmp_max)

if max_num - min_num + 1 > 0:
    print(max_num - min_num + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = [li[j][0] for j in range(M)]
R = [li[k][1] for k in range(M)]
ctr = 0

if min(R) - max(L) >= 0:
    print(min(R) - max(L) + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
left = 1
right = n

for i in range(m):
    l,r = map(int,input().split())
    if l > left:
        left = l
    if r < right:
        right = r

# print(left,right)
if right >= left:
    print(right-left+1)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
 
l, r = 1, N
 
for _ in range(M):
    a, b = map(int, input().split())
    l = max(l, a)
    r = min(r, b)
 
print(max(0, r - l + 1))

Submission status: Accepted

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

l, r = 1, N

for _ in range(M):
    a, b = map(int, input().split())
    l = max(l, a)
    r = min(r, b)

print(max(0, r - l + 1))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
left = 0
right = n
ls = list(range(n))

for i in range(m):
    l, r = map(int, input().split())
    if left < l-1:
        left = l-1
    if right > r:
        right = r
print(len(ls[left:right]))

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = upper - lower + 1
ans = max(0, ans)
print(ans)

Submission status: Accepted

Solution code:
tamanho=list(map(int,input().rstrip().split()))
intervalo=[1,tamanho[0]]

for i in range(tamanho[1]):
  iterando=list(map(int,input().rstrip().split()))
  intervalo[0]=max(intervalo[0],iterando[0])
  intervalo[1]=min(intervalo[1],iterando[1])

if intervalo[0]>intervalo[1]:
  print(0)
else:
  print(intervalo[1]-intervalo[0]+1)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
max_L = 0
min_R = 1000000

for i in range(m):
  l,r = map(int, input().split())
  max_L = max(max_L, l)
  min_R = min(min_R, r)
  
print((min_R-max_L + 1) if (min_R-max_L) >= 0 else 0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Ls = []
Rs = []
for i in range(M):
  L, R = map(int, input().split())
  Ls.append(L)
  Rs.append(R)
Lmax = max(Ls)
Rmin = min(Rs)
r = 0 if Rmin<Lmax else Rmin-Lmax+1
print(r)

Submission status: Accepted

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

left, right = 0, inf
for lr in LR:
    left = max(left, lr[0])
    right = min(right, lr[1])
#  print(left, right)
if left > right:
    print(0)
else:
    print(right - left + 1)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = [li[j][0] for j in range(M)]
R = [li[k][1] for k in range(M)]
ctr = 0

if min(R) - max(L) >= 0:
    print(min(R) - max(L) + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lmax = 0
rmin = n
for _ in range(m):
    l, r = map(int, input().split())
    lmax = max(lmax, l)
    rmin = min(rmin, r)

if lmax > rmin:
    print(0)
else:
    print(rmin - lmax + 1)