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(" "))
LR = [input() for _ in range(M)]

left = 1
right = N
for i in LR:
    Li, Ri = map(int, i.split(" "))
    left = max(left, Li)
    right = min(right, Ri)

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

Submission status: Accepted

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

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

if __name__=="__main__":
    solve()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

ll=1
rr=n
for _ in range(m):
    l,r = [int(i) for i in input().split()]
    if ll < l:
        ll=l
    if rr > r:
        rr=r
    


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

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())
    L = max(L,l)
    R = min(R,r)

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

# n = int(input())
# a, b = map(int, input().split())
# int_list = list(map(int, input().split()))
# l = list(input().split())

def solve():
    l_list = []
    r_list = []
    n, m = map(int, input().split())
    for i in range(m):
        l, r = map(int, input().split())
        l_list.append(l)
        r_list.append(r)

    max_l = max(l_list)
    min_r = min(r_list)
    print(max(0, min_r - max_l + 1))

solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def getN():
    return int(input())

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

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

n, m = getlist()
imos = [0 for i in range(n+1)]
for i in range(m):
    a, b = getlist()
    imos[a-1] += 1
    imos[b] -= 1

accum = [0]
tmp = 0
for val in imos:
    tmp += val
    accum.append(tmp)

ans = 0
for v in accum:
    if v == m:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys


fin = sys.stdin.readline
N, M = [int(elem) for elem in fin().split()]
cards = [[int(elem) - 1 for elem in fin().split()] for _ in range(M)]

num_cards_passes_at = [0] * N
for l, r in cards:
    num_cards_passes_at[l] += 1
    if r < N - 1:
        num_cards_passes_at[r + 1] -= 1

cur_state = 0
for i in range(N):
    cur_state += num_cards_passes_at[i]
    num_cards_passes_at[i] = cur_state

# print(num_cards_passes_at)
print(sum(state == M for state in num_cards_passes_at))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
n,m = map(int,input().split())
imos = [0]*(10**5+10)
for i in range(m):
    l,r = map(int,input().split())
    imos[l-1] += 1
    imos[r] -= 1
A = accumulate(imos)
cnt = 0
for a in A:
    if a == m:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import math
sys.setrecursionlimit(20000000)
#input = sys.stdin.readline
n,m = map(int,input().split())
x = []
LE = 0
RI = n
for i in range(m):
    a,b = map(int,input().split())
    LE = max(LE,a)
    RI = min(RI,b)
print(max(0,RI-LE+1))

Submission status: Accepted

