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: Runtime Error

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n, m = mi()
    l_list = []
    r_list = []
    for _ in range(m):
        l, r = mi()
        l_list.append(l)
        r_list.append(r)
    print(max(min(r_list)-max(l_list)+1), 0)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
N, M = map(int, sys.stdin.readline().split())
LR = [list(map(int,sys.stdin.readline().split())) for i in range(N)]
B = list(range(0, 10 ** 5 + 1))
for i in LR:
	A = list(range(i[0], i[1]))
	B = set(A) & set(B)
print(len(B))

Submission status: Runtime Error

Solution code:
import collections
N,M=map(int,input().split(" "))
List=[input().split(" ") for _ in range(M)]

List3=[]
for i in range(M):
    List2=[x for x in range(int(List[i][0]),int(List[i][1])+1)]
    for j in range(len(List2)):
        List3.append(List2[j])
List3.sort()
List4=list(set(List3))

Ans=[0]*len(List3)
for i in range(len(List3)):
    Ans[List3[i]-1]+=1

count=0    
for i in range(len(Ans)):
    if Ans[i]==M:
        count+=1
print(count)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

card = np.array([i+1 for i in range(N)])

for l, r in LR:
    card = card[(card >= l) & (card <= r)]
    if len(card) == 0:
        break
print(len(card))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,M= list(map(int,input().split()))
end = []
start = []
for i in range(M):
    A,B = list(map(int,input().split()))
    end.append(B)
    start.append(A)
end_min = min(end)
start_max = max(start)
if start_max >= end_min:
    print(0)
else:
    print(end_min-start_end+1)

Submission status: Runtime Error

Solution code:
N,M= list(map(int,input().split()))
end = []
start = []
for i in range(M):
    A,B = list(map(int,input().split()))
    end.append(B)
    start.append(A)
end_min = min(end)
start_max = max(start)
if start_max => end_min:
    print(0)
else:
    print(end_min-start_end+1)

Submission status: Runtime Error

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

A=[int(P[0][0]), int(P[0][1])]

for i in range (1, M):
	if int(P[i][0]) <= A[1]:
		A[0] = max(A[0], int(P[i][0]))
		A[1] = min(A[1], int(P[i][1]))
	else:
		A=[]

if len(A) >0:
	choniki = list(range(A[0], A[1]+1))           
	print(len(choniki))
else:
	print(0)

Submission status: Runtime Error

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

A=[int(P[0][0]), int(P[0][1])]

for i in range (1, M):
	if int(P[i][0]) <= A[1]:
		A[0] = max(A[0], int(P[i][0]))
		A[1] = min(A[1], int(P[i][1]))
	else:
		A=[]

if len(A) >0:
	choniki = list(range(A[0], A[1]+1))           
	print(len(choniki))
else:
	print(0)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
l0, r0 = 1, n
for j in range(m):
    l1, r1 = map(int, inp)
    l0 = max(l0, l1)
    r0 = min(r0, r1)
print(max(0, r0-l0+1))

Submission status: Runtime Error

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

ans = [table[0], table[1]]

for i in table:
    if ans[0] >= i[0] <= ans[1]:
        ans[0] = i[0]
    if ans[0] >= i[1] <= ans[1]:
        ans[1] = i[1]

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
_, __, *d = map(int, open(0).read().split())
print(max(min(d[1::2]) - max(d[::2]) + 1), 0)

Submission status: Runtime Error

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

maxi = 0
mini = 10**6
for lr in LR:
    maxi = max(lr[0],maxi)
    mini = min(lr[1],mini)

if mini-maxi+1 >= n:
    print(n)
else:
    print(mini-maxi+1)

Submission status: Runtime Error

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)
    if l>r:
        print(0)
        sys.exit()
print(r-l+1)

Submission status: Runtime Error

Solution code:
N, M = [int(i) for i in input().split()]
left = 0
right = M
for _ in range(N):
  L, R = [int(i) for i in input().split()]
  if left < L:
    left = L
  if right > R:
    right = R
print(R-L+1)

Submission status: Runtime Error

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep = ' ', end = '\n'):
    print(*var, file=sys.stderr, end = end, sep = sep)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, m = I()
a = [0] * (n + 10)
for i in range(m):
    x, y = I()
    a[x] += 1
    a[y + 1] -= 1
