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:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n, m = readmi()
a = deque([0] )* (10 ** 5 + 2)
q = [readl() for _ in range(m)]
for i in q:
    a[i[0]] = 1
    a[i[1] + 1] = -1
for i in range(n + 1):
    a[i + 1] += a[i]
print(a.count(max(a)))

Submission status: Runtime Error

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n, m = readmi()
a = deque([0] )* (n + 2)
q = [readl() for _ in range(m)]
for i in q:
    a[i[0]] = 1
    a[i[1] + 1] = -1
for i in range(n + 1):
    a[i + 1] += a[i]
print(a.count(max(a)))

Submission status: Runtime Error

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

Nmin = 1
Nmax = N

for i in range(M):
    L, R = list(map(int,input().split()))
    Nmin = max(L,Nmin)
    Nmax = min(R,Nmax)

print(max(Nmax-Nmin+1),0)

Submission status: Runtime Error

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

l_list = [LR[i][0] for i in range(M)]
r_list = [LR[i][1] for i in range(M)]

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=0
b=n*10**9
A=True
for i in range(m):
  x,y=map(int,input().split())
    a=max(a,x)
    b=min(b,y)
if b>=a:
  ans=b-a+1
  print(ans)
else:
  print(0)

Submission status: Runtime Error

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n, m = readmi()
a = deque([0] )* (n + 2)
for i in range(m):
    x,y = readmi()
    a[x] = 1
    a[y + 1] = -1
for i in range(n + 1):
    a[i + 1] += a[i]
print(a.count(max(a)))

Submission status: Runtime Error

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

lefts = [0] * m
rights = [0] * m

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

lefts_max = max(lefts)
rithts_min = min(rights)

result = rights_min - lefts_max + 1

print(max(result,0))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
LR = []
length = []
for _ in range(M):
  l, r = map(int, input().split())
  LR.append([r-l, l, r])
LR = sorted(LR, key=lambda x:x[0])  
res = set([i for i in range(1, N + 1)])
for m in range(M):
  tmp = [i for i in range(max(LR[m][1], min(res)),min(LR[m][2] + 1, max(res) + 1))]
  res = set(tmp) & res
print(len(res))

Submission status: Runtime Error

Solution code:
mi = 1
ma = N

for i in range(M):
    if mi < List[i][0]:
        mi = List[i][0]
    if ma > List[i][1]:
        ma = List[i][1]

diff = ma - mi + 1
if diff < 0:
    diff = 0
print("{}".format(diff))

Submission status: Runtime Error

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

for i in int(m):
  av =[0,0]
  av[0] = max(int(g[i][0]),av[0])
  av[1] = min(int(g[i][1]),av[1])
print(av[1] -av[0]+1)

Submission status: Runtime Error

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

l_max = 1
r_min = n
for l, r in lrs:
    l_max = max(l, l_max)
    r_min = min(r, r_min)

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

Submission status: Runtime Error

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

low = 0
high = n
for i in range(0, m):
  tmp = input().split()
  if int(tmp[0]) > low:
    low = int(tmp[0])
  if int(tmp[1]) < high:
    high = int(tmp[1])

if high =< low:
  num = 0
else:
  num = high-low+1
print(num)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())    
                                    
x = []                              
for i in range(M):                  
    l,r = map(int, input().split()) 
    x.append([l, r])                
                                    
                                    
cand = list(range(1, N+1))          
                                    
for i, p in enumerate(x):           
    l,r = p                         
    if cand[0] < l:                 
        del cand[:(l - cand[0])]    
    if cand[-1] > r:                
        del cand[(r-cand[0]+1):]    
                                    
print(len(cand))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
K = []
F = 0
for i in range(M):
  A,B = map(int,input().split())
  F = max(A.F)
  K.append(B)
counter = 0
for i in range(len(K)):
  if (K[i]>=F):
    counter+=1
print(counter)

Submission status: Runtime Error

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

count = [m]

for i in range(m):
    l_, r_ = map(int, input().split())
    for j in range(r_-l_):
        count[l_+j-1] += 1

num = 0

for i in range(m):
    if count[i] == m:
       num+=1
print(num)

Submission status: Runtime Error

Solution code:
import numpy as np
n, m  = map(int, input().split())

cards = np.array([])
for i in range(m):
    a, b = map(int, input().split())
    cards = np.append(cards, np.arange(a, b+1))
    
count = np.unique(cards, return_counts=True)
ans = sum(count[1] == m)
print(ans)