Solution code:
def cumsum(l):
    for i in range(1, len(l)):
        l[i] += l[i-1]
    return l


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

    imos = [0] * (N+1)

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

    imos = cumsum(imos)
    # print(imos)

    print(len([x for x in imos if x == M]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    N, M = map(int, line_input())
    gate = [0 for n in range(N+1)]
    for i in range(M):
        l, r = map(int, line_input())
        gate[l-1] += 1
        gate[r] -= 1
    ans = (1 if gate[0] == M else 0)
    for i in range(1, N):
        gate[i] = gate[i] + gate[i-1]
        if gate[i] == M: ans += 1
    print(ans)
    return 0
  
if __name__ == "__main__":
    solve()

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)
l = max(L)
r = min(R)

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

Submission status: Accepted

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

Submission status: Accepted

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

max_l = -1
min_r = 999999
for i in range(0, m):
    l, r = map(int, raw_input().split(" "))
    max_l = max(max_l, l)
    min_r = min(min_r, r)

ans = min_r - max_l + 1

if ans <= 0:
    print(0)

else:
    print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
N, M = map(int, input().split())
T = [0]*(N+1)

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

T = list(accumulate(T))
print(T.count(M))

Submission status: Accepted

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

l_max, r_min = -1, 100000
for _ in range(M):
    L, R = list(map(int, input().split()))
    if l_max < L:
        l_max = L
    if r_min > R:
        r_min = R

if r_min >= l_max:
    print(r_min - l_max + 1)
else:
    print(0)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

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

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

    ans = 0
    for i in range(len(dp)):
        ans += dp[i] == M
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = (int(num) for num in input().split())

a = 1
b = n
for _ in range(m):
    l,r = (int(num) for num in input().split())
    if l > a:
        a = l
    if r < b:
        b = r
        
if b >= a:
    print(b-a+1)
else:
    print(0)

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

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math

import array
import bisect
import collections
from collections import Counter, defaultdict
import fractions
import heapq
import re

sys.setrecursionlimit(1000000)


def array2d(dim1, dim2, init=None):
    return [[init for _ in range(dim2)] for _ in range(dim1)]

def argsort(l, reverse=False):
    return sorted(range(len(l)), key=lambda i: l[i], reverse=reverse)

def argmin(l):
    return l.index(min(l))

def YESNO(ans, yes="YES", no="NO"):
    print([no, yes][ans])

II = lambda: int(input())
MI = lambda: map(int, input().split())
MIL = lambda: list(MI())
MIS = lambda: input().split()


def main():
    N, M = MI()
    lr = [0] * (N + 1)
    rr = [0] * (N + 1)
    for i in range(M):
        l, r = MI()
        lr[l] += 1
        rr[r] += 1
    c = 0
    ans = 0
    for i in range(N + 1):
        c += lr[i]
        if c == M: ans += 1
        c -= rr[i]
    return ans


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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD=10**9+7

N,M=rl()
L=[0]*M
R=[0]*M
for i in range(M):
    L[i],R[i] = rl()

gate = [0]*(N+2)
for l,r in zip(L,R):
    gate[l] += 1
    gate[r+1] -= 1
for i in range(1,N+1):
    gate[i] += gate[i-1]

print(gate.count(M))

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

Submission status: Accepted

Solution code:
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, M = i2nn()
    LR = ii2nnn(M)
    L, R = 0, int(1e+5)
    for l, r in LR:
        if L < l:
            L = l
        if R > r:
            R = r
    n = R - L + 1
    if n < 0:
        n = 0
    print(n)

main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    from sys import stdin, stdout

    def read():
        return stdin.readline().rstrip('\n')

    def read_array(sep=None, maxsplit=-1):
        return read().split(sep, maxsplit)

    def read_int():
        return int(read())

    def read_int_array(sep=None, maxsplit=-1):
        return [int(a) for a in read_array(sep, maxsplit)]

    def write(*args, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in args) + end)

    def write_array(array, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in array) + end)

    n, m = read_int_array()
    l, r = 1, n
    for i in range(m):
        l1, r1 = read_int_array()
        l = max(l, l1)
        r = min(r, r1)
    write(max(r+1-l, 0))


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
###############################################################################

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

Submission status: Accepted

Solution code:
#!/usr/bin/env 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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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))  # 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())
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#ABC127-C
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))

Submission status: Accepted

Solution code:
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))

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

Submission status: Accepted

Solution code:
# N = int(input())
N, M = map(int, input().split())
# L = list(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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
import sys
import bisect
from collections import defaultdict

ans=0
#n=int(input())
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

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(0, min(R)-max(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)
print(max(0, r-l+1))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

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

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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Runtime Error

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

for _ in range(m):
    x, y = map(int, input().split())
    tmp = [i for i in range(x, y + 1)]
    for ele in tmp:
        ans.remove(ele)
print(n - len(ans))

Submission status: Runtime Error

Solution code:
#ABC127 C

N,M = map(int,input().split())
x,y = [],[]
C = []
for i in range(M):
    L,R = map(int,input().split())
    x.append(L)
    y.append(R)
    C.append([j in range(L,R+1)])
D = 0
for k in range(N):
    K = 0
    for l in range(M):
        if k in C[l]:
            K += 1
    if K == M:
        D += 1
        
print(D)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

print(min-max+1)