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())
info = []
for _ in range(m):
    l, r = map(int, input().split())
    info.append([l, r])
# print(info)
L = 0
R = 10**5 + 1
for l, r in info:
    # Lb, Rb = True, True
    if l < L:
        pass
    elif L <= l <= R:
        L = l
    else:
        print(0)
        exit()
    if R < r:
        pass
    elif L <= r <= R:
        R = r
    else:
        print(0)
        exit()
    # if not (Lb or Rb):
    #     print(0)
    #     exit()
    # print(L, R)

print(R - L + 1)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
LR=[list(map(int,input().split())) for _ in range(m)]
imos=[0]*(n+1)
for i,j in LR:
    imos[i-1]+=1
    imos[j]-=1

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

print(imos.count(m))

Submission status: Accepted

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

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

Submission status: Accepted

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

LL, RR = L[0], R[0]
is_zero = False
for i in range(1, M):
    if RR < L[i]:
        is_zero = True
        break
    elif LL <= L[i] <= RR:
        LL = max(LL, L[i])
        RR = min(RR, R[i])
    elif L[i] < LL <= R[i]:
        RR = min(RR, R[i])
    else:
        is_zero = True
        break

if is_zero:
    print(0)
else:
    print(RR - LL + 1)

Submission status: Accepted

Solution code:
n, m, *ls = map(int, open(0).read().split())
l = max(ls[0::2])
r = min(ls[1::2])
print(max(0, r-l+1))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lmin = 0
lmax = 1e5+10
for _ in range(m):
  l, r = map(int, input().split())
  lmin = max(lmin, l)
  lmax = min(lmax, r)
print(max(0, lmax-lmin+1))

Submission status: Accepted

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

ans_l = 0
ans_r = n
for _ in range(m):
    l, r = map(int, input().split())
    ans_l = max(ans_l, l)
    ans_r = min(ans_r, r)
print(max(0,ans_r-ans_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)
    R.append(r)
a=max(L)
b=min(R)
li=[i for i in range(a,b+1)]
print(len(li))

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)
count = 0
if min(R)-max(L)+1>=0:
    print(min(R)-max(L)+1)
else:
    print(0)

Submission status: Accepted

Solution code:
L = []
R = []
N, M = map(int, input().split())
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>=1:
    print(ans)
else:
    print(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)

left = max(L)
right = min(R)

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
c=[]
t=[]
for _ in range(M):
  c_i,t_i=map(int,input().split())
  c.append(c_i)
  t.append(t_i)
x=max(c)
y=min(t)
z=y-x+1
if z>=1:
  print(z)
else:
  print(0)

Submission status: Accepted

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

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

count = 0
small = max(l_ls)
big = min(r_ls)

print(max(big-small+1,0))

Submission status: Accepted

Solution code:
#import math
#import bisect
#import numpy as np
#import itertools
#import copy
import sys

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n, m = list(map(int,ipti().split()))
    l = [0] * m
    r = [0] * m

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

    lower = -INF
    upper = INF

    for i in range(m):
        lower = max(lower, l[i])
        upper = min(upper, r[i])

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

res = min(r) - max(l) + 1 if min(r) >= max(l) else 0
print(res)

Submission status: Accepted

Solution code:
import numpy as np
import copy

n,m = map(int,input().split())
max_l = 0
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)