ans = 0
for i in range(1, n + 10):
    a[i] = a[i] + a[i - 1]
    if a[i] == m:
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
li = [list(map(int,input().split())) for _ in range(m)]
maxl = max(li, key=lambda x:x[0])[0]
minr = min(li, key=lambda x:x[1])[1]
if maxl <= minr:
    print(r-l+1)
else:
    print(0)

Submission status: Runtime Error

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

left  = 0
right = n
for i in range(m):z
  l, r = map( int, input().split() )
  left = max( left, l )
  right = min( right, r )

print( right - left + 1 )

Submission status: Runtime Error

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)

min = min(L)
max = max(R)

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
ans = [-1,10**6]

for i in range(m):
    tmp = list(map(int,input().split()))
    ans[0] = max(ans[0],min(tmp))
    ans[1] = min(ans[1],max(tmp))

print(max(ans[1]-ans[0]+1),0)

Submission status: Runtime Error

Solution code:
100000 1
1 100000

Submission status: Runtime Error

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: Runtime Error

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

Submission status: Runtime Error

Solution code:

n, m = map(int, input().split())
L = [0] * m
R = [0] * def funcname(self, parameter_list):
    pass
for i in range(m):
    L[i], R[i] = map(int, input().split())
print(min(R) - max(L) + 1)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(m)]
mi=1
ma=n
for i in range(n):
  mi=max(mi,a[i][0])
  ma=min(ma,a[i][1])

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(n)]
mi=1
ma=n
for i in range(n):
  mi=max(mi,a[i][0])
  ma=min(ma,a[i][1])

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

Submission status: Runtime Error

Solution code:
#include<iostream>

using namespace std;

