Problem:


Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split(" ")))
min_, max_ = 0, N
for i in range(M):
    L, R = list(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())
a=[]
l=[]
r=[]
for i in range(m):
    a.append(list(map(int,input().split())))
    l.append(a[i][0])
    r.append(a[i][1])

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
l, r = 0, 10 ** 5 + 1
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=[int(a) for a in input().split()]
LR=[[int(a) for a in input().split()] for _ in range(M)]
L1,R1=-10**10,10**10
for L, R in LR:
    L1 = max(L1, L)
    R1 = min(R1, R)
print(max(R1-L1+1, 0))

Submission status: Accepted

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

left = 1
right = N

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

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())
    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 = 1
r = 10 ** 6
for i in range(M):
    lr = list(map(int, input().split()))
    l = max(l, lr[0])
    r = min(r, lr[1])
ans = 0
for i in range(N):
    if l <= i + 1 <= r:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from itertools import permutations
input = sys.stdin.readline

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

arr=[1,N]
flag=True
for i in range(M):
	l,r=map(int,input().split())
	if flag:
		arr[0]=max(l,arr[0])
		arr[1]=min(r,arr[1])
	if arr[1]<arr[0]:
		flag=False

if flag:
	print(arr[1]-arr[0]+1)
else:
	print(0)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L,R = map(int,input().split())
pos = True
for i in range(M-1):
    L_,R_ = map(int,input().split())
    if R < L_ or R_ < L:
        pos = False
    L = max(L,L_)
    R = min(R,R_)
