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 = list(map(int, input().split()))
high=10**10
low=0
for i in range(m):
    a,b = list(map(int, input().split()))
    if a>low:
        low=a
    if b<high:
        high=b
if low>high:
    print(0)
else:
    print(high-low+1)

Submission status: Accepted

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

Submission status: Accepted

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

for _ in range(M):
    L, R = map(int, input().split())
    Ls.add(L)
    Rs.add(R)

print(len([i for i in range(max(Ls), min(Rs)+1)]))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
gl = []
for i in range(m):
  g = list(map(int, input().split()))
  gl.append(g)
zeroflag = False
for j in range(1, m):
  db = gl[j][0] - gl[j - 1][0]
  da = gl[j][1] - gl[j - 1][1]
  if db >= 0:
    if da >= 0:
      gl[j][1] = gl[j - 1][1]
  else:
    if da >= 0:
      gl[j][0] = gl[j - 1][0]
      gl[j][1] = gl[j - 1][1]
    else:
      gl[j][0] = gl[j - 1][0]
  if gl[j][0] > gl[j][1]:
    zeroflag = True
    break
if zeroflag == False:
  print(gl[-1][1] - gl[-1][0] + 1)
else:
  print('0')

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
import copy
n, m = map(int, input().split())
lr = []
for i in range(m):
    l, r = map(int, input().split())
    lr.append([l,r])

result = list(range(lr[0][0],lr[0][1]+1))


maximum = lr[0][1]
minimum = lr[0][0]
for i in range(len(lr)):
    maximum = min(maximum,lr[i][1])
    minimum = max(minimum,lr[i][0])

if maximum < minimum:
    print(0)
else:
    print(maximum-minimum+1)

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(M)]
G = [0, N]
def overlap(a, b):
    if max(a[0], b[0]) > min(a[1], b[1]):
        return [0, 0]
    return [max(a[0], b[0]), min(a[1], b[1])]
for m in range(M):
    G = overlap(G, A[m])
if G == [0, 0]:
    print(0)
else:
    print(G[1] - G[0] + 1)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
maxL = 1
minR = N
for i in range(M):
  L, R = map(int, input().split())
  maxL = max(maxL, L)
  minR = min(minR, R)
