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: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
m = set(range(N))
for i in range(M):
	L, R = map(int, input().split())
	_m = set(range(L-1, R))
	m = m & _m
print(len(m))

Submission status: Time Limit Exceeded

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
ll=[]
for i in range(b):
    c,d=input().split()
    c=int(c)
    d=int(d)
    for j in range(c,d+1):
        ll.append(j)
count=0
for i in range(1,a+1):
    k=ll.count(i)
    if k==b:
        count+=1
print(count)

Submission status: Time Limit Exceeded

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

L1,R1 = map(int,input().split())
through = set(range(L1,R1+1))
for i in range(M-1):
    L,R = map(int,input().split())
    through = through & set(range(L,R+1))
print(len(through))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

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

    L = []
    R = []
    id = list(range(N+1))
    id_set = set(id)
    for _ in range(M):
        if len(id) == 0:
            break
        l, r = list(map(int, input().split()))
        id_set = id_set & set(id[l:r+1])
    print(len(id_set))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, M = (int(x) for x in input().split())
L, R = [None]*M, [None]*M
for i in range(M):
    L[i], R[i] = (int(x) for x in input().split())

tmp = set(range(L[0], R[0]+1))
for j in range(1, M):
    tmp = tmp & set(range(L[j], R[j]+1))

print(len(tmp))

Submission status: Time Limit Exceeded

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

stepper = 0

gate = []

for j in range(M):
    gate_1, gate_2 = map(int, input().split())
    gate.append([gate_1, gate_2])

for i in range(N):
    i += 1
    counter = 0
    for k in gate:
        #print(k, i)
        if k[0] <= i <= k[1]:
            counter += 1
    #print(counter, M)
    if counter == M:
        stepper += 1

print(stepper)

Submission status: Time Limit Exceeded

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

LR = []
for _ in range(M):
    l, r = map(int, input().split())
    LR.append((l, r))
    
cards = LR[:]
master_ids = set(range(cards[0][0], cards[0][1]+1))
for l, r in cards:
    ids = set(range(l, r+1))
    master_ids = master_ids & ids
    if not master_ids:
        break
    
ans = len(master_ids)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n,m = Is()
s = set(range(1,n+1))
for i in range(m):
    l,r = Is()
    s = s & set(range(l,r+1))
print(len(s))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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())
count = 0
for i in range(n):
  for j in range(m):
    if (i+1 < l[j]) or (i+1 > r[j]):
      break
  else:
    count = count + 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split(' '))
s = []
for i in range(m):
    l, r = map(int, input().split(' '))
    a = set(j for j in range(l, r+1))
    s.append(a)
result = s[0]
for i in range(len(s)-1):
    result = result & s[i+1]
print(len(list(result)))

Submission status: Time Limit Exceeded

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

set_ID = {i for i in range(N)}

for _ in range(M):
    L, R = map(int, input().split())
    set_M = {i for i in range(L - 1, R)}
    set_ID = set_ID & set_M
    
print(len(set_ID))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
set1=set()
set2=set()
L,R=map(int,input().split())
for i in range(L,R+1):
    set1.add(i)
for i in range(M-1):
    L,R=map(int,input().split())
    for j in range(L,R+1):
        set2.add(j)
    set1=set1&set2
    set2.clear()
print(len(set1))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
id = [x for x in range(1, n + 1)]
for i in range(m):
    l, r = map(int, input().split())
    id = [x for x in id if l <= x <= r]
print(len(id))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
n, m = map(int, input().split())
l, r = 10**6, 0
ll = []
for i in range(m):
    a, b = map(int, input().split())
    ll.append([a, b])
    l = min(l, a)
    r = max(r, b)
ans = 0
for i in range(l, r+1):
    cnt = 0
    for j in range(len(ll)):
        if ll[j][0] <= i <= ll[j][1]:
            cnt += 1
    if cnt == len(ll):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = [0] * (N + 1)

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

for i in range(M):
  for j in range(L[i], R[i] + 1):
    ans[j] += 1


print(ans.count(M))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, k = map(int, input().split())
val = [map(int, input().split()) for _ in range(k)]
cand = set(np.arange(1,N+1))
for v in val:
    l,ll = v
    cand = cand.intersection(set(np.arange(l, ll+1)))
