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

Solution (Python):


from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)





def main():
    n, m = intin()
    lrlist = intinl(m)

    ml = 1
    mr = n

    for l, r in lrlist:
        ml = max(l, ml)
        mr = min(r, mr)

    print(max(0, mr - ml + 1))


if __name__ == '__main__':
    main()

Solution (Python):


from collections import deque
import itertools as ite
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = map(int, raw_input().split())
DP = [0] * (N + 2)

for i in range(M):
    L, R = map(int, raw_input().split())
    DP[L] += 1
    DP[R + 1] -= 1

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

print sum(num == M for num in DP)

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

Solution (Python):
from itertools import accumulate
N, M = map(int, input().split())
imos = [0] * (N + 2)
for i in range(M):
    l, r = map(int, input().split())
    imos[l] += 1
    imos[r + 1] -= 1

imos = list(accumulate(imos))

ans = 0
for im in imos:
    ans += (im == M)

print(ans)

Solution (Python):
n, m = map(int, input().split())
mx, my = 0, n + 1
for i in range(m):
	x, y = map(int, input().split())
	mx = max(mx, x)
	my = min(my, y)
print(max(0, my - mx + 1))

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

max_l = 0
min_r = lr[0][1]

for l,r in lr:
    max_l = max(l,max_l)
    min_r = min(r,min_r)

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

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

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

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

L = [Li for Li, Ri in LR]
R = [Ri for Li, Ri in LR]

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

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

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

Solution (Python):

import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    l = sorted(L)
    r = sorted(R)
    ret = max(0, r[0] - l[-1] + 1)
    print(ret)
    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))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

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

Solution (Python):
N, M = map(int, input().split())
L, R = (
    zip(*(map(int, input().split()) for _ in range(M))) if M else
    ((), ())
)

x = 1
y = N
for l, r in zip(L, R):
    x = max(l, x)
    y = min(r, y)
ans = max(y - x + 1, 0)
print(ans)

Solution (Python):

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
n,m=IL()
l,r=IL()
for i in range(m-1):
    nl,nr=IL()
    l=max(nl,l)
    r=min(nr,r)
print(max(r-l+1,0))

Solution (Python):
N,M = map(int,input().split())
lr = []
for _ in range(M):
    l, r = map(int,input().split())
    lr.append((l,r))
maxim = 10**6
minumum = 0

for l, r in lr:
    if minumum < l:
        minumum = l
    if r < maxim:
        maxim = r

print(max(maxim-minumum+1,0))

Solution (Python):
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))

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

Solution (Python):

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


L = [0] * (N + 1)

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

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

print(L.count(M))

Solution (Python):
from collections import defaultdict as ddic, deque, Counter
from itertools import permutations, combinations, product
import bisect, heapq

rr = raw_input
rri = lambda: int(raw_input())
rrm = lambda: map(int, raw_input().split())

def solve(N, M, gates):
    OPEN, CLOSE = 1, 0
    events = []
    for L, R in gates:
        events.append((L, OPEN))
        events.append((R+1, CLOSE))
    events.sort()

    active = prev = 0
    ans = 0
    for x, cmd in events:
        if active == M and cmd == CLOSE:
            
            ans += x - prev
        if cmd == OPEN:
            active += 1
        else:
            active -= 1
        
        prev = x
    return ans

N, M = rrm()
gates = [rrm() for _ in xrange(M)]
print solve(N, M, gates)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    n,m = LI()
    aa = [LI() for _ in range(m)]
    l = 0
    r = inf
    for a,b in aa:
        if l < a:
            l = a
        if r > b:
            r = b

    return max(0,r-l+1)


print(main())

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

left = -1
right = N
for _ in range(M):
    tl, tr = map(int, input().split())
    left = max(left, tl)
    right = min(right, tr)

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

Solution (Python):
n,m=map(int,input().split())
ma=float("inf")
mi=0
for i in range(m):
    a,b=map(int,input().split())
    ma=min(ma,b)
    mi=max(mi,a)
print(max(ma-mi+1,0))

Solution (Python):
import sys
import bisect
from collections import defaultdict

ans=0

n,m=map(int,input().split())
l=[0]*(n)
r=[0]*(n)
a=[0]*(n+3)
for i in range(m):
    x,y=map(int,input().split())
    a[x]+=1
    a[y+1]+=-1
for i in range(n):
    a[i+1]+=a[i]
ans=a.count(m)

print(ans)

Solution (Python):
from itertools import accumulate
N, M = map( int, input().split())
l = 1
r = N
for _ in range(M):
    a, b = map( int, input().split())
    if l <= a:
        l = a
    if b <= r:
        r = b
if r - l >= 0:
    print(r-l+1)
else:
    print(0)

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

lmax = 0
rmin = 10**9

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

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

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

Solution (Python):
import sys

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

Solution (Python):
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

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

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

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,M = inpl()
imos = [0]*(N+1)

for i in range(M):
    l,r = inpl()
    l,r = l-1,r-1
    imos[l] += 1
    imos[r+1] -= 1

tmp = 0
for i in range(N+1):
    tmp += imos[i]
    imos[i] = tmp

ans = 0
for k in imos:
    if k == M:
        ans += 1
print(ans)

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

ans = 0
for i in range(N):
    if C[i] == M:
        ans += 1
print(ans)

Solution (Python):
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(0, min(R)-max(L)+1))

Solution (Python):
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(0, r-l+1))

Solution (Python):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
l,r=0,n+1
for _ in range(m):
    a,b=map(int,input().split())
    l=max(l,a)
    r=min(r,b)
print(max(0,r-l+1))

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

Solution (Python):
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 < 0:
    print(0)
else:
    print(r-l+1)

Solution (Python):
import sys
input = sys.stdin.readline

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

MIN=0
MAX=10**9

for a,b in G:
    MIN=max(MIN,a)
    MAX=min(MAX,b)

print(max(0,MAX-MIN+1))

Solution (Python):
I=lambda:map(int,input().split())
N,M=I()
L=[]
R=[]
for i in [0]*M:
    l,r=I()
    L+=[l]
    R+=[r]
print(max(min(min(R),N)-max(L)+1,0))

Solution (Python):
import math
import sys
sys.setrecursionlimit(10 ** 6 + 10)
from collections import defaultdict

def kari():
    map(int, input().split())

    num, num2 = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num2)]

def main():
    num, num2 = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num2)]

    pre, aft = data[0][0], data[0][1]
    for i in range(num2):
        pre = max(data[i][0], pre)
        aft = min(aft, data[i][1])

    ans = min(num, aft) - pre + 1
    if ans < 0:
        ans = 0
    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, m = na()
l = 1
r = n
for i in range(m):
    ll, rr = na()
    l = max(l, ll)
    r = min(r, rr)
if l > r:
    print(0)
else:
    print(r-l+1)

Solution (Python):
n, m = map(int, input().split())
imos = [0]*(n+1)
for i in range(m):
    a,b = map(int,input().split())
    imos[a-1] += 1
    imos[b] -=1
for i in range(n):
    imos[i+1] += imos[i]
print(imos.count(m))

Solution (Python):
n,m = map(int,input().split())
a,b = -10**10, 10**10
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))

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

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

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