Submission status: Runtime Error

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+1)
L_max = max(Ls)
R_min = min(Rs)
print(max(R_min - L_max, 0)

Submission status: Runtime Error

Solution code:
S = []
N, M = input().split(" ")
for m in range(len(int(M))):
  S.append(set([i for i in range(int(a),int(b)+1)]))
if M == 1:
  print(len(S[0]))
else:
  sets = S[0]
  for i in range(1,M):
    sets = sets & S[i]
  print(len(sets))

Submission status: Runtime Error

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

a = []
for i in range(M):
    l, r = map(int, input().split())
    a.append(list(range(l,r+1)))
b = a[0].copy()
for i in range(1, len(a[i])-1):
    b = list(set(b) & set(a[i]))
print(len(b))

Submission status: Runtime Error

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

a=li[0][0]
b=li[0][1]
for i in range(m-1):
  if li[i+1][0]>a:
    a=li[i+1][0]
  if li[i+1][1]<b:
    b=li[i+1][1]

if b<a:
  print(int(0))
  break
  
if b-a+1<n:
  print(b-a+1)
else:
  print(n)

Submission status: Runtime Error

Solution code:
from sys import exit

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

ans = []

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

    if i == 0:
        for card in range(L, R + 1):
            ans.append(card)
        continue

    if ans[0] < L:
        for card in range(ans[0], L):
            ans.remove(card)

    if R < ans[-1]:
        for card in range(R + 1, ans[-1] + 1):
            ans.remove(card)

    if not ans:
        print(0)
        exit()

print(len(ans))

Submission status: Runtime Error

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

for i in range(M):
    L,R = list(map(int,input().split()))
    if L == R:
        ans[L-1]+=1
    else:
        for j in range(L-1,R,1):
            ans[j]+=1

maxn = 0
for i in range(M):
    if ans[i] > maxn:
        maxn = ans[i]
        
for i in range(M):
    if ans[i] == maxn:
        anss += 1
print(anss)

Submission status: Runtime Error

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

for i in range(M):
    L,R = list(map(int,input().split()))
    if L == R:
      ans[L-1]+=1
    else:
      for j in range(L-1,R,1):
        ans[j]+=1

maxn = 0
for i in range(M):
    if ans[i] > maxn:
        maxn = ans[i]
        
for i in range(M):
    if ans[i] == maxn:
        anss += 1
print(anss)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

N,M = map(int, sys.stdin.readline().rstrip().split(" "))
Lmax, Rmin = 0, N
for _ in range(M):
    L, R = map(int, sys.stdin.readline().rstrip().split(" "))
    Lmax = max(L, Lmax)
    Rmin = min(R, Rmin)
assert Rmin >= Lmax
print(Rmin - Lmax + 1)
exit(0)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L, R = [], []
for _ in range(M):
  l, r = map(int, input().split())
  L.append(l)
  R.append(r)
res = set([i for i in range(1, N + 1)])
for m in range(M):
  tmp = [i for i in range(max(L[m], min(res)),min(R[m] + 1, max(res)))]
  res = set(tmp) & res
print(len(res))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,m = map(int, input().split())
n = range(1,n+1)
for i in range(m):
  a,b = map(int, input().split())
  n = [j for j in n if a <= j <= b]
print(len(j))

Submission status: Runtime Error

Solution code:

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


L=[]
R=[]
for i in range(m):
    l,r=map(int,input().split( ))
    L.append(l)
    R.append(r)

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

Submission status: Runtime Error

Solution code:

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


L=[]
R=[]
for i in range(m):
    l,r=map(int,input().split( ))
    L.append(l)
    R.append(r)

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

Submission status: Runtime Error

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

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

maxn = 0
for i in range(M):
    if ans[i] > maxn:
        maxn = ans[i]
        
print(maxn)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# IMPORT ###############################################################################################################
import sys                                                      # システム
sys.setrecursionlimit(10**6)                                    # 再帰処理の上限変更
from collections import deque, Counter                          # キュー・個数カウント
from itertools   import accumulate, permutations, combinations  # 累積和・順列・組み合わせ
from operator    import itemgetter                              # ソート補助
from bisect      import bisect_left, bisect_right, bisect       # 二分探索
from heapq       import heappop, heappush                       # プライオリティキュー
from fractions   import gcd                                     # 最大公約数
from math        import ceil, floor, sqrt, cos, sin, tan, pi    # 数学処理
from copy        import deepcopy                                # オブジェクトコピー
import numpy         as np                                      # 数値計算

# MAIN #################################################################################################################
def main():
    # Init #############################################################################################################
    im = InputManager()
    # Input ############################################################################################################
    n, m = im.inputMultiple()
    gates = np.zeros((n, m), dtype=np.uint8)
    for i in range(m):
        l, r = im.inputMultiple()
        gates[l-1:r, i] = 1
    # Logic ############################################################################################################
    count = 0
    for idx, gate in enumerate(gates):
        if 0 not in gate:
            count = count + 1
    # Output ###########################################################################################################
    print(count)
    # Return ###########################################################################################################
    return

# INPUT MANAGER ########################################################################################################
class InputManager(object):
    def __init__(self):
        pass

    @staticmethod
    def inputSingle(dtype=int):
        return dtype(input())
    
    @staticmethod
    def inputMultiple(dtype=int, aslist=False):
        if aslist:
            return list(map(dtype, input().split()))
        else:
            return map(dtype, input().split())

    @staticmethod
    def input2D(h, dtype=int):
        return [list(map(dtype, input().split())) for i in range(h)]

# CALL MAIN ############################################################################################################
main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,m = input().split()
n = int(n)
m = int(m)
l = 0
r = n+1
a = input().split()
for i in range(m):
    l2,r2 = input().split()
    l2 = int(l2)
    r2 = int(r2)
    if l2 > l:
        l = l2
    if r2 < r:
        r = r2
if (r - l + 1) >= 0:
    print(r-l+1)
else:
    print("0")

Submission status: Runtime Error

Solution code:
import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;

public class Main {

  public static void main(String[] args) {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    Scanner sc = new Scanner(is);

    /* read */
    int n = sc.nextInt();
    int m = sc.nextInt();

    int[] l = new int[m];
    int[] r = new int[m];

    int lMax = Integer.MIN_VALUE;
    int rMin = Integer.MAX_VALUE;
    for (int i = 0; i < m; i++) {
      l[i] = sc.nextInt();
      r[i] = sc.nextInt();
      lMax = Math.max(l[i], lMax);
      rMin = Math.min(r[i], rMin);
    }
    os.println(rMin - lMax + 1);
  }

}

Submission status: Runtime Error

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

Solution code:

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,M = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(M)]
hoge = set()

zenbu = {int(i + 1) for i in range(N)}

for i in e:
    konkaino = {int(i) for i in range(e[0],e[1] + 1)}
    zenbu = zenbu & konkaino


print(len(zenbu))

Submission status: Runtime Error

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, input().split()))
    l = max(a, l)
    r = min(b, r)
print(max(0, r - l + 1))