print(max(minR - maxL +1, 0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mn=0
mx=10**6
for i in range(m):
    a,b=map(int,input().split())
    mn=max(mn,a)
    mx=min(mx,b)
if mn>mx:
    print(0)
else:
    print(mx-mn+1)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
# lr_list = list()
l_list = list()
r_list = list()
for _ in range(m):
    l, r = list(map(int, input().split()))
    l_list.append(l)
    r_list.append(r)
    # lr_list.append(list(map(int, input().split())))

min_r = min(r_list)
max_l = max(l_list)
if max_l <= min_r:
    print(min_r - max_l + 1)
else:
    print(0)

Submission status: Accepted

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

l = 0
r = N+1

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

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n, m = map(int, input().split())
    l = 1
    r = n

    for _ in range(m):
        li, ri = map(int, input().split())
        if l < li:
            l = li
        if r > ri:
            r = ri

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

Submission status: Accepted

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

print(len(range(max(L),min(R)+1)))

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
N, M = [int(x) for x in stdin.readline().rstrip().split()]
C = np.zeros(N)
Rmin = N
Lmax = 1
for i in range(M):
    L, R = [int(x) for x in stdin.readline().rstrip().split()]
    if R < Rmin:
        Rmin = R
    if L > Lmax:
        Lmax = L
if Lmax > Rmin:
    print(0)
else:
    print(N-(Lmax-1)-(N-Rmin))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

num = [0] * (n + 1)
for l, r in lr:
    num[l - 1] += 1
    num[r] -= 1
num = list(accumulate(num))
print(num.count(m))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

num = [0] * (n + 1)
for l, r in lr:
    num[l - 1] += 1
    num[r] -= 1
num = list(accumulate(num))
print(num.count(m))

Submission status: Accepted

Solution code:
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 'None'

n,m = map(int,input().split())
l = 1
r = n
for i in range(m):
    a,b = map(int,input().split())
    if a > l:
        l = a
    if b < r:
        r = b
x = r-l+1
if x < 0:
    x = 0
print(x)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, M = map(int, input().split())
    maxL, minR = 0, N+1
    for i in range(M):
        L, R = map(int, input().split())
        maxL = max(L, maxL)
        minR = min(R, minR)
    ans = minR -maxL + 1
    print(ans if ans>0 else 0)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

N, M = map(int, input().split())
LR = [[int(x) for x in input().split()] for _ in range(M)]

left = 1
right = 10**5

for l, r in LR:
    left = max(left, l)
    right = min(right, r)
ans = right - left + 1
if ans<0:
    print(0)
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
lh=0
rh=10**9
for i in range(m):
  l,r=map(int,input().split())
  if lh<=l:
    lh=l
  if rh>=r:
    rh=r
if rh-lh<0:
  print(0)
else:
  print(rh-lh+1)

Submission status: Accepted

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

if max(s)<=min(S):
    print(min(S)-max(s)+1)
else:
    print('0')

Submission status: Accepted

Solution code:
def main():
    ini_inp=list(map(lambda x:int(x),input().split()))
    N=ini_inp[0]
    M=ini_inp[1]

    L=[]
    R=[]
    for i in range(M):
        sec_inp=list(map(lambda x:int(x),input().split()))
        L.append(sec_inp[0])
        R.append(sec_inp[1])

    low=L[0]
    up=R[0]
    flag=0
    for i in range(1,M):
        if L[i]>up:
            flag=1
            break
        elif L[i]>=low:
            low=L[i]
        elif R[i]<=up:
            up=R[i]

    if flag==0:
        print(up-low+1)
    else:
        print(0)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
a = []
for i in range(M):
    a.append(list(map(int, input().split())))
b = [0, float("INF")]
for i in range(M):
    b[0] = max(a[i][0], b[0])
    b[1] = min(a[i][1], b[1])
if b[0] <= b[1]:
    print(b[1] - b[0] + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
ll, rr = 0, 100000 
for i in range(m):
    l, r = map(int,input().split())
    if ll < l:
        ll = l
    if r < rr:
        rr = r
s = rr -ll
if s < 0:
    s = 0
else:
    s += 1
print(s)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
# = list(map(int, input().split()))
# x = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]
# n = int(input())
# n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    a, b = list(map(int, input().split()))
    l.append(a-1)
    r.append(b-1)

end = n-1
start = 0
for i in range(m):
    if start <= l[i] and r[i] <= end:
        start = l[i]
        end = r[i]
    elif l[i] < start and r[i] <= end:
        end = r[i]
    elif start <= l[i] and end < r[i]:
        start = l[i]

    if end-start+1 < 0:
        print(0)
        exit()
print(end-start+1)

Submission status: Accepted

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

lr=[list(map(int,input().split())) for _ in range(m)]
#print(lr)

imos=[0]*(n+1)
for i in lr:
    start,end=i
    imos[start-1]+=1
    imos[end]-=1
for i in range(1,n):
    imos[i]=imos[i-1]+imos[i]

print(imos.count(m))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l_max = 0
r_min = float('inf')
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_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())
keys = [list(map(int, input().split())) for _ in range(M)]

left = 1
right = N
for key in keys:
    if left < key[0]:
        left = key[0]
    if key[1] < right:
        right = key[1]
if right < left:
    print(0)
else:
    print(right - left + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)
###################################################
def main():
    N,M=list(map(int,list(input().split())))
    A=[0]*(N+1)
    for a in range(M):
        l,r=map(int,input().split())
        l-=1
        r-=1
        A[l]+=1
        A[r+1]-=1
    for a in range(1,len(A)):
        A[a]+=A[a-1]
    print(A.count(M))
main()

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)
###################################################
def main():
    N,M=list(map(int,list(input().split())))
    LR=[]
    for a in range(M):
        LR.append(list(map(int,input().split())))
    l=LR[0][0]
    r=LR[0][1]
    for a in range(M):
        l=max(l,LR[a][0])
        r=min(r,LR[a][1])
    d=max(r-l+1,0)
    print(d)


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
def main():
    N, M = map(int, input().split())
    maxl = -float('inf')
    minr = float('inf')
    for _ in range(M):
        l, r = map(int, input().split())
        if l >= maxl:
            maxl = l
        if r <= minr:
            minr = r
    val = minr - maxl + 1
    if val >= 0:
        print(val)
    else:
        print(0)
main()

Submission status: Accepted

Solution code:
#print#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code

def resolve():
    N,M=pin()
    x=0
    y=N
    for i in range(M):
        a,b=pin()
        x=max(x,a)
        y=min(y,b)
        
    t=(y-x+1)
    if t<0:t=0
    print(t)
            
        
#%%submit!    
resolve()

Submission status: Accepted

Solution code:
def resolve():
    n, m = map(int, input().split())
    lst = [list(map(int, input().split())) for _ in range(m)]

    prev = lst[0]
    for new in lst[1:]:
        prev = [max(prev[0], new[0]), min(prev[1], new[1])]

    print(len(range(prev[0], prev[1]+1)))


resolve()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
left = 1
right = n
for _ in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
LRm = [list(map(int, input().split())) for _ in range(M)]
min_n = 0
max_n = N
for l, r in LRm:
    min_n = max(min_n, l)
    max_n = min(max_n, r)
print(max(max_n-min_n+1, 0))

Submission status: Accepted

Solution code:
N, M = list(map(int, input("").split(" ")))
permit = [list(map(int, input("").split(" "))) for i in range(M)]
l,r = 1, N
for i,j in permit:
  l = max(l, i)
  r = min(r, j)
print(max(r-l+1,0))

Submission status: Accepted

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

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

min_a = 1
max_b = N

for i in range(M):
    a,b = [int(i) for i in input().split()]
    if min_a < a:
        min_a = a
    if max_b > b:
        max_b = b

print(max(0, max_b-min_a+1))

Submission status: Accepted

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

for i in range(M):
    l, r = map(int, input().split())
    
    if i==0:
        left = l
        right = r

    else:
        if l>left:
            left = l
        if r<right:
            right = r

print(0) if left>right else print(right-left+1)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l = 1
r = n
for j in range(m):

    l_i,r_i = map(int,input().split())

    l = max(l,l_i)
    r = min (r,r_i)

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

Submission status: Accepted

Solution code:
def prison(n, m, lrli):

    rmin = 10**5
    lmax = 0

    for i in lrli:
        rmin = min(rmin, i[1])
        lmax = max(lmax, i[0])

    return rmin-lmax+1 if rmin >= lmax else 0

def main():
    n, m = map(int, input().split())
    lrli = [list(map(int, input().split()))for i in range(m)]
    print(prison(n, m, lrli))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(0 if rmin-lmax+1<=0 else rmin-lmax+1)

Submission status: Accepted

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

print(0 if rmin-lmax+1<=0 else rmin-lmax+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(r-l+1)
  
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n, m = Is()
LR = TIR(m)

l = -INF
r = INF

for l_, r_ in LR:
    l = max(l, l_)
    r = min(r, r_)

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

Submission status: Accepted

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

max_l = 1
min_r = n
for i in range(m):
  l, r = map(int, input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
  
ans = min_r - max_l + 1
if ans < 0:
  ans = 0
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(M):
    l, r = map(int, input().split())
    Llist.append(l)
    Rlist.append(r)

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

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

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

Submission status: Accepted

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

ll = list()
lr = list()
for i in range (M):
    l, r = map(int, input().split())
    ll.append(l)
    lr.append(r)

ans = min(lr) - max(ll) + 1

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

Submission status: Accepted

Solution code:
# C - Prison

N, M = map(int, input().split())
left = 0
right = 10 ** 5
for _ in range(M):
    l, r = map(int, input().split())
    left, right = max(left, l), min(right, r)

print(len([i for i in range(left, right + 1)]) if left <= right else 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)
  
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())

lmax,rmin=0,10**5
for _ in range(m):
  l,r=map(int,input().split())
  lmax=max(lmax,l)
  rmin=min(rmin,r)

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

Submission status: Accepted

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

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

lmax=max(l)
rmin=min(r)
if rmin>=lmax:
  print(rmin-lmax+1)
else:
  print(0)

Submission status: Accepted

Solution code:
import numpy as np
_, M = map(int, input().split())
L, R = np.array([tuple(map(int, input().split())) for _ in range(M)], dtype=np.int32).T
print(max(0, R.min() - L.max() + 1))

Submission status: Accepted

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


l_max = 0
r_min = float('inf')

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

if r_min >= l_max:
    ans = int(r_min-l_max+1)
else :
    ans = 0
print(ans)

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)
big=min(r)
small=max(l)
print(big-small+1 if big-small>=0 else 0)

Submission status: Accepted

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

LList = []
RList = []

for i in range(M):
    L = List[i][0]
    R = List[i][1]
    
    LList.append(L)
    RList.append(R)
    

print(max((min(RList)-max(LList))+1,0))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
hidari=-10**100
migi=10**100
keep=1
for i in range(m):
  l,r=map(int,input().split())
  if l>migi or r<hidari:
    keep=0
    break
  elif hidari<=l<=r<=migi:
    hidari=l
    migi=r
  elif l<=hidari<=migi<=r:
    pass
  elif l<=hidari<=r<=migi:
    migi=r
  elif hidari<=l<=migi<=r:
    hidari=l

if keep==1:
  res=migi-hidari+1       
  print(min(res,n))
else:
  print(0)

Submission status: Accepted

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

L = [] 
R = []
for i in range(M):
  L.append(a[i][0])
  R.append(a[i][1])

l = max(L)
r = min(R)
ans = 0
if r >= l:
  ans = r-l+1
  
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[i],R[i]=map(int,input().split())

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

if ans<=0:
    print(0)
else:
    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[i],R[i]=map(int,input().split())

l=L[0]
r=R[0]

for i in range(1,M):
    if L[i]>l:
        l=L[i]
    if R[i]<r:
        r=R[i]
    if r-l < 0:
        print(0)
        exit()
print(r+1-l)

Submission status: Accepted

Solution code:
n,m  = map(int,input().split())
lmax = 0
rmin = 10**10
for i in range(m):
         l,r = map(int,input().split())
         if l > lmax:
                  lmax = l
         if r < rmin:
                  rmin = r
if lmax > rmin:
         print(0)
else:
         print(rmin - lmax +1)

Submission status: Accepted

Solution code:
#C
n, m = map(int, input().split())
l = []
r = []
for _ in range(m):
    tmp=list(map(int,input().split()))
    l.append(tmp[0])
    r.append(tmp[1])
lmax = max(l)
rmin = min(r)
# print(lmax,rmin)
if rmin >= lmax:
    print(rmin -lmax + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
n,m = map(int,input().split())
ls = [list(map(int,input().split())) for i in range(m)]
a = ls[0][0]
b = ls[0][1]
for j in range(1,m):
    a = max(a,ls[j][0])
    b = min(b,ls[j][1])
    if b-a<0:
        print(0)
        sys.exit()
print(b-a+1)

Submission status: Accepted

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

Submission status: Accepted

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

l = 0
r = (10**5)+1

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

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

Submission status: Accepted

Solution code:
def main():
    N, M = list(map(int, input().split(' ')))
    max_l, min_r = 1, N
    for _ in range(M):
        L, R = list(map(int, input().split(' ')))
        max_l = max([max_l, L])
        min_r = min([min_r, R])
    print(max([min_r - max_l + 1, 0]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

maxl = 0
minr = 10**5
for i in range(m):
  l, r = map(int, input().split())
  maxl = max(maxl, l)
  minr = min(minr, r)

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
max = float("INF")
min = 0
for i in range(m):
    l, r = map(int, input().split())
    if max > r:
        max = r
    if min < l:
        min = l
if max - min + 1 >= 0:
    print(max - min + 1)
else:
    print(0)