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())
L1, R1 = map(int, input().split())
min = L1
max = R1

for i in range(1, M):
    newL, newM = map(int, input().split())
    if newL > min:
        min = newL
    if newM < max:
        max = newM
result = 0
if max - min + 1 > 0:
    result = max-min+1
print("{}".format(result))

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)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
[L,R]=[1,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=[]
for i in range(m):
    L=list(map(int,input().split()))
    l.append(L)
L=1
R=n
for i in range(m):
    if L<l[i][0]:
        L=l[i][0]
    if R>l[i][1]:
        R=l[i][1]
if L>R:
    print(0)
else:
    print(R-L+1)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
ll=0
rr=10**6
for i in range(m):
    l,r=map(int,input().split())
    if ll<l:
        ll=l
    if rr>r:
        rr=r
if rr-ll>=0:
    print(rr-ll+1)
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 Rmin < Lmax:
    print(0)
    exit()
print(Rmin-Lmax+1)

Submission status: Accepted

Solution code:
inp = input()
st = inp.split()
n = int(st[0])
m = int(st[1])

max_l = 0
min_r = n+10
for _ in range(m):
    i_inp = input()
    st_ino = i_inp.split()
    l = int(st_ino[0])
    r = int(st_ino[1])
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r
ans = min_r - max_l + 1
if ans < 0:
    print(0)
else:
    print(ans)

Submission status: Accepted

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

print(max(min(r)-max(l)+1,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())
l = 1
r = N
for i in range(M):
        if l < L[i]:
                l = L[i]
        if r > R[i]:
                r = R[i]
        if l > r:
                break
print(r-l+1 if l <= r else 0)

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(l, maxL)
    minR = min(r, minR)
if(maxL > minR):
    print(0)
else:
    print(minR - maxL + 1)

Submission status: Accepted

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

maxvL = 0
minvR = 1000000
for i in range(M):
    L, R = list(map(int, input().split()))
    if maxvL < L:
        maxvL = L
    if minvR > R:
        minvR = R

count = 0
for i in range(N):
    if maxvL <= i + 1 <= minvR:
        count += 1
print(count)

Submission status: Accepted

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

l_list = [None] * M
r_list = [None] * M

allows = [0] * (N + 1)
for i in range(M):
    l, r = map(int, input().split())

    allows[l - 1] += 1
    allows[r] -= 1

total = 0
count = 0
for n in allows:
    total += n
    if total == M:
        count += 1
print(count)

Submission status: Accepted

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

l, r = 0, float("inf")
for lr in lrList:
    l, r = max(lr[0], l), min(lr[1], r)

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

Submission status: Accepted

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

right = N
left = 0

for i in e:
    if left < i[0]:
        left = i[0]
    if right > i[1]:
        right = i[1]
if right >= left:
    print(right - left + 1) 
else:
    print("0")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = [1, n]
for _ in range(m):
  a, b = map(int, input().split())
  if a <= l[1] and b >= l[0]:
    if a > l[0]:
      l[0] = a
    if b < l[1]:
      l[1] = b
  else:
    l = [1, 0]
    break

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

res = min_r - max_l + 1

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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    li_max = 0
    ri_min = 10 ** 5
    for i in range(M):
        li, ri = map(int, input().split())
        if li > li_max:
            li_max = li
        if ri < ri_min:
            ri_min = ri
    ans = ri_min - li_max + 1
    if ans < 0:
        ans = 0
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split(" "))
keylist = [1, N]

for i in range(M):
    l, r = map(int, input().split(" "))
    if keylist[0] < l:
        keylist[0] = l
    if keylist[1] > r:
        keylist[1] = r

ans = keylist[1] - keylist[0] + 1

if ans < 1:
    print(0)
else:
    print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mi = None
ma = None
fail = False
for i in range(m):
    l, r = map(int, input().split())
    if mi is None:
        mi = r
    if ma is None:
        ma = l
    ma = max(l, ma)
    mi = min(r, mi)
    if mi < l:
        fail = True
    if r < ma:
        fail = True
if fail:
    print("0")
else:
    print(str(int(mi - ma) + 1))

Submission status: Accepted

Solution code:
def solve(n, m, list1):
    left = 0
    right = n
    for i in range(len(list1)):
        left = max(list1[i][0], left)
        right = min(list1[i][1], right)
    return max(right-left+1, 0)

n, m = map(int, input().split())
list1 = []
for _ in range(m):
    list1 += [list(map(int, input().split()))]
print(solve(n, m, list1))

Submission status: Accepted

Solution code:
def solve():
    N, M = [int(_) for _ in raw_input().split()]
    maxL = 0
    minR = N+1
    for i in range(M):
        Li, Ri = [int(_) for _ in raw_input().split()]
        if Li > maxL:
            maxL = Li
        if Ri < minR:
            minR = Ri
    if minR >= maxL:
        ret = minR - maxL + 1
    else:
        ret = 0
    print ret

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

tmpL = 0
tmpR = float("inf")
for i in range(M):
  L, R = map(int, input().split())
  tmpL = max(tmpL, L)
  tmpR = min(tmpR, R)

if tmpR - tmpL < 0:
  print(0)
else:
  print(tmpR - tmpL + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ml = [list(map(int,input().split())) for i in range(m)]
l_l = []
r_l = []
for i in range(m):
    l_l.append(ml[i][0])
    r_l.append(ml[i][1])
if min(r_l) < max(l_l):
    print(0)
elif n<max(l_l):
    print(0)
elif max(l_l)<=n<=min(r_l):
    print(n-max(l_l)+1)
else:
    print(min(r_l)-max(l_l)+1)

Submission status: Accepted

Solution code:
# coding:utf-8

import math
import re

def main(params):
    n,m = params[0]
    mi = 0
    mx = 0
    for g in params[1:]:
        f = int(g.split()[0])
        s = int(g.split()[1])
        if mi == 0 or mi < f:
            mi = f
        if mx == 0 or mx > s:
            mx = s
    if mi <= mx:
        l = 0
        for i in range(mi,mx+1):
            l += 1
        print l
    else:
        print 0
#    a = params[1].split()
#    b = list(params[2])

if __name__ == "__main__":
    n,m = raw_input().split()
    n = int(n)
    m = int(m)
    cnt = m # TODO: edit here
    params = [[n,m]]
    for i in range(cnt):
        param = raw_input()
        params.append(param)
    main(params)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)
print(max(0,min_r-max_l+1))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, m = map(int, input().split())
li = [0] * (n + 1)
for _ in range(m):
    l, r = map(lambda x: int(x) - 1, input().split())
    li[0] += 1
    li[l] -= 1
    li[r + 1] += 1
    li[n] -= 1
for i in range(n):
    li[i + 1] += li[i]
print(sum(x <= 0 for x in li[:-1]))

Submission status: Accepted

Solution code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#n=int(input())
#li=input().split()
#import fractions
#n=int(input())
#S=input()
#import math

lis=list(map(int,input().split()))
lr=[1,lis[0]]
for i in range(lis[1]):
    lri=list(map(int,input().split()))
    if lr[0]<lri[0]:
        lr[0]=lri[0]
    if lr[1]>lri[1]:
        lr[1]=lri[1]

ans=lr[1]-lr[0]+1
if ans>=1:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys

def intinput():
    return map(int,sys.stdin.readline().split())

n,m=intinput()
a1=0
b1=n+1
for i in range(m):
    a,b=intinput()
    a1 = max(a,a1)
    b1 = min(b,b1)
if b1>=a1:
    print(b1-a1+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

max_l = 0
min_r = 1e06

for _ in range(M):
	l, r = sorted([int(x) for x in 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 = list(map(int, input().split()))

num = [0] * N

L = 1
R = N

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

id = [0,N]

for _ in range(M):
    L, R = map(int,input().split())
    if id[0]<L:
        id[0] = L
    if id[1]>R:
        id[1] = R
    if id[0]>id[1]:
        print(0)
        exit()

print(id[1]-id[0]+1)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
ans_l = 0
ans_r = n
for i in range(k):
    l,r = map(int,input().split())
    if l >= ans_l:
        ans_l = l
    if r <= ans_r:
        ans_r = r

if ans_r-ans_l+1 >= 0:
    print(ans_r-ans_l+1)
else:
    print("0")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
haj=0
owa=n
for i in range(m):
  a,b=map(int,input().split())
  if(a>haj):
    haj=a
  if(b<owa):
    owa=b
if(owa-haj>=0):    
  print(owa-haj+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())
    
L_max = max(L[k] for k in range(M))
R_min = min(N,min(R[k] for k in range(M)))

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

Submission status: Accepted

Solution code:
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
print(max(min_R - max_L +1,0))

Submission status: Accepted

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

minN = 1
maxN = N
for i in range(M):
    L, R = tuple([int(x) for x in input().split(' ')])
    minN = max(L, minN)
    maxN = min(R, maxN)
print(max(maxN-minN+1,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())

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

Submission status: Accepted

Solution code:
n, m = [int(c) for c in input().split()]
lr = [[int(c) for c in input().split()] for _ in range(m)]
ls, rs = zip(*lr)
if min(rs) < max(ls):
    print(0)
else:
    print(min(rs) - max(ls) + 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    res = min(R) - max(L) + 1
    if res < 0:
        res = 0
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]" 
    R = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

min = 0
max = N

for i in range(M):
    if min <= A[i][0]:
        min = A[i][0]
    if max >= A[i][1]:
        max = A[i][1]
        
if max - min + 1 > 0:
    print(max - min + 1)
else:
    print(0)

Submission status: Accepted

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

minmax = 0
maxmin = 100000

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

    if r < maxmin:
        maxmin = r

if maxmin - minmax + 1 > 0:
    print(maxmin - minmax + 1)
else:
    print("0")

Submission status: Accepted

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

L = []
R = []
for i in range(m):
    l, r = tuple(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())

for i in range(m):
    l, r=map(int, input().split())
    if i==0:
        l_big=l
        r_min=r
    else:
        if l_big<l:
            l_big=l
        if r_min>r:
            r_min=r
if r_min-l_big+1>=0:
    print(r_min-l_big+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())
print(max(min(r) -max(l) + 1,0))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l_min=0
r_min=n+1
for i in range(m):
    l_b,r_b=map(int,input().split())
    if l_min<l_b:
        l_min=l_b
    if r_min>r_b:
        r_min=r_b
    #print(l_min,r_min)
k=r_min-l_min+1
print(k if k>0 else 0)

Submission status: Accepted

Solution code:
def main():
    n, m = list(map(int, input().split()))
    lr = []
    for _ in range(m):
        l, r = list(map(int, input().split()))
        lr.append((l, r))

    lmax, rmin = lr[0]
    for l, r in lr:
        if l > lmax:
            lmax = l
        if r < rmin:
            rmin = r

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


main()

Submission status: Accepted

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

LR = []
max_L = 0
min_R = N
for i in range(M):
    LR = list(map(int, input().split()))
    if max_L < LR[0]:
        max_L = LR[0]
    if min_R > LR[1]:
        min_R = LR[1]

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())
L, R = [0] * M, [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())

MIN,MAX=-1,10**6
for m in range(M):
	L,R=map(int,input().split())
	if MIN < L:
		MIN = L
	if R < MAX:
		MAX = R

if MAX < MIN:
	ans = 0
else:
	ans = MAX - MIN + 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)

print(max(min(R)-max(L)+1,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())
mi=1
ma=n
for i in range(m):
    if l[i]>mi:
        mi=l[i]
    if ma>r[i]:
        ma=r[i]
if ma>=mi:
    ans=ma-mi+1
else:
    ans=0
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for i in range(M)]
imos = [0 for i in range(N+2)]
ans = 0
for lr in LR:
    imos[lr[0]] += 1
    imos[lr[1]+1] -= 1

for i in range(1, N+2):
    imos[i] += imos[i-1]
    ans += 1 if (imos[i]==M) else 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Lmax = -1
Rmin = N + 1

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
mi = 0
ma = 1000001
for i in range(M):
    right, left = map(int, input().split())
    if right > mi:
        mi = right
    if left < ma:
        ma = left
if mi > ma:
    print(0)
else:
    print(ma - mi + 1)

Submission status: Accepted

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

L = []
for i in range(m):
    l, r = map(int, input().split())
    L.append((l, r))
    
ans_min = 0
ans_max = 10 ** 7
for i in L:
    ans_min = max(i[0], ans_min)
    ans_max = min(i[1], ans_max)

ans = min(ans_max-ans_min+1, n)
print(max(ans, 0))

Submission status: Accepted

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

ran = [0 for i in range(N+2)]
for l, r in items:
    ran[l-1] += 1
    ran[r] -= 1


for i in range(N+1):
    ran[i+1] += ran[i]

target = set()
for i, r in enumerate(ran):
    if r == M:
        target.add(i+1)
print(len(target))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal
input = sys.stdin.readline

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

lst_1 = []
lst_2 = []

for _ in range(M):
    a,b = map(int, input().split())
    lst_1.append(a)
    lst_2.append(b)

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
maxL = 0
minR = 10**6
for i in range(M):
    L,R = map(int,input().split())
    if (maxL < L) :
        maxL = L
    if (minR > R) :
        minR = R

ans = minR - maxL + 1

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

Submission status: Accepted

Solution code:
[N,M] = list(map(int, input().split()))
#[N,M] = [, 2]
lp = 0
up = N
for i in range(M):
    [x,y] = list(map(int, input().split()))
    lp = max(lp,x)
    up = min(up,y)
if lp > up:
    print(0)
else:
    print(up-lp+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())
  if left < l:
    left = l
  if right > r:
    right = r
if right >= left:
  print(right - left + 1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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


left = -1
right = 10**9

for i in range(m):
    left = max(left, l[i])
    right = min(right, r[i])
    if left>right:
        print(0)
        exit()

print(right - left + 1)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    [n, m] = list(map(int, input().split()))
    lr = [list(map(int, input().split())) for _ in range(m)]
    max_l = 1
    min_r = n
    for l, r in lr:
        if max_l < l:
            max_l = l
        if min_r > r:
            min_r = r
    print(max(min_r - max_l + 1, 0))

Submission status: Accepted

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
def ddprint(x):
  if DBG:
    print(x)

n,m = inm()
lmax = -1
rmin = n+1
for i in range(m):
    l,r = inm()
    lmax = max(lmax, l)
    rmin = min(rmin, r)
print((rmin-lmax+1) if rmin >= lmax else 0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
left=0
right=1000000
for i in range(m):
    l,r=map(int,input().split())
    if left<l:
        left=l

    if right>r:
        right=r

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lr_list = [list(map(int, input().split())) for _ in range(m)]
max_val, min_val = n, 1
for l, r in lr_list:
    if l > min_val:
        min_val = l
    if r < max_val:
        max_val = r
ans = max_val - min_val + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
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 SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

N, M = LI()
L, R = [], []
for i in range(M):
    x, y = LI()
    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 = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(m)]

l_max = 0
r_min = n
for l, r in lst:
    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())
ans=0
ml=0
mr=n
for i in range(1,m+1):
    l,r=map(int,input().split())
    ml=max(ml,l)
    mr=min(mr,r)
print(max(0,mr-ml+1))

Submission status: Accepted

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

l = [0]*m
r = [0]*m

mi = 1
ma = n + 1

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

ans = ma - mi + 1

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

Submission status: Accepted

Solution code:
import sys
range = xrange
input = raw_input

n,m = [int(x) for x in input().split()]

lmax = 0
rmin = 10**6
for _ in range(m):
    l,r = [int(x) for x in input().split()]

    lmax = max(l,lmax)
    rmin = min(r,rmin)

print max(rmin-lmax+1,0)

Submission status: Accepted

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

l = [ran[i][0] for i in range(m)]
r = [ran[i][1] for i in range(m)]
x = max(l)
y = min(r)

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

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)

min_r = min(R)
max_l = max(L)
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())
max_L = -1
min_R = 10**6
for i in range(M):
    L, R = map(int, input().split())
    max_L = max(L, max_L)
    min_R = min(R, min_R)

print(max(min_R - max_L + 1, 0))

Submission status: Accepted

Solution code:
n, m = [int(i) for i in 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 (z+1)
else:
  print (0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
listC =[]
while True:
    try:
        listC.append(list(map(int,input().split())))
    except:
        break;
N = listC[0][0]
M = listC[0][1]
L = []
R = []
for i in range(1,len(listC)):
    L.append(listC[i][0])
    R.append(listC[i][1])

maxL = max(L)
minR = min(R)
if maxL <= minR:
    output = minR-maxL+1
else:
    output = 0
print(output)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Min = 0
Max = N
for m in range(M):
    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:
N, M = map(int, input().split())

ids = [0 for i in range(N + 2)]
for i in range(M):
  L, R = map(int, input().split())
  ids[L] += 1
  ids[R+1] -= 1

prev = 0
cnt = 0 
for i in ids:
  prev = i + prev
  if prev == M:
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

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

maxL = 0
minR = 10 ** 5

for _ in range(M):
    L, R = [int(x) for x in input().split()]
    maxL = max(maxL, L)
    minR = min(minR, R)

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

Submission status: Accepted

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

ids = [tuple(map(int, input().split())) for _ in range(M)]

Lmax = max([L for L, _ in ids])
Rmin = min([R for _, R in ids])

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
a = 10**6
b = 0
for i in range(M):
    L, R = map(int, input().split())
    b = L if L>b else b
    a = R if R<a else a
if a-b+1>=0:
    print(a-b+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())

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 = 0
R = N
for m in range(M):
    L_new,R_new = map(int,input().split())
    if L_new > L:
        L = L_new
    if R_new < R:
        R = R_new

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