if max_l <= min_r:
    print(min_r - max_l + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
lr_list = []

for i in range(m):
    lr_list.append(list(map(int, input().split())))

l = 1
r = n

for item in lr_list:
    l = max(l, item[0])
    r = min(r, item[1])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  _, _, *lst = map(int, open(0).read().split())
  l, r = max(lst[::2]), min(lst[1::2])
  print(0 if l > r else r - l + 1)

main()

Submission status: Accepted

Solution code:
_, _, *lst = map(int, open(0).read().split())
l, r = max(lst[::2]), min(lst[1::2])
print(0 if l > r else r - l + 1)

Submission status: Accepted

Solution code:
import sys
_, _, *lst = map(int, sys.stdin.read().split())
l, r = max(lst[::2]), min(lst[1::2])
print(0 if l > r else r - l + 1)

Submission status: Accepted

Solution code:
import sys
n, m, *lst = map(int, sys.stdin.read().split())
l = max(lst[::2])
r = min(lst[1::2])
if l > r:
  print(0)
else:
  print(r - l + 1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
left, right = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if l > left:
    left = l
  if r < right:
    right = r
  if left > right:
    print(0)
    quit()
print(right - left + 1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n, m = map(int, input().strip().split())
  ls, rs = [], []
  for _ in range(m):
    l, r = map(int, input().strip().split())
    ls.append(l)
    rs.append(r)
  l = max(ls)
  r = min(rs)
  print(max(r - l + 1, 0))

if __name__=='__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
lr=[list(map(int,input().split())) for _ in range(m)]
a=sorted(lr,key=lambda x:x[1])[0][1]-sorted(lr,key=lambda x:x[0])[-1][0]+1
print(max(a,0))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
import heapq
L=[]
R=[]
for i in range(M):
  l,r=map(int,input().split())
  heapq.heappush(L, -l)
  heapq.heappush(R, r)
l=heapq.heappop(L)
r=heapq.heappop(R)
if -l>r:
  print(0)
else:
  print(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 = [int(v) - 1 for v in input().split()]
        cards[l] += 1
        cards[r+1] -= 1

    count = 0
    if cards[0] == M:
        count += 1

    for i in range(1, N+1):
        cards[i] += cards[i - 1]
        if cards[i] == M:
            count += 1

    print(count)


main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L, R = 1, N
for _ in range(M):
  left, right = map(int, input().split())
  if L < left:
    L = left
  if right < R:
    R = right

ans = max(R - L + 1, 0)
print(ans)

Submission status: Accepted

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

min_ = 1
max_ = 10**5
for i in range(m):
    l,r = map(int, input().split())
    min_ = max(min_, l)
    max_ = min(max_, r)

ans = max(0, max_ - min_ + 1)
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(M)]
mi=0
ma=float("inf")
for i in range(M):
    if G[i][0]>mi:
        mi=G[i][0]
    if G[i][1]<ma:
        ma=G[i][1]
    #print(mi,ma)
print(max(ma-mi+1,0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

LR = np.ones([M,2])

for i in range(M):
    LR[i][0],LR[i][1] = [int(i) for i in input().split()]
    
max_L = max(LR[:,0])
min_R = min(LR[:,1])
if max_L <= min_R:
    ans = int(min_R - max_L+1)
else:
    ans =0
    
print(ans)

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)

c = 0
lm = max(llist)
rm = min(rlist)
for i in range(1, n + 1):
    if lm <= i <= rm:
        c += 1

print(c)

Submission status: Accepted

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

L_max = 0
R_min = 10 ** 5 + 1
for L, R in LR:
    L_max = max(L_max, L)
    R_min = min(R_min, R)

ans = R_min - L_max + 1
if ans < 0:
    print(0)
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l_max = 1
r_min = N + 1
for _ in range(M):
    L, R = map(int, input().split())
    if l_max < L:
        l_max = L
    if r_min > R:
        r_min = R
if r_min + 1 > l_max:
    print(r_min - l_max + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(n):
    cards[i+1] += cards[i]
print(cards.count(m))

Submission status: Accepted

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

print(b-a+1 if a <= b else 0)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
k_1 = []
k_2 = []
for i in range(M):
  l,r = map(int,input().split())
  k_1.append(l)
  k_2.append(r)
if max(k_1)<=min(k_2):
  print(min(k_2)-max(k_1)+1)
else:
  print(0)

Submission status: Accepted

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

lm = 0
rm = 10**5+1
for _ in range(M):
    l, r = map(int, input().split())
    lm = max(lm, l)
    rm = min(rm, r)

ans = rm - lm + 1
print(ans if ans >= 0 else 0)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, m = map(int, input().split())
    max = 0
    min = n

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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=list(map(int, input().split()))
l = 0
r = float("inf")
for _ in range(m):
    a,b = list(map(int, input().split()))
    l = max(l,a)
    r = min(r,b)

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
left = 0
right = 1000000000
for _ in range(m):
    a, b = (int(x) for x in input().split())
    left = max(left, a)
    right = min(right, b)
if right-left+1 < 0:
    print(0)
else:
    print(right-left+1)

Submission status: Accepted

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

Submission status: Accepted

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

if m == 1:
  l, r = map(int, input().split())
  print(r-l+1)
else:
  ls, rs = [], []
  for _ in range(m):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)
  ans = min(rs) - max(ls) + 1
  if ans > 0:
    print(ans)
  else:
    print(0)

Submission status: Accepted

Solution code:
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
A = [None]*M
B = [None]*M
for i in range(M):
    A[i], B[i] = map(int, readline().split())

ans = min(B) - max(A)

print(ans + 1 if ans >= 0 else 0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    N,M = map(int,input().split())
    L = []
    R = []
    for _ in range(M):
        l,r = map(int,input().split())
        L.append(l)
        R.append(r)
    L.sort(reverse=True)
    R.sort()
    lmax = L[0]
    rmin = R[0]
    print(max(0,rmin-lmax+1))



if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(m):
    l, r = map(int, input().split())
    li.append(l)
    ri.append(r)

ans = max(0,min(ri)-max(li)+1)
print(ans)

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

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
flag = True
for i in range(m):
    l, r = map(int, input().split())
    if i == 0: 
        l1 = l
        r1 = r
    else:
        if r < l1 or r1 < l:
            flag = False
        elif l1 < l:
            l1 = l
        elif r1 > r:
            r1 = r
if flag: print(r1-l1+1)
else: print(0)

Submission status: Accepted

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

high=N
low=0

ans = [True for i in range(N)]
for i in range(M):
    for c,k in enumerate(range(L[i]-2, low-1, -1)):
        if(low < k):
            low=k
        ans[k]=False


    for c,j in enumerate(range(R[i],high)):
        if(high > j):
            high=j
        ans[j]=False


print(ans.count(True))

Submission status: Accepted

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

min_number = 1
max_number = n
for i in range(m):
    low = lr_lst[i][0]
    upper = lr_lst[i][1]

    min_number = max(min_number, low)
    max_number = min(max_number, upper)

answer = max_number - min_number + 1

if answer < 0:
    answer = 0

print(answer)

Submission status: Accepted

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)
L=max(L)
R=min(R)
print(0 if L>R else R-L+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
LR=[list(map(int,input().split())) for i in range(M)]
l=1
r=N
for lr in LR:
    if l<lr[0]:
        l=lr[0]
    if lr[1]<r:
        r=lr[1]
print(max(0,r-l+1))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,m=mii()
left=0
right=10**100

for i in range(m):
    x,y=mii()
    left=max(left,x)
    right=min(right,y)

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

Submission status: Accepted

Solution code:
def main():
  IN=list(map(int,open(0).read().split()[2:]));L,R=max(IN[::2]),min(IN[1::2]);print(R-L+1 if R>=L else 0)
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  IN= open(0).read().split()[2:]
  left,right=-1*float("inf"),float("inf")
  
  for L,R in zip(IN[::2],IN[1::2]):
    L,R=int(L),int(R)
    if right<L or R<left:
      print(0)
      break
    else:
      tmp=sorted([left,right,L,R])
      left,right=tmp[1],tmp[2]
  else:
    print(right-left+1)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  import sys
 
  n,m=map(int,input().split())
  LR=tuple([list(map(int,e.split())) for e in sys.stdin])
  left,right=-1*float("inf"),float("inf")
  
  for L,R in LR:
    if right<L or R<left:
      print(0)
      break
    else:
      tmp=sorted([left,right,L,R])
      left,right=tmp[1],tmp[2]
  else:
    print(right-left+1)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  import sys

  n,m=map(int,input().split())
  LR=[list(map(int,e.split())) for e in sys.stdin]
  left,right=-1*float("inf"),float("inf")
  
  for L,R in LR:
    if right<L or R<left:
      print(0)
      break
    else:
      tmp=sorted([left,right,L,R])
      left,right=tmp[1],tmp[2]
  else:
    print(right-left+1)
  
if __name__=="__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
mat = [list(map(int,input().split())) for o in range(M)]
lmax = mat[0][0]
for i in range(M):
    if mat[i][0] > lmax:
        lmax = mat[i][0]
rmin = mat[0][1]
for i in range(M):
    if mat[i][1] < rmin:
        rmin = mat[i][1]

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

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
L, R = zip(*[tuple(map(int, input().split())) for _ in range(M)])
print(max(0, min(R) - max(L) + 1))

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate as ac
N, M = map(int, input().split())
o = [0]*(N+2)

for _ in range(M):
    l, r = map(int, input().split())
    o[l] += 1
    o[r+1] -= 1

print(list(ac(o)).count(M))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#不十分だった
n, m = map(int, input().split())
l_max, r_min = 0, 10**5 + 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())
L,R=0,n
for _ in range(m):
    l,r = map(int,input().split())
    L,R = max(L,l),min(R,r)
print(max(R-L+1,0))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lst = [k+1 for k in range(n)]
lmax = 0
rmin = 10**5
for i in range(m):
    lx,rx = map(int,input().split())
    if lx >= lmax:
        lmax = lx
    if rx <= rmin:
        rmin = rx
res = rmin - lmax + 1
if res > 0:
    print(res)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
l_lis = []
r_lis = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    l_lis.append(tmp[0])
    r_lis.append(tmp[1])
ll = max(l_lis)
rr = min(r_lis)
count = rr - ll + 1
if count <= 0:
    print(0)
else:
    print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if max_ID < min_ID:
    print(0)
else:
    print(max_ID - min_ID + 1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lrl = list(list(map(int, input().split())) for _ in range(m))
lmax = 0
rmin = n

for lr in lrl:
    if lr[0] > lmax:
        lmax = lr[0]
    if lr[1] < rmin:
        rmin = lr[1]
if lmax <= rmin:
    print(rmin-lmax+1)
else:
    print(0)

Submission status: Accepted

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

    card_list = [0] * n

    l_list = []
    r_list = []

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

    max_l = max(l_list)
    min_r = min(r_list)

    print(min_r-max_l+1 if min_r >= max_l else 0)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

l = -float("inf")
r = float("inf")

for i in range(M):
    l_i, r_i = map(int, input().split())
    if l_i > l:
        l = l_i
    if r_i < r:
        r = r_i

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

Submission status: Accepted

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)
diff = min(R) - max(L) + 1
if diff > 0:
    print(diff)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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:
    print(0)
else:
    print(R - L + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
imos = [0]*(N+1)
for _ in range(M):
    # ゲートの情報が入ってくる
    l, r = map(lambda x: int(x)-1, input().split())
    imos[l] += 1
    imos[r+1] -= 1  # rまでは有効であり、r+1以降が無向になる仕様であるのでr+1をデクリメントする

cumulative = [imos[0]]
for value in imos[1:]:
    cumulative.append(cumulative[-1]+value)
ans = 0
for c in cumulative:
    if c == M:
        ans += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# 30
# lack of understanding of Q !

n,m  = map(int, input().split())
L = 0
R = 500000
for i in range(m):
    l,r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)
    l = 0
    r = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k,m=map(int,input().split())
l=[0 for i in range(k+1)]
for i in range(m):
  a,s=map(int,input().split())
  l[a-1]+=1;l[s]-=1
ans=0
f=0
for j in range(k):
  l[j]+=f
  f=l[j]
print(len([1 for i in l if i==m]))

Submission status: Accepted

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

l,r=0,N
for lr in LR:
    x,y=lr[0],lr[1]
    l=max(l,x)
    r=min(r,y)

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

Submission status: Accepted

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

ans_left,ans_right=LR[0][0],LR[0][1]

for lr in LR:
    left,right=lr[0],lr[1]
    if ans_left<left:
        ans_left=left
    if ans_right>right:
        ans_right=right

ans=ans_right-ans_left+1
if ans<=0:
    ans=0
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
LR = [[int(x) for x in input().split()] for _ in range(M)]
L = []
R = []
for i in range(M):
    L.append(LR[i][0])
    R.append(LR[i][1])
maxL = max(L)
minR = min(R)
if minR - maxL < 0:
    print(0)
else:
    print(minR-maxL+1)

Submission status: Accepted

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

min = cards[0][0]
max = cards[0][1]

for i in range(m):
    if cards[i][0] > min:
        min = cards[i][0]
    if cards[i][1] < max:
        max = cards[i][1]

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

Submission status: Accepted

Solution code:
import sys
n,m=(int(i) for i in input().split())
l = [None]*m
r = [None]*m

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

rangel=l[0]
ranger=r[0]

for i in range(1,m):
    if rangel < l[i]:
        rangel = l[i]

    if ranger > r[i]:
        ranger = r[i]

    if rangel > ranger:
        ans =0
        print(ans)
        sys.exit()

ans = ranger - rangel +1
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)
if max(L)>min(R):
    print(0)
else:
    print(min(R)-max(L)+1)

Submission status: Accepted

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

left=1
right=pow(10,5)

for i in range(M):
    L,R=map(int,input().split())
    left=max(left, L)
    right=min(right,R)
ans=right-left+1
print(0 if left>right else ans)