print(len(cand))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
keys = set()
for i in range(1, N+1):
    keys.add(i)
for _ in range(M):
    akey = set()
    l, r = map(int, input().split())
    for i in range(l, r+1):
        akey.add(i)
    keys = keys & akey
print(len(keys))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
gate=[input() for __ in range(M)]
s,e=map(int,gate[0].split())

ans=set([i for i in range(s,e+1)])

for g in gate:
    s,e=map(int,g.split())
    ans=set(ans)
    pre=set([i for i in range(s,e+1)])
    ans = list(ans & pre)

print(len(ans))

Submission status: Time Limit Exceeded

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

L, R = map(int, input().split())
hoge = list(range(L, R+1))
hoge = set(hoge)

for i in range(1, M):
    L, R = map(int, input().split())
    fuga = list(range(L, R+1))
    hoge = hoge & set(fuga)

print(len(hoge))

Submission status: Time Limit Exceeded

Solution code:
#入力取得
N, M = map(int, input().split())


gate = []
count = 0

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


for i in range(N):
    #i+1で考える？
    for row in gate:
        if row[0] <= i+1 and row[1] >= i+1:
            flag = 1
        else:
            flag = 0
            break
    if flag ==1:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
ls = [[]]*M
for p in range(M):
    ls[p] = input().split()
ans = 0
for q in range(1,N+1):
    an = True
    for a,b in ls:
        if an:
            if int(a) <= q <= int(b):
                an = True
            else:
                an = False
    if an:
       ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = set(range(1, n+1))
for i in range(m):
    ans = ans & set(lr[i])
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
ans = 0
for i in range(m):
    l,r = map(int, input().split())
    tmp = set(range(l,r+1))
    if i == 0:
        ans = tmp
    else:
        ans = ans & tmp

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
l,r=map(int,input().split())
s={i for i in range(l,r+1)}
for _ in range(M-1):
    l, r = map(int, input().split())
    t = {i for i in range(l, r + 1)}
    s = s & t
    if len(s) == 0:
        print(0)
        exit(0)
print(len(s))

Submission status: Time Limit Exceeded

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

a, b = [int(x) for x in input().split()]
s = set(range(a, b + 1))
for _ in range(M-1):
  a, b = [int(x) for x in input().split()]
  temp = set(range(a, b + 1))
  s = s & temp
print(len(s))

Submission status: Time Limit Exceeded

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

id = [True] * (N+1)

for i in range(M):
    L, R = map(int, input().split())
    for j in range(1, L):
        id[j] = False
    for j in range(R+1, N+1):
        id[j] = False

cnt = 0
for i in range(1, N+1):
    if id[i] == True:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
A = [0] * (N+1)
for i in range(M):
    L,R = map(int,input().split())
    for i in range(L,R+1):
        A[i] += 1
print(sum([1 for a in A if a == M]))

Submission status: Time Limit Exceeded

Solution code:
li = input().split()
n = int(li[0])
m = int(li[1])
# IDカードn枚，　ゲートm個
l_r = []
# i番目のゲートはl~rのIDカードを持っていれば通過できる
for i in range(m):
    l_r.append(input().split())
answer = 0
for i in range(n):
    for lr in l_r:
        l = int(lr[0])
        r = int(lr[1])
        if l <= i + 1 and i + 1 <= r:
            continue
        else:
            break
    else:
        answer += 1
print(answer)

Submission status: Time Limit Exceeded

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

set_ = set()
for m in range(M):
    s, e = map(int, input().strip().split())
    tmp = set()
    for i in range(s, e+1):
        tmp.add(i)

    if m == 0:
        set_ = tmp
    else:
        set_ = set_ & tmp

print(len(set_))

Submission status: Time Limit Exceeded

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

ans = set(range(lr[0][1] + 1)) - set(range(lr[0][0]))
for l, r in lr:
    ans &= (set(range(r + 1)) - set(range(l)))
print(len(ans) if n > len(ans) else n)

Submission status: Time Limit Exceeded

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

master_card = set()
L0, R0 = map(int, input().split())
master_card |= set(range(L0, R0 + 1))

for i in range(1, M):
    Li, Ri = map(int, input().split())
    master_card &= set(range(Li, Ri + 1))