int main(void) {
 int n, m, tmp1, tmp2, ans=0;
 cin >> n >> m;
 int left=-1, right=10000000;
 for(int i=0;i<m;i++) {
   cin >> tmp1 >> tmp2;
   if(left < tmp1) left = tmp1;
   if(right > tmp2) right = tmp2;
 }
 ans = max(right-left+1, 0);
 cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
from sys import stdin

n, m = (int(x) for x in stdin.readline().rstrip().split())
lr = [stdin.readline().rstrip().split() for _ in range(m)]
l = max([i[0] for i in lr])
r = min([i[1] for i in lr])

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

Submission status: Runtime Error

Solution code:
from sys import stdin

n, m = (int(x) for x in stdin.readline().rstrip().split())
s = int()
e = int()
for i in range(m):
    line = stdin.readline().rstrip().split()
    if i == 0:
        s = int(line[0])
        e = int(line[1])
    else:
        if s <= int(line[0]) <= e:
            s = int(line[0])
        if s <= int(line[1]) <= e:
            e = line[1]

print(len([i for i in range(s, e+1) if 1 <= i <= n]))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = []
R = []
count = 0
for j in range(M):
  L,R = map(int,input().split())
for i in range(N+1):
  if i > max(L) and i < min(R):
    count += 1

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L,R = list(map(int,input().split()))
count = 0
for i in range(N+1):
  if i > max(L) and i < min(R):
    count += 1

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L,R = [list(map(int,input().split())) for _ in range(M)]
count = 0
for i in range(N+1):
  if i > max(L) and i < min(R):
    count += 1
print(count)

Submission status: Runtime Error

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

left = max(L)
right = min(R)

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

Submission status: Runtime Error

Solution code:
n, m, *lst = map(int, input().split())
l = max(lst[::2])
r = min(lst[1::2])
if l > r:
  print(0)
else:
  print(r - l + 1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
L=[0]*M
R=[0]*M
for i in range(M):
  L[i],R[i]=map(int,input().split())
if M==1:
  print(R[0]-L[0]+1)
else:
  s=0
  for j in range(L[0],R[0]+1):
    if all(L[k]<=j<=R[k] for k in range(j,M)):
      s+=1
print(s)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
L=[0]*M
R=[0]*M
for i in range(M):
  L[i],R[i]=map(int,input().split())
if M==1:
  print(L[0]-R[0]+1)
else:
  s=0
  for j in range(L[0],R[0]+1):
    if all(L[k]<=j<=R[k] for k in range(j,M)):
      s+=1
print(s)

Submission status: Runtime Error

Solution code:
#include <iostream>
using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;
	int a = 0;
	int b = n;
	for (int i = 0; i < m; i++){
		int l, r;
		cin >> l >> r;
		a = max(a, l);
		b = min(b, r);
	}

	cout << (a <= b ? b-a+1 : 0) << endl;
}

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = list(map(int,input().split()))
R = list(map(int,input().split()))
ans = 0
if N >= min(R):
  ans = max(0,min(R) - min(L) + 1)
else:
  ans = max(0,N - min(L) + 1)
print(ans)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = [list(map(int,input().split())) for _ in range(M)]
R = [list(map(int,input().split())) for _ in range(M)]
L.sort()
R.sort()
count = 0
for i in range(1,N+1):
  if i >= L[-1] and i <= R[0]:
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = list(map(int,input().split()))
R = list(map(int,input().split()))
L.sort()
R.sort()
count = 0
for i in range(1,N+1):
  if i >= L[-1] and i <= R[0]:
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = list(map(int,input().split()))
R = list(map(int,input().split()))
L.sort()
R.sort()
count = 0
for i in range(N):
  if i >= L[0] and i <= R[0]:
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
LR = [list(map(int,input().split())) for _ in range(N)]
count = 0
for i in range(N):
  for j,k in LR:
    if i >= j and i <= k:
      count += 1
print(count)

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
ar = [0,N]
for m in range(M):
    l,r = map(int, input().split())
    if l>ar[0]:
        ar[0] = l
    if r<ar[1]:
        ar[1]=r
print(max([0,ar[1]-ar[0]+1])

Submission status: Runtime Error

Solution code:
#TLEする
#こんなことしなくても最小と最大を更新していけばおｋ
#m-1にしてた
n,m=map(int,input().split())
l,r=map(int,input().split())
l_max,r_min=l,r
for i in range(m):
    l,r=map(int,input().split())
    if l_max<l: l_max=l
    if r_min>r: r_min=r
print(r_min-l_max+1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
left = 0 # 制約における最小を超える値
right = 10 ** 6 # 制約における最大を超える値
for _ in range(m):
  l, r = map(int, input().split())
  left = max(left, l)
  right = min(right, r)
    right = r

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

Submission status: Runtime Error

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

a = range(table[0][0],table[0][1]+1)
for i,t in enumerate(table[1:]):
    if i == 0:
    	b = range(t[0], t[1]+1)
    	ans = set(a) & set(b)
    else:
        b = range(t[0], t[1]+1)
        ans = ans & set(b)
print(len(ans))

Submission status: Runtime Error

Solution code:
100000 1
1 100000

Submission status: Runtime Error

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

ans=set()
l, r = map(int, input().split())
for j in range(l,r+1):
    ans.add(j)

for i in range(m-1):
  l, r = map(int, input().split())
  temp=set()
  lm=min(list(ans))
  rm=min(list(ans))
  for j in range(min(l,lm), min(r,rm)+1):
    temp.add(j)
  ans=temp & ans

print(len(ans))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(m)]
ans=0
for i in range(1,N+1):
    if all([i in range(l[k][0],l[k][1]+1) for k in range(m)]):
        ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
N,M=map(int, input().split())
pass_id = range(1,N+1)

for i in range(M):
    L,R=map(int, input().split())
    for id in pass_id:
        if not id in range(L,R+1):
            pass_id.pop(pass_id.index(id))
print(len(pass_id))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

landl=set(range(L[0],R[0]+1))


print(len(list(landl & set(range(L[i],R[i]+1)) for i in range(1,M))[0]))

Submission status: Runtime Error

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

l, r = map(int, input().split())
lis = [i for i in range(l, r+1)]

for i in range(M-1):
    l, r = map(int, input().split())
    if r > max(lis):
        r = max(lis)
    if l < min(lis):
        l = min(lis)
    tmp_lis = [i for i in range(l, r+1)]
    lis = set(lis) & set(tmp_lis)

lis = [i for i in lis if i <= N]
print(len(lis))

Submission status: Runtime Error

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

ans = 0
for i in range(n):
    for j in range(m):
        if not((i + 1) >= l[j] and (i + 1) <= r[j]):
            break
        if j == m - 1:
            ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

mat = np.ones((M, N), dtype=np.int32)

for i, (l, r) in enumerate(lr):
    mat[i, 0:l-1] = 0
    mat[i, r:N] = 0

ans = mat[0,:]
    
for i in range(1, M):
    ans *= mat[i,:]

print(sum(ans))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
array = []
for i in range(M):
    a = list(map(int,input().split()))
    array.append(a)
maxa = max(array)
maxa = maxa[1]
array2 = [0 for i in range(maxa+2)]
for i in array:
    array2[i[0]] += 1
    array2[i[1]+1] -= 1
for i in range(len(array2)-1):
    array2[i+1] += array2[i]
print(array2.count(M))

Submission status: Runtime Error

Solution code:
def main():
    LR = open(0).read().split()[2:]
    max_L = max(map(int, LR[::2]))
    min_R = min(map(int, LR[1::2]))
    print(min_R - max_L + 1 if max_L <= min_M else 0)
    return


main()

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=0
b=float('inf')
for i in range(m):
      c,d=map(int,input().spilt())
      a=max(a,c)
      b=min(b,d)
print(max(0,b-a+1))

Submission status: Runtime Error

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())
ct=[0]*(n+1)
for i in range(m):
    ct[l[i]] += 1
    ct[r[i]] += 1

while ct[0]==0:
    ct.pop(0)
while ct[len(ct)-1]==0:
    ct.pop(len(ct)-1)
suc=0
ma=0
for i in range(1,m+1):
    if ct[i]==0:
        suc+=1
    else:
        ma=max(suc,ma)
    if i==m:
        ma=max(suc,ma)

print(ma)

Submission status: Runtime Error

Solution code:
n, m=map(int, input().split())
l, r=list(map(int, input().split()))
l.sort()
r.sort()
if max(l) < min(r):
  print(0)
else:
  print(min(r) - max(l) + 1)

Submission status: Runtime Error

Solution code:
import numpy as np
N, M = [int(i) for i in input().split()]
L = [0]*M
R = [0]*M
for i in range(M):
    L[i], R[i] = [int(i) for i in input().split()]

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

Submission status: Runtime Error

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

lmin = 1
rmax = n
for _ in range(m):
    l,r = map(int,input().split())
    if l > lmin:
        lmin = l
    if rmax > r:
        rmax = r
    
print(max(rmax - lmin + 1), 0)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    L=-INF; R=INF
    for _ in range(m):
        l,r=map(int,input().split())
        L=max(L,l)
        R=min(R,r)
    print(max(R-L+1),0)
resolve()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

lm=max(l)
mr=min(r)
x=mr-lm+1

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

for j in range(m):
  num1 = max(num[j][0], num1)
  num2 = min(num[j][1], num2)

ans = num2 - num1 + 1

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

Submission status: Runtime Error

Solution code:
import collections
n,m = map(int, input().split())
gate = []
for i in range(m):
  l,m = map(int, input().split())
  for j in range(l,m+1):
    gate.append(j)
c = collections.Counter(gate)
values, counts = zip(*c.most_common())
count = 0
for i in range(1,len(counts)+1):
  if counts[i-1] == counts[i]:
    count += 1
print(count)

Submission status: Runtime Error

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

for _ in range(M):
  L ,R = map(int, input().split())
  for i in range(L,R+1):
      card[i]=0
      
print(card.count(1))

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
Key=[0]*(N+1)
for i in range(M):
    L,R = map(int,input().split())
    Key[L-1] += 1
    Key[R] -= 1
for i in range(N):
    Key[i+1] += Key[i] # 累積和
print(Key)
print(Key.count(m))

Submission status: Runtime Error

Solution code:
n,m=map(int,input())
 
l=-1
r=10**6
 
for _ in range(m):
  il, ir=map(int,input())
  l=max(l,il)
  r=min(r,ir)
  
print(max(0,r-l+1))

Submission status: Runtime Error

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

l=-1
r=10**6

for _ in range(m):
  il, ir=map(int,input())
  l=max(l,il)
  r=min(r,ir)
  
print(r-l+1)

Submission status: Runtime Error

Solution code:
import heapq
N,M = map(int, input().split())
A = list(map(int, input().split()))
BC = []

for i in range(M):
    BC.append(list(map(int, input().split())))

BC.sort(key=lambda x: x[1], reverse=True)
A_min = min(A)
heapq.heapify(A)
for i in range(M):
    for j in range(BC[i][0]):
        if BC[i][1] > min(A):
            heapq.heappush(A,BC[i][1])
            heapq.heappop(A)

    A_min = min(A)

print(sum(A))

Submission status: Runtime Error

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

L = []
R = []

for i in range(M):
    inp = input().split()
    L.append(inp[0])
    R.append(inp[1])
    
if max(L) <= min(R):
    print(min(R)-max(L)+1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L,R = map(int,input().split())
tmp = list(range(L,R+1))
for i in range(M-1):
  L,R = map(int,input().split())
  ans = []
  for j in tmp:
    if L<=j<=R:
      ans.append(j)
  tmp = ans
print(len(ans))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L = []
R = []
ans = 0
for _ in range(M):
    inp = int(input().split())
    L.append(inp[0])
    R.append(inp[1])
for i in range(N):
    if L[i] < i + 1 <R[i]:
        ans += 1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

L_max = max(L)
R_min = min(R)

ans = R_min - L_max +1
print(ans)

Submission status: Runtime Error

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

count = 0
for i in range(N):
  if i+1 >= List[i][0] & i+1 <= List[i][1]:
    count = count + 1

print(count)

Submission status: Runtime Error

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

for i in range(N):
  if i+1 >= List[2*i] & i+1 <= List[2*i+1]:
    count = count + 1
print(count)

Submission status: Runtime Error

Solution code:
N, M = int(int, input().split)
List = [map(int,input().split()) for i in range(M)]
count = 0
for i in range(N):
  if i+1 >= List[2*i] & i+1 <= List[2*i+1]:
    count = count + 1
  else:
    continue

print(count)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
lst=[[] for _ in range(M)]
for i in range(M):
  lst[i]=list(map(int,input().split()))
ans=0
visited=[]
ini=lst.pop(0)
tmp=[i for i in range(ini[0],ini[1]+1)]

def f(tmp):
  if len(lst)==0:
    return len(tmp)
  else:
    l=lst.pop(0)
    v1,v2=l[0],l[1]
    if tmp==[]:
      return 0
    else:
      m1=v1
      M1=v2
      m2=min(tmp)
      M2=max(tmp)
      m,M=max(m1,m2),min(M1,M2)
      tmp=[i for i in range(m,M+1)]
      return f(tmp)
    
print(f(tmp))

Submission status: Runtime Error

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())
ans = 0
A = [0] * N
for i in range(M):
    A[L[i]:R[i]+1] += 1
for i in range(N):
    if A[i] == M:
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n,m=list(map(int, input().split() ))
Firstlis=[]
Secondlis=[]
for i in range(m):
  s=list(map(int,inout().split()))
S1=s[0]
S2=s[1]
Firstlis.append(S1)
Secondlis.append(S2)
Min=max(Firstlis)
Max=min(Secondlis)
print(Max-Min)

Submission status: Runtime Error

Solution code:
n,m=list(map(int, input().split() ))
ists=[]
for i in range(m):
  s=list(map(int,inout().split()))
  ists.append(s)
ans=0
justified=True
for j in range(n):
  for k in range(m):
    if ists[k][0]<=j<=ists[k][1]:
      continue
    else:
      justified=False
      break
  if justified:
    ans+=1
    justified=True

print(ans)

Submission status: Runtime Error

Solution code:
n,m=list(map(int,input().split()))
ists=[]
for i in range(m):
  s=list(map(int,inout().split()))
  ists.append(s)
ans=0
justified=True
for j in range(n):
  for k in range(m):
    if ists[k][0]<=j<=ists[k][1]:
      continue
    else:
      justified=False
      break
  if justified:
    ans+=1
print(ans)

Submission status: Runtime Error

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

Ss = [list(map(int,input().split()))[1:] for _ in range(M)]
Ps = list(map(int,input().split()))

ans = 0
switch_pattern_num = 2**N

for i in range(switch_pattern_num):
    binary = format(i,'0%ib'%N)
    satisfied = True
    for j,S in enumerate(Ss):
        count = 0
        for e in S:
            if int(binary[e-1]):
                count += 1
        if count % 2 != Ps[j]:
            satisfied = False
    if satisfied:
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    x, y=[int(i) for i in input().split()]
    l.append(x)
    r.append(y)
ans = 0
for i in range(n):
    cnt = 0
    for j in range(m):

        for k in range(l[j], r[j]+1):S
            if i + 1 == k:
                cnt += 1
        if cnt == m:
            ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L_list = []
R_list = []

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

print(L - R + 1)

Submission status: Runtime Error

Solution code:
n,m = [int(i) for i in input().split()]
if m == 1:
    print(1)
else:
    s1,e2  = {int(i) for i in input().split()}
    num_set = {i for i in range(s1,e2+1)}
    for i in range(m-1):
        s,e = [int(i) for i in input().split()]
        card_set = {i for i in range(s,e+1)}
        num_set = num_set & card_set
    
    print(len(num_set))