if pos:
    print(R-L+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
A,B=[0]*(n+1),[0]*(n+1)
for i in range(m):
  l,r=map(int,input().split())
  A[l-1]+=1
  A[r]-=1
B[0]=A[0]
for i in range(1,n):
  B[i]=B[i-1]+A[i]
print(B.count(m))

Submission status: Accepted

Solution code:
n, m = [int(x) for x in raw_input().split()]
a = []
al, ar = 1, n
for _ in xrange(m):
    l, r = [int(x) for x in raw_input().split()]
    a.append((l, r))
a.sort()
for (l, r) in a:
    al = max(l, al)
    ar = min(r, ar)
ans = max(ar - al + 1, 0)
print ans

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

L=1
R=N
for i in range(M):
    tL,tR = map(int, input().split())
    if tL > L:
        L = tL
    if tR < R:
        R = tR

Ans = R-L+1
if Ans < 0:
    Ans = 0

print(Ans)

Submission status: Accepted

Solution code:
from operator import itemgetter

N,M = [int(x) for x in input().split()]
LR = [[int(x) for x in input().split()]for _ in range(M)]
max_l = max(LR,key = itemgetter(0))[0]
min_r = min(LR,key = itemgetter(1))[1]
print(max(min_r-max_l+1,0))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[]
min=0
max=n
for i in range(m):
    x,y=map(int,input().split())
    if min<x:
        min=x
    if y<max:
        max=y

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

Submission status: Accepted

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

passable = [0] * (n+1)
for l, r in lr:
    passable[l-1] += 1
    passable[r] -= 1
for i in range(n):
    passable[i+1] += passable[i]
print(passable[:n].count(m))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

cards = [0]*N
for L, R in LRs:
    cards[L-1] += 1
    if R < N:
        cards[R] -= 1

cards = list(accumulate(cards))

print(cards.count(M))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ll = 1
rr = n
for i in range(m):
  l, r = map(int, input().split())
  ll = max([ll, l])
  rr = min([rr, r])

if rr-ll+1 < 0:
  print(0)
else:
  print(rr-ll+1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Ls = []
Rs = []
for i in range(M):
    L, R = [int(elm) for elm in input().split(" ")] 
    Ls.append(L)
    Rs.append(R)
    
max_L = max(Ls)
min_R = min(Rs)

#print(max_L, min_R)
out = max(min_R - max_L+1, 0)
print(out)

Submission status: Accepted

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

L = [0 for _ in range(M)]
R = [0 for _ in range(M)]

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

maxL = max(L)
minR = min(R)

sa = minR - maxL + 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
read = input 
rn = lambda :map(int, read().split())
n,m = rn()
s = [0 for i in range(n + 10000)]
for i in range(m):
    l,r = rn()
    s[l] += 1
    s[r+1] -= 1
for i in range(1, len(s) - 10):
    s[i] += s[i-1]
ans = 0
for i in range(1, len(s) - 10):
    if s[i] == m:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lmax = 1
rmin = n
for _ in range(m):
  l,r = map(int,input().split())
  if l > lmax:
    lmax = l
  if r < rmin:
    rmin = r
if rmin >= lmax:
  print(rmin-lmax+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(l, lmax)
    rmin = min(r, rmin)

print(max(0, rmin-lmax+1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Lmax = 0
Rmin = 10 ** 5
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())

ans = [0] * (n + 2)

for i in range(m):

    l, r = map(int, input().split())

    ans[l] += 1

    ans[r + 1] -= 1

count = 0

for i in range(n + 1):

    ans[i + 1] += ans[i]

    if ans[i] == m:
        count += 1

print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

mi = -1
ma = 100000000000
for i in range(M):
    l,r = map(int, input().split())
    mi = max(l, mi)
    ma = min(ma, r)

if ma >= mi:
    print(ma-mi+1)
else:
    print(0)

Submission status: Accepted

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

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

count = 0
for i in range(n):
    if memo[i] == m:
        count += 1

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = [int(item) for item in input().split()]
la, ra = -float('inf'), float('inf')
for i in range(m):
    l, r = [int(item) for item in input().split()]
    la = max(l, la)
    ra = min(r, ra)
print(max(0, ra - la + 1))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,m=LI()
  l1=[]
  l2=[]
  for i in range(m):
    a,b=LI()
    l1.append(a)
    l2.append(b)

  l1.sort()
  l2.sort()

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

main()
# print(main())

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate
def inpl(): return map(int, input().split())

N, M = inpl()
X = [0]*(N+1)
for _ in range(M):
    l, r = inpl()
    X[l-1] += 1
    X[r] -= 1
S = list(accumulate(X))
print(sum([s == M for s in S]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
maxl = -1
minr = 10**6
for i in range(m):
    lh, rh = [int(i) for i in input().split()]
    if maxl < lh:
        maxl = lh
    if minr > rh:
        minr = rh
diff = minr - maxl
if diff >= 0:
    print(diff+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(L, Lmax)
    Rmin = min(R, Rmin)

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

Submission status: Accepted

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

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

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)
ll = max(l)
rr = min(r)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
n,m = map(int,input().split())
gates = [0]*(n+1)
for i in range(m):
    l,r  = map(int,input().split())
    gates[l-1] += 1
    gates[r] -= 1
gates = list(accumulate(gates))
#print(gates)
ans = gates.count(m)
print(ans)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n, m = wi()
lr = mi(m)

gali = [0] * (n + 1)

for i in range(m):
    gali[lr[i][0] - 1] += 1
    gali[lr[i][1]] += -1
gali = list(accumulate(gali))
for i in range(n):
    if gali[i] == m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:


def read_input():
    n, m = map(int, input().split())

    max_l = 0
    min_r = 10**5 + 1

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

        if l > max_l:
            max_l = l

        if r < min_r:
            min_r = r

    return max_l, min_r



def submit():
    l, r = read_input()

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




if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
maxl = 0
minr = float('inf')
for _ in range(M):
    l, r = map(int, input().split())
    maxl = max(maxl, l)
    minr = min(minr, r)

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

Submission status: Accepted

Solution code:
n,m=map(int,raw_input().split())
l = []
r = []
for i in xrange(m):
	li,ri=map(int,raw_input().split())
	l.append(li)
	r.append(ri)
L = max(l)
R = min(r)
print max(0,R-L+1)

Submission status: Accepted

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

    A.append(l)
    B.append(r)
print(max(0,min(B)-max(A)+1))

Submission status: Accepted

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

max_l = 1
min_r = N+1
for i in range(M):
    Li, Ri = (int(x) for x in input().split())
    max_l = max(max_l, Li)
    min_r = min(min_r, Ri+1)

n = max(min_r - max_l, 0)

print(n)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l_max = 0
r_min = 10**5 + 1
for _ in range(M):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)
if r_min - l_max + 1 > 0:
    print(r_min - l_max + 1)
else:
    print("0")

Submission status: Accepted

Solution code:
from sys import stdin

n, m = map(int, raw_input().strip().split())
inp = stdin.readlines()

left = 0
right = pow(10, 9)

for line in inp:
    l, r = map(int, line.strip().split())
    left = max(left, l)
    right = min(right, r)

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

print ans

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsm = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
lis = [0] * (n + 1)
for i in range(m):
    l, r = nm()
    lis[l - 1] += 1
    lis[r] -= 1
for i in range(1, n + 1):
    lis[i] += lis[i - 1]
cnt = 0
for i in lis:
    if i == m:
        cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

L = [0] * (N + 2)

for l, r in LR:
  L[l] += 1
  L[r + 1] -= 1

Ans = 0
for i in range(1, N + 1):
  L[i] += L[i - 1]
  if L[i] >= M:
    Ans += 1

print(Ans)

Submission status: Accepted

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

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

arr = [0]*(n+2)

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

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

for i in range(1,len(arr)):
    arr[i] += arr[i-1]


ans = 0

for i in range(len(arr)):
    if arr[i]==m:
        ans+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=[int(i) for i in input().split()]
L,R=1,n
for i in range(m):
    l,r=[int(i) for i in 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(map(int, input().split())) for _ in range(M)]
L = [l for l,r in LR]
R = [r for l,r in LR]

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

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000


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

ans_max = -1
ans_min = mod
for i in range(M):
    ans_max = max(ans_max,L[i])
    ans_min = min(ans_min,R[i])

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

Submission status: Accepted

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

l = 1
r = n
for _ in range(m):
    li, ri = list(map(int, input().split()))
    l = max(l, li)
    r = min(r, ri)

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

Submission status: Accepted

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

from itertools import accumulate

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

cnt = [0]*(N+1)

for L, R in info:
    L -= 1; R -= 1
    cnt[L] += 1
    cnt[R+1] -= 1

cnt = list(accumulate(cnt))

ans = 0
for c in cnt:
    if c >= M:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n ,m = li()

imos = [0]*(n+1)

for _ in range(m):
    l,r = li_()
    imos[l] += 1
    imos[r+1] -= 1

cum = list(accumulate(imos))

ans = 0
for ci in cum:
    if ci == m:
        ans += 1

print(ans)

Submission status: Accepted

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

imos = [0] * (n + 2)

for l, r in L:
    imos[l] += 1
    imos[r + 1] -= 1

ans = 0

for i in range(n + 1):
    imos[i + 1] += imos[i]
    if imos[i + 1] >= m:
        ans += 1

print(ans)

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

n, m = map(int, input().split())
lr = [list(map(int, input().split())) for i in range(m)]
card = [0 for i in range(n + 1)]
for i, j in lr:
    card[i - 1] += 1
    card[j] -= 1

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1 if imos[0] == m else 0
for i in range(1, n+1):
  imos[i] += imos[i-1]
  if imos[i] == m:
    ans += 1
print(ans)

Submission status: Accepted

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



N,M = map(int, input().split())
csum = [0]*(N+2)
for _ in range(M):
  l,r = map(int, input().split())
  csum[l] += 1
  csum[r+1] -= 1

for i in range(1,N+2):
  csum[i] += csum[i-1]
print(csum.count(M))

Submission status: Accepted

Solution code:
from itertools import accumulate as ac
n,m=map(int,input().split())
b=[0]*(n+1)
for i in range(m):
    l,r=map(int,input().split())
    b[l-1]+=1
    b[r]-=1
b=list(ac(b))[:-1]
print(b.count(m))

Submission status: Accepted

Solution code:
def getlist():
	return list(map(int, input().split()))

N, M = getlist()
l = 0
r = float("inf")
for i in range(M):
	L, R = getlist()
	if L > l:
		l = L
	if R < r:
		r = R

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

Submission status: Accepted

Solution code:
s = [0 for _ in range(int(10e5) + 1)]

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

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


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

ans = 0

for i in range(1,int(1e5)+1):
  s[i] = s[i-1] + s[i]

  if s[i] == m:
    ans += 1
print(ans)

Submission status: Accepted

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

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

for x,y in A :
    if l < x :
        l = x
    if y < r :
        r = y
print(max(0,r-l+1))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    if a <= 5:
        print(0)
    elif 6 <= a <= 12:
        print(b // 2)
    else:
        print(b)
    return

#B
def B():
    r, d, x = LI()
    for i in range(10):
        print(x * r - d)
        x = x * r - d
    return

#C
def C():
    n, m = LI()
    imo = [0 for i in range(n+1)]
    for _ in range(m):
        l, r = LI_()
        imo[l] += 1
        imo[r + 1] -= 1
    for i in range(1,n):
        imo[i] += imo[i - 1]
    ans = 0
    #print(imo)
    for i in imo:
        if i == m:
            ans += 1
    print(ans)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

else:
    print(r-l+1)

Submission status: Accepted

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

max_left = 1
min_right = n

for i in range(m):
    left, right = map(int, input().split())
    max_left = max(max_left, left)
    min_right = min(min_right, right)

print(max(min_right - max_left + 1, 0))

Submission status: Accepted

Solution code:
b, c = map(int, input().split())
max_l = 0
min_r = b
for i in range(c):
    l, r = map(int, input().split())
    max_l = max(l, max_l)
    min_r = min(r, min_r)
print(min_r - max_l + 1 if min_r >= max_l else 0)

Submission status: Accepted

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

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

LT = max(L)
RB = min(R)

print(max(RB-LT+1,0))

Submission status: Accepted

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

Submission status: Accepted

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

min_i = 100000000000
max_i = 0
for i in range(m):
    min_i = min(min_i, info[i][1])
    max_i = max(max_i, info[i][0])


print(max(min_i - max_i + 1, 0))

Submission status: Accepted

Solution code:
N , M = map(int,input().split())
L = [0 for _ in range(10**5+10)]

for i in range(M):
    x,y = map(int,input().split())
    L[x] += 1
    L[y+1] -= 1
from itertools import accumulate
accumulated =  list(accumulate(L))
cnt = 0
for x in accumulated:
    if x == M:
        cnt += 1
print(cnt)

Submission status: Accepted

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

LMAX = 0
RMIN = N
for i in range(M):
    l,r = map(int,input().split())
    if l>LMAX:LMAX=l
    if r<RMIN:RMIN=r
        
ans = RMIN-LMAX+1
print(ans if ans>=0 else 0)

Submission status: Accepted

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

Submission status: Accepted

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

ll, rr = 1, N

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

print(max(rr - ll + 1, 0))

Submission status: Accepted

Solution code:

def main():
    buf = input()
    buflist = buf.split()
    N = int(buflist[0])
    M = int(buflist[1])
    LR = []
    for i in range(M):
        buf = input()
        buflist = buf.split()
        LR.append((int(buflist[0]), int(buflist[1])))
    LR = list(sorted(LR))
    left_limit = LR[-1][0]
    LR = list(sorted(LR, key=lambda x:x[1]))
    right_limit = LR[0][1]
    print(max(0, right_limit - left_limit + 1))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
inf = float('inf')
mod = 10**9+7


def INT_(n): return int(n)-1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [input() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    from itertools import accumulate
    N, M = MI()
    imos = [0]*(N+2)
    for _ in range(M):
        l, r = MI()
        imos[l] += 1
        imos[r+1] -= 1
    cum = list(accumulate(imos))
    ans = 0
    for a in cum:
        if a == M:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, raw_input().split())
L = []
R = []
for i in range(M):
	l1,r1 = map(int, raw_input().split())
	L.append(l1)
	R.append(r1)
l2 = max(L)
r2 = min(R)
if(l2 <= r2):
	print(r2-l2+1)
else:
	print(0)

Submission status: Accepted

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

lm = 0
rm = N

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

result = max(rm - lm + 1, 0)

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# l <= x <= rのIDで通る
n,m = map(int, input().split())
tmp = []
tip = [0] * 10 ** 7
ma = 0
for i in range(m):
    x,y = map(int, input().split())
    ma = max(ma, y)
    tip[x] += 1
    tip[y + 1] -= 1

for i in range(1, ma + 1):
    tip[i] += tip[i - 1]

print(tip.count(m))