print(len(master_card))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
ans=[1 for i in range(n)]
for i in range(m):
	l,r=map(int,input().split())
	for j in range(n):
		if ans[j]==1:
			if j<l-1 or j>r-1:
				ans[j]=0
print(ans.count(1))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().rstrip().split()]
    l,r = [int(x) for x in stdin.readline().rstrip().split()]
    ans = set(range(l,r+1))

    for i in range(m-1):
        l,r = [int(x) for x in stdin.readline().rstrip().split()]
        cards = set(range(l,r+1))
        ans = ans & cards

    print(len(ans))
    
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
l, m = map(int, input().split())
ans = set(list(range(l, m+1)))
for i in range(M-1):
    l, m = map(int, input().split())
    ans = ans & set(list(range(l, m+1)))
print(len(ans))

Submission status: Time Limit Exceeded

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

LR = []
for _ in range(M):
    l, r = map(int, input().split())
    LR.append((l, r))
    
cards = LR[:]
master_ids = set(range(cards[0][0], cards[0][1]+1))
for l, r in cards:
    ids = set(range(l, r+1))
    master_ids = master_ids & ids
    #print(ids, master_ids)
    
ans = len(master_ids)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
a, b = map(int, input().split())
A = [0] * a
A = np.array(A)

for i in range(b):
    c, d = map(int , input().split())
    A[c-1 : d]  += 1

print(np.sum(A == b))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l1, r1 = map(int, input().split())
ans = set(range(l1, r1+1))
for  i in range(2, m+1):
    l, r = map(int, input().split())
    can_pass = set(range(l, r+1))
    ans = ans.intersection(can_pass)
    if len(ans) == 0:
        break
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M = [int(n) for n in input().split()]
A = [1 for _ in range(N)]

for _ in range(M):
    L, R = [int(n) for n in input().split()]
    for i in range(0, L-1, 1):
        A[i] = 0
    for i in range(R, N, 1):
        A[i] = 0

cnt = 0
for i in range(N):
    if A[i] == 1:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
A = [1]*N
for i in range(M):
    L, R = map(int,input().split())
    for j in range(0,L-1):
        A[j] = 0
    for j in range(R,N):
        A[j] =0
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 

N, M = [ int(x) for x in input().split() ]

nm = []
for _i in range(M):
    nm.append([ int(x) for x in input().split() ])

ans = { x: 1 for x in range(1, N+1) }

for _nm in nm:
    nums = range(_nm[0], _nm[1] + 1)
    new_ans = {}
    for n in nums:
        if n in ans:
            new_ans[n] = 1
    ans = new_ans

print(len(ans.keys()))

Submission status: Time Limit Exceeded

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

    ln[i]=list(range(ln[i][0],ln[i][1]+1))

nxl=ln[0]
for i in range(1,m):
    nxl=set(nxl)&set(ln[i])
print(len(list(nxl)))

Submission status: Time Limit Exceeded

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

LL, MM = map(int, input().split())
gate = [LL + i for i in range(MM-LL+1)]

for _ in range(M-1):
    LL, MM = map(int, input().split())
    tmp = [LL + i for i in range(MM-LL+1)]
    gate = list(set(gate) & set(tmp))

print(len(gate))

Submission status: Time Limit Exceeded

Solution code:
n ,m = map(int,input().split())
s = set()
for i in range(m):
    l,r = map(int, input().split())
    tmp = {i for i in range(l,r+1)}
    if not s:
        s = s | tmp
        continue
    s = s&tmp
print(len(s))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
ans = set(range(1,n+1))
for i in range(m):
    l,r = map(int, input().split())
    tmp = set(range(l,r+1))
    ans = ans & tmp

print(len(ans))

Submission status: Time Limit Exceeded

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

num = [0] * N

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

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, M = map(int,input().split())
A = np.zeros(N+1)
for i in range(M):
    L,R = map(int,input().split())
    A[L:R+1] += 1
print(sum([1 for a in A if a == M]))

Submission status: Time Limit Exceeded

Solution code:
N,M = [int(i) for i in input().split()]
ans = [0]*N
for _ in range(M):
    L,R = [int(i)-1 for i in input().split()]
    for j in range(L,R+1):
        ans[j] += 1

count = 0
for i in ans:
    if i == M:
        count += 1

print(count)

Submission status: Time Limit Exceeded

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

tmp = [0] * n

def add_one(x):
    return x + 1

