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:
p = input().split()
n,m = int(p[0]), int(p[1])
mi,ma = 1,n
for i in range(m):
    p = input().split()
    l,r = int(p[0]), int(p[1])
    if l > mi:
        mi = l
    if r < ma:
        ma = r
print(max([ma-mi+1,0]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M =list(map(int, input().split()))
LR_list = [list(map(int, input().split())) for _ in range(M)]
get_L = lambda lst: lst[0]
get_R = lambda lst: lst[1]
Lmax = max(map(get_L, LR_list))
Rmin = min(map(get_R, LR_list))
if (Rmin < Lmax):
    print(0)
else:
    print(Rmin - Lmax +1)

Submission status: Accepted

Solution code:

N ,M = map(lambda x: int(x),input().split())

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
max_l = 0
min_r = 10e6
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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, LR):
    l = 1
    r = N
    for L, R in LR:
        l = max(l, L)
        r = min(r, R)
    return max((r - l + 1), 0)


def main():
    # N = read_int()
    N, M = read_int_n()
    LR = [read_int_n() for _ in range(M)]
    print(slv(N, M, LR))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M=map(int ,raw_input().split())
S = 1
E = N
for i in range(M):
    L,R=map(int ,raw_input().split())
    if S<L:
        S=L
    if E>R:
        E=R
if E<S:
    print(str(0))
else:
    print(str(E-S+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())
    L = max(l,L)
    R = min(r,R)

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

Submission status: Accepted

Solution code:

from math import *

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

l=[0]*n
r=[0]*n

for i in range(m):
	s=input().split(" ")
	l[int(s[0])-1]+=1
	r[int(s[1])-1]+=1



i=0

c=0
tmp=0

while(i<n):
	tmp+=l[i]
	if(tmp>=m):
		c+=1
	tmp-=r[i]
	i+=1

print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May 25 20:54:19 2019

@author: Yamazaki Kenichi
"""

N,M = map(int,input().split())
lr = [list(map(int,input().split())) for i in range(M)]

left, right = 1, N
for c in lr:
    left = max(left,c[0])
    right = min(right,c[1])
ans = max(right - left + 1, 0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=1
R=n
for _ 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:

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    lnum = 0
    rnum = 100000
    for i in range(m):
        l,r = list(map(int, input().split()))
        lnum = max(lnum, l)
        rnum = min(rnum, r)
    
    result = rnum - lnum + 1
    if(result < 0):
        print(0)
    else:
        print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, raw_input().split())
l, r = 0, 10**6
for i in range(m):
	x, y = map(int, raw_input().split())
	l = max(x, l)
	r = min(r, y)
print max(0, r - l + 1)

Submission status: Accepted

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

Submission status: Accepted

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

lefts = list()
rights = list()
for i in range(m):
    left, right = map(int, input().split())
    lefts.append(left)
    rights.append(right)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[0 for i in range(n+1)]
for i in range(m):
  a,b=map(int,input().split())
  l[a-1]+=1
  l[b]-=1
s=0
c=0
for i in l:
  s+=i
  if s==m:
    c+=1
print(c)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
lr = []
left,right = map(int,input().split())
for i in range(1,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=[]
R=[]
for i in range(M):
	l,r=list(map(int,input().split()))
	L.append(l)
	R.append(r)


Lmax=1
Rmin=N
for i in range(M):
	if L[i]>Lmax: Lmax=L[i]
	if R[i]<Rmin: Rmin=R[i]

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

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

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heappush,heappop
import re

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, ss_raw()))

INF = 1<<29



def main():
    N, M = ints_raw()
    rower = 1
    higher = N
    for _ in range(M):
        l,r = ints_raw()
        rower = max(l,rower)
        higher = min(higher,r)
    return max(higher-rower+1,0)
            
print(main())

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
 
n,m = na()
a=1
b=n
for i in range(m):
    l,r = na()
    a=max(a,l)
    b=min(b,r)

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

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
lower=0
upper=1000000000000000
for i in range(b):
    c,d = map(int,input().split())
    if c>lower:
        lower=c
    if d<upper:
        upper=d

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

Submission status: Accepted

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

hidari=0
migi=n+1

for i in range(m):
    list=input().split(" ")
    x=int(list[0])
    y=int(list[1])
    if x>hidari:
        hidari=x
    if y<migi:
        migi=y

#print(hidari, migi)
print(max(migi-hidari+1,0))

Submission status: Accepted

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

Submission status: Accepted

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

lm = max([l for l, r in LR])
rm = min([r for l, r in LR])

print(max(rm - lm + 1, 0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

low = 1
high = n

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

    low = max(low,l)
    high = min(high,r)


ans = high-low + 1

if ans < 0:
    ans = 0

print(ans)

Submission status: Accepted

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

lm,rm = 0,n+1
for i in range(m):
  l,r = [int(it) for it in input().split()]
  lm = max(l,lm)
  rm = min(r,rm)
s = max(rm - lm + 1,0)
print ( s )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    maxl = -float('inf')
    minr = float('inf')
    for l, r in zip(L, R):
        maxl = max(l, maxl)
        minr = min(r, minr)
    
    return max(0, (minr - maxl + 1))


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))
    result = solve(N, M, L, R)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

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())
    if Lmax < L:
        Lmax = L
    if Rmin > R:
        Rmin = R

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

lmin = []
rmax = []
for i in range(M):
    L, R = (int(i) for i in input().split())
    lmin.append(L)
    rmax.append(R)

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

Submission status: Accepted

Solution code:
n, m = (int(_) for _ in input().split())  
x = [list(int(_) for _ in input().split()) for i in range(m)]
l = max([i[0] for i in x])
h = min([i[1] for i in x])

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

Submission status: Accepted

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

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

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)

l = max(l_lst)
r = min(r_lst+[N])

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

Submission status: Accepted

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

l=0
r = 10**9
for g in gates:
    l = max(l, g[0])
    r = min(r, g[1])
print(r-l+1 if r>=l else 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(R-L+1)
else:   print(0)

Submission status: Accepted

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

for _ in range(m):
    l, r = [int(i) for i in input().split()]
    if l > l_max:
        l_max = l
    if r < r_min:
        r_min = r

print(max(r_min - l_max + 1, 0))

Submission status: Accepted

Solution code:

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

l_max = 0
r_min = n+1
for i in range(m):
    a, b = [int(i) for i in input().split()]
    l_max = max(l_max, a)
    r_min = min(r_min, b)
print(max(0, r_min - l_max + 1))

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    acc = [0] * (N+1)
    for _ in range(M):
        left, right = map(int, input().split())
        acc[left-1] += 1
        acc[right] -= 1

    cnt = 0
    for a in accumulate(acc):
        if a == M:
            cnt += 1

    return cnt


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

    l_max = 0
    r_min = float("inf")
    for i in range(M):
        l, r = list(map(int, input().split()))
        l_max = max(l_max, l)
        r_min = min(r_min, r)

    print(max(r_min-l_max+1, 0))    



if __name__ == "__main__":
    solve()

Submission status: Accepted

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

min_id = 0
max_id = N+1
for i in range(M):
    l, r = map(int, input().split())
    if min_id < l:
        min_id = l
    if max_id > r:
        max_id = r

result = max_id - min_id
if result < 0:
    print(0)
else:
    print(result+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [0 for i in range(n + 1)]
for i in range(m):
    L, R = map(int, input().split())
    L -= 1
    R -= 1
    a[L] += 1
    a[R + 1] -= 1
a_t = [0]
for i in range(n + 1):
    a_t.append(a[i] + a_t[i])
ans = 0
for i in range(n + 2):
    if a_t[i] == m:
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
cnt = 0
for i in range(N+2):
    c += x[i]
    if c == M:
        cnt += 1


print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import bisect
from datetime import datetime as dt
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)
maxL = max(L)
minR = min(R)
print(max(0, minR - maxL + 1))

Submission status: Accepted

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

ans = [0]*(N+1)

for _ in range(M):
    L, R = map(int, input().split())
    ans[L-1] += 1
    ans[R] -= 1

for i in range(1, N):
    ans[i] += ans[i-1]

print(ans[:-1].count(M))

Submission status: Accepted

Solution code:
import sys
# gcd
from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
# import math
# from functools import reduce
#
# input関係の定義
# fin = open('in_2.txt', 'r')
# sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

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

    # write code
    maxL = max(L)
    minR = min(R)
    print(max(minR-maxL+1, 0))

Submission status: Accepted

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

Submission status: Accepted

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

le = 0
ri = N - 1
for i in range(M):
    L, R = map(int, input().split())
    L -= 1
    R -= 1
    le = max(le, L)
    ri = min(ri, R)

if (le <= ri):
    print(ri - le + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_c

import itertools
from collections import Counter
from collections import defaultdict
import bisect

def main():
    N, M = map(int, input().split())

    max_l = 0
    min_r = 10 ** 6
    for _ 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))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections as c
ip = lambda : map(int, input().split())

# ##############
N,M = ip()
L,R = ip()
for i in range(1,M):
    l,r = ip()
    if R<l or r<L:
        print(0)
        exit()
    L = max(L,l)
    R = min(R,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)]

pl, pr = 1, N
for l, r in LR:
	pl = max(pl, l)
	pr = min(pr, r)

print(max(0, pr - pl + 1))

Submission status: Accepted

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

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

Submission status: Accepted

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

l = -1
r = N + 1
for i in range(M):
    L, R = map(int, input().split())
    l = max(l, L)
    r = min(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 = -1
r_min = pow(10, 6)

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

Submission status: Accepted

Solution code:
def solve():

  n,m = (int(i) for i in input().split())
  ans = 0
  left = 0
  right = 10**9
  for i in range(m):
    l,r = (int(i) for i in input().split())
    left = max(left,l)
    right = min(right,r)
  if right <left :
    print(0)
  else:
   print(1+right-left)
    
solve()

Submission status: Accepted

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

for _ in range(M):
    tL, tR = [int(i) for i in input().split()]
    if tL > L:
        L = tL
    if tR < R:
        R = tR

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

Submission status: Accepted

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


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


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

ansL = 0
ansR = float('inf')
for _ in range(M):
  l, r = map(int, input().split())
  ansL = ansL if ansL > l else l
  ansR = ansR if ansR < r else r

print(max(0, ansR - ansL + 1))

Submission status: Accepted

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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    lm = max(L)
    rm = min(R)
    print(max(0,rm-lm+1))

    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:
def main():
    N, M = [int(n) for n in input().split()]
    memo = [0]*N
    for i in range(M):
        l, r = [int(n)-1 for n in input().split()]
        memo[l] += 1
        if  r+1 < N:
            memo[r+1] -= 1
    for i in range(N-1):
        memo[i+1] += memo[i]
    res = 0
    for i in range(N):
        if memo[i] == M:
            res += 1
    print(res)
main()

Submission status: Accepted

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

x = 0
y = N
for i in range(M):
    L, R = map(int, input().split())
    x = max(x, L)
    y = min(y, R)

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

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)

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

Submission status: Accepted

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

ans = min(B)-max(A)+1
if ans <= 0:
    ans = 0
print(ans)

Submission status: Accepted

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

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

a=[list(map(int,input().split())) for i in range(M)]
a1=[a[i][0] for i in range(M)]
a2=[a[i][1] for i in range(M)]

ans=max(0,min(a2)-max(a1)+1)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

l=[]
r=[]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def main():
    n, m = map(int, raw_input().split())
    min_id = 1
    max_id = n
    for _ in xrange(m):
        l, r = map(int, raw_input().split())
        min_id = max(min_id, l)
        max_id = min(max_id, r)
    print max(0, max_id - min_id + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = 0
R = 10**6
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 R >= L else 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline

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

A = [0] * (N + 1)
for _ in range(M):
    L, R = map(int, input().split())
    A[L - 1] += 1
    A[R] += -1

for i in range(1, N):
    A[i] += A[i - 1]

c = Counter(A)
print(c[M])

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n, m = map(int, input().split())
    cards = [0] * (n + 2)
    for i in range(m):
        l, r = map(int, input().split())
        cards[l] += 1
        cards[r + 1] -= 1

    cnt, res = 0, 0
    for i in range(n + 2):
        cnt += cards[i]
        if cnt == m:
            res += 1

    print(res)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        if self._buffer.tell():
            return self._buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
data=[0]*(10**5+10)
for i in range(m):
    l,r=map(int,input().split())
    data[l]+=1
    data[r+1]-=1
ans=0
for i in range(len(data)-1):
    data[i+1]=data[i+1]+data[i]
    if data[i+1]==m:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


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


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