for i in range(m):
    l = lr[i][0] - 1
    r = lr[i][1] - 1
    tmp[l: r+1] = list(map(add_one, tmp[l: r + 1]))

counter = 0
for j in range(n):
    if tmp[j] == m:
        counter += 1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
N, M = map(int, input().split())
l1, r1 = map(int, input().split())
ans = [i for i in range(l1, r1+1)]
for m in range(M-1):
    l1, r1 = map(int, input().split())
    l = [i for i in range(l1, r1+1)]
    ans = list(set(ans) & set(l))
print(len(ans))

Submission status: Time Limit Exceeded

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

s = set()
for i in range(M):
  L, R = map(int, input().split())
  tmp = set()
  for j in range(L, R + 1):
    tmp |= {j}
  if i == 0:
    s |= tmp
  else:
    s &= tmp

print(len(s))

Submission status: Time Limit Exceeded

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

Ls = [0] * M
Rs = [0] * M

for i in range(M):
    Ls[i], Rs[i] = list(map(int, input().split()))

count = 0
for i in range(N):
    judge = True
    for k in range(M):
        L = Ls[k]
        R = Rs[k]
        if not (L <= i + 1 <= R):
            judge = False
    if judge:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
ans = set(range(1, n + 1))
for _ in range(m):
    l, r = map(int, input().split())
    st = set(range(l, r + 1))
    ans &= st
print(len(ans))

Submission status: Time Limit Exceeded

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

answer=0
for i in range(N+1):
    ok=1
    for l,r in gate:
        if l<=i and i<=r:
            pass
        else:
            ok=0
            break
    if ok==1:
        answer+=1
print(answer)

Submission status: Time Limit Exceeded

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

print(np.sum(count_list==M))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
ans = set(range(1,n+1))
for i in range(m):
  l, r = map(int, input().split())
  x = set(range(l, r+1))
  ans = ans & x
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


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

cards = [0] * (N + 1)

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

    for j in range(L, R + 1):
        cards[j] += 1

print(cards.count(M))

Submission status: Time Limit Exceeded

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

for i in range(M):
    r, m = list(map(int, input().split()))
    for j in range(r, m+1):
        gate[j] += 1

print(sum([1 for i in gate if i == M]))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l1, r1 = map(int, input().split())
ans = set(range(l1, r1+1))
for  i in range(2, m+1):
    l, r = map(int, input().split())
    can_pass = set(range(l, r+1))
    ans = ans.intersection(can_pass)

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

n,m=intinput()
x=np.arange(1,n+1)
z=np.ones(n,bool)
for i in range(m):
    a,b=intinput()
    y = np.logical_and(a<=x,x<=b)
    z = np.logical_and(y,z)
print(np.sum(z))

Submission status: Time Limit Exceeded

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

import numpy as np

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

x = np.zeros(N, dtype=int)
for _ in range(M):
    l, r = map(int, input().rstrip().split(' '))
    x[:l-1] = 1
    x[r:] = 1
    
print(N - x.sum())

Submission status: Time Limit Exceeded

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

import numpy as np
tmp = np.zeros(n)

for i in range(m):
    l = lr[i][0] - 1
    r = lr[i][1] - 1
    tmp[l: r+1] = tmp[l: r+1] + 1

counter = 0
for j in range(n):
    if tmp[j] == m:
        counter += 1

print(counter)

Submission status: Time Limit Exceeded

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

l = [0] * (n + 1)
s = 0

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

for k in range(n + 1):
    if l[k] == m:
        s += 1
print(s)

Submission status: Time Limit Exceeded

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(i[0],i[1] + 1)}
    zenbu = zenbu & konkaino


print(len(zenbu))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


class Solution:
    def main(self):
        N, M = map(int, input().split())
        L = []
        R = []
        cards = set(list(range(1, N+1)))
        for _ in range(M):
            l, r = map(int, input().split())
            cands = set(list(range(l, r+1)))
            cards = cards & cands
        print(len(cards))


if __name__ == '__main__':
    Solution().main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
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 II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, M = LI()
    LR = []
    for _ in range(M):
        LR.append(LI())
    SE = None
    for l, r in LR:
        se = set(range(l, r + 1))
        if SE is None:
            SE = se
        else:
            SE = SE & se
    ans = len(SE)
    return ans

print(main())

Submission status: Time Limit Exceeded

Solution code:
N,M=list(map(int,input().split()))
ans=set([i for i in range(1,N+1)])
for i in range(M):
    a,b=list(map(int,input().split()))
    tmp=set([i for i in range(a,b+1)])
    ans &=tmp
print(len(ans))

Submission status: Time Limit Exceeded

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

import numpy as np
a = np.zeros(N)

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

print(len(a[a==M]))

Submission status: Accepted

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

minv = gate[0][0]
maxv = gate[0][1]

for x in gate:
  minv = max(minv,x[0])
  maxv = min(maxv,x[1])
  
print(maxv-minv+1 if maxv-minv+1 >= 1 else 0)

Submission status: Accepted

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)

# 共通部分を求め、共通部分が無ければ0を出力する
m = max(L)
M = min(R)
if M >= m: print(M-m+1)
else: print(0)

Submission status: Accepted

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)

m = max(L)
M = min(R)
if m <= M: print(M-m+1)
else: print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mn,mx=-1,10**7
for _ in range(m):
  l,r= map(int,input().split())
  mn=max(mn,l)
  mx=min(mx,r)
ans=max(mx-mn+1,0)
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = []
R = []
for _ in range(M):
    a,b = map(int,input().split())
    L.append(a)
    R.append(b)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
left = set()
right = set()
for i in range(m):
  l,r = map(int, input().split())
  left.add(l)
  right.add(r)
mini = max(left)
maxi = min(right)
print(len(range(mini,maxi+1)))

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
common_l, common_r = 0, N
for l, r in LR:
    common_l = max(l, common_l)
    common_r = min(r, common_r)
print(max(0, common_r-common_l+1))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lower = -float('inf')
upper = float('inf')
for i in range(m):
    l,r = map(int,input().split())
    if l > lower:
        lower = l
    if r < upper:
        upper = r
    if upper < lower:
        upper = 0
        lower = 0
        break
if upper == lower == 0:
    print(0)
else:
    print(upper - lower + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l_max = 0
r_min = n

for _ in range(m):
    x, y = map(int, input().split())
    l_max = max(l_max, x)
    r_min = min(r_min, y)

print(len([i for i in range(l_max, r_min + 1)]))

Submission status: Accepted

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

min_i = 1
max_i = n

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

if max_i-min_i>=0:
    print(max_i-min_i+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L,R=map(int,input().split())
for i in range(M-1):
    H,I=map(int,input().split())
    if H>L:
        L=H
    if R>I:
        R=I
    if R<L:
        break
if R<L:
    print(0)
else:
    ans=list(range(L,R+1))
    print(len(ans))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L_lis = []
R_lis = []
for i in range(M):
  L, R = map(int, input().split())
  L_lis.append(L)
  R_lis.append(R)
new_L, new_R = max(L_lis), min(R_lis)
if new_L <= new_R:
  print(new_R - new_L + 1)
else:
  print(0)

Submission status: Accepted

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

ans = min(R) - max(L) + 1
if ans < 0: ans = 0
print(ans)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

N, M = I()
count = 0
 
min_a = 1
max_b = N
 
for i in range(M):
    a,b = [int(i) for i in input().split()]
    if min_a < a:
        min_a = a
    if max_b > b:
        max_b = b
 
print(max(0, max_b-min_a+1))

Submission status: Accepted

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

max_l, min_r = 0, float("inf")
for i in range(m):
  max_l = max(s[i][0], max_l)
  min_r = min(s[i][1], min_r)
  
print(max(min_r - max_l + 1, 0))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
lmax,rmin=1,n
for _ 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())
start = 1
end = n
for i in range(m):
    x, y = map(int,input().split())
    start = max(start, x)
    end = min(end, y)
print(end-start+1 if end-start >=0 else 0)

Submission status: Accepted

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

Submission status: Accepted

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

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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, *lr = list(map(int, open(0).read().split()))
print(max(0, min(lr[1::2]) - max(lr[::2]) + 1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L, R = [0]*M, [0]*M
A = set(range(N))
lm, rm = 0, N
for i in range(M):
    L[i], R[i] = map(int, input().split())
    lm = max(lm, L[i])
    rm = min(rm, R[i])
if lm <= rm:
    print(rm-lm+1)
else:
    print(0)