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 -*-
l = []
r = []
all_gate_clear = 0
card_count = []

#numcard
N = input()
#gatenum
M = input()
#idカードの生成
for i in range(M):
    l[i],r[i] = map(int, input().split())

    
    for gate_num in range(1,M+1):
        #1ゲートの処理
        for card in range(1,N+1):
            for num in range(r[gate_num]+ 1):
                if l[gate_num] + num == card:
                    if card_count[card] == gate_num -1:
                        card_count[card] += 1
                        break

    for card in range(1,N+1):
        if card_count[card] == M:
            all_gate_clear += 1

print(all_gate_clear)

Submission status: Runtime Error

Solution code:
import sys

n, m = map(int, input().split())
nums = []

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
    nums = list(filter(lambda x: l <= x <= r, nums))
    if len(nums) == 0:
        print(0)
	sys.exit()

print(len(nums))

Submission status: Runtime Error

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

answer = []
tmp = []

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

for i in range(M-1):
    L, R = map(int, input().split())
    for j in range(L, R+1):
        if j in answer:
            tmp.append(j)
        elif j > answer[-1]:
            continue
    del answer 
    answer = tmp
    del tmp
    tmp = []

print(len(answer))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
x = []
for i in range(M):
  a,b = map(int, input().split())
  x.extend(range(a,b+1))


import collections
counter = collections.Counter(x)
freqCheck = counter.most_common()
 
mode = [freqCheck[0][0]] # 最頻値
maxCount = freqCheck[0][1] # 最頻値の出現回数
 
# 最頻値の出現回数が同じ数字は最頻値とする
count = 1
while maxCount == freqCheck[count][1]:
    mode.append(freqCheck[count][0])
    count = count + 1
print(len(mode))

Submission status: Runtime Error

Solution code:
n, m=map(int, input().split())
box=[[]]*m
for i in range(0,m):
  l,r=map(int, input().split())
  box[i]=list(range(l, r+1))
  if (i>0):
    ans = list(set(box[i]) & set(box[i-1]))
print(len(ans))

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
N,M = [int(x) for x in input().split()]
idls =[]
for i in range(N):
    idls.append(i+1)
ans =[]
for i in range (M):
    L,R = [int(x) for x in input().split()]
    iidls = idls[L-1:R]
    ans+=iidls
c= collections.Counter(ans)

a = c.most_common()

counter = 0
count = 0
while True:
    if a[counter][1] == M:
        count+=1
        counter +=1
        if counter == N:
            break
    else:
        break

print(count)

Submission status: Runtime Error

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

# 1個目を受け取る
l, r = map(int, input().split())


def check(i, l, r):
    if i == m:
        print(r - l + 1)
        exit()
    l_, r_ = map(int, input().split())
    nl = max(l, l_)
    nr = min(r, r_)
    i += 1
    check(i, nl, nr)


check(1, l, r)

Submission status: Runtime Error

Solution code:
N, M= map(int, input().split())
L, R = map(int, input().split())
card = [L, R+1]
count = 0
if M == 1:
    card = list(range(1,N+1))
    m = list(range(L, R+1))
    passcard = set(card) & set(m)
    card = passcard
    print(len(passcard))
    
else:
    for i in range(M-1):
        L, R = map(int, input().split())
        m = list(range(L, R+1))
        passcard = set(card) & set(m)
        passcard = list(passcard)
        card = passcard
        if card[-1] == N:
            print(len(passcard)+1)
        else:    
            print(len(passcard))

Submission status: Runtime Error

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

l = [""] * m

for i in range(m):
    l[i] = list(map(int,input().split()))
ans3 = set([0,n+1])

for i in range(m):
    ans2 = [j for j in range(min(ans3),max(ans3)+1) if l[i][0] <= j <= l[i][1]]
    if i == 0:
        ans3 = set(ans2)
    else:
        ans3 = ans3 & set(ans2)
    ans2.clear()
    
print(len(ans3))

Submission status: Runtime Error

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

As = list(map(int,input().split())).sort()

Bs = []
Cs = []
for i in range(M):
	inputs = list(map(int,input().split()))
	Bs.append(inputs[0])
	Cs.append(inputs[1])

for j in range(1, M + 1):
	for n in range(Bs[j - 1]):
		if As[n] < Cs[j - 1]:
			As[n] = Cs[j - 1]
	As.sort()

print(sum(As))

Submission status: Runtime Error

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

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
    nums = list(filter(lambda x: l <= x <= r, nums))
    if len(nums) == 0:
	print(0)
	break

print(len(nums))

Submission status: Runtime Error

Solution code:
N, M = map(int,input().split())
a, b = map(int,input().split())
l = list(range(a,b+1))  
for i in range(M-1):
  l2 = []
  c, d = map(int,input().split())
  l1 = list(range(c,d+1))  
  for j in l1:
    if j in l:
      l2.append(j)
  l = l2
print(len(l2))

Submission status: Runtime Error

Solution code:
import numpy as np
d = input().split(" ")
id_list = np.arange(1,int(d[0])+1)

for i in range(int(d[1])):
  tmp = input().split(" ")
  id_list = id_list[id_list==np.arange(int(tmp[0]),int(tmp[1])+1)]
  
print(len(id_list))

Submission status: Runtime Error

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

l = [""] * m

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

ans3 = [i for i in range(1,n+1)]
ans3 = set(ans3)


for i in range(m):
    ans2 = [j for j in range(min(ans3),max(ans3)+1) if l[i][0] <= j <= l[i][1]]
    ans3 = ans3 & set(ans2)
    ans2.clear()
    
print(len(ans3))

Submission status: Runtime Error

Solution code:
    a, b = map(int, input().split())
    list_a = list(range(1, a, 1))
    for num in range(b):
        c, d = map(int, input().split())
        for item in list_a:
            if item <= c and  item >= d:
                list_a.remove(item)
                
    print(len(list_a))

Submission status: Runtime Error

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

data = []
l = 1
r = 10**5
for m in range(M):
    li, ri = map(int, input().split(' '))
    if li > l:
      l = li
    if ri < r:
      r = ri

if l<= N <= r:
  cnt = (N - l) + 1
elif r >= N:
  cnt = (r - l) + 1
else N < l:
  cnt = 0

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N,M,L,R;
  int Lmax = 0;
  int Rmin = 1000000;
  cin >> N >> M;
  for(int i=0;i<M;i++){
    cin >> L >> R;
    if(L>=Lmax){
      Lmax = L;
    }
    if(R<=Rmin){
      Rmin = R;
    }
  }
  if(Rmin > Lmax){
      cout << Rmin-Lmax+1 << endl;
  }
  else{
    cout << "0" << endl;
  }
  return 0;
}

Submission status: Runtime Error

Solution code:
n ,m = map(int, input().split())
result = set()
first = True
for i in range(m):
    l, r = map(int, input().split())
    tmp = set()
    for i in range(l, r + 1):
        tmp.add(i)
    if first:
        result = tmp
        # print("tmp {}".format(tmp))
        first = False
    else:
        result = result.intersection(tmp)
        # print("result {} tmp {}".format(result, tmp))

print(len(result))n ,m = map(int, input().split())
result = set()
first = True
for i in range(m):
    l, r = map(int, input().split())
    tmp = set()
    for i in range(l, r + 1):
        tmp.add(i)
    if first:
        result = tmp
        first = False
    else:
        result = result.intersection(tmp)
print(len(result))

Submission status: Runtime Error

Solution code:
 N, M= (int(i) for i in input().split())

 ans = set(range(1,N+1))

 for i in range(M):
     L_tmp, R_tmp = map(int, input().split())
     ans = ans & set(range(L_tmp,R_tmp+1))

 print(len(ans))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
for i in range(m):
    b,c = map(int,input().split())
    a.extend([c] * b)
a.sort(reverse=True)
print(sum(a[:n]))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
ans = 0
b = []
while m != 0:
    l, r = map(int, input().split())
    if not b:
        for a in range(1, n + 1):
            c = a
            print(b)
            while True:
                if a >= l:
                    if a <= r:
                        b.append(c)
                        break
                    else:
                        break
                else:
                    break
    elif m != 1:
        for a in b:
            print(b)
            c = a
            while True:
                if a >= l:
                    if a <= r:
                        ans += 1
                        break
                    else:
                        b.remove(c)
                        break
                else:
                    b.remove(c)
                    break
    else:
        for a in b:
            print(b)
            c = a
            while True:
                if a == n:
                    ans += 1
                    break
                if a >= l:
                    if a <= r:
                        a += 1
                    else:
                        break
                else:
                    break
    m -= 1

print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int, input().split())
listA = [list(map(int,input().split())) for i in range(m)]
setA = {1}
for h in range(2,n+1):
    setA.add(h)
for j in range(m):
    for k in range(listA[j][0]+1, listA[j][1]):
        listA[j]=set(listA[j])
        listA[j].add(k)
for l in range(m):
    setA=setA & listA[l]
print(len(setA))

Submission status: Runtime Error

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

for i in range(M):
    L,R=map(int,input().split())
    if A[0]>=L:
        start=0
    else:
        start=A.index(L)
    if A[-1]<=R:
        end=len(A)+1
    else:
        end=A.index(R)+1
    A=A[start:end]
    
print(len(A))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
ans = 0
b = []
while m != 0:
    l, r = map(int, input().split())
    if not b:
        for a in range(1, n + 1):
            c = a
            print(b)
            while True:
                if a >= l:
                    if a <= r:
                        b.append(c)
                        break
                    else:
                        break
                else:
                    break
    elif m != 1:
        for a in b:
            print(b)
            c = a
            while True:
                if a >= l:
                    if a <= r:
                        ans += 1
                    else:
                        b.remove(c)
                        break
                else:
                    b.remove(c)
                    break
    else:
        for a in b:
            print(b)
            c = a
            while True:
                if a == n:
                    ans += 1
                    break
                if a >= l:
                    if a <= r:
                        a += 1
                    else:
                        break
                else:
                    break
    m -= 1

print(ans)

Submission status: Runtime Error

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

mi, mx = map(int, input().split())
for i in range(m-1):
    l, r = map(int, input().split())
    if mi < l <= mx:
        mi = l
    if mi <= r < mx:
        mx = r
    if mx < l or r < mi:
        flag = True
        break

if flag:
    print(0)
else:
    print(mx - mi + 1)

Submission status: Runtime Error

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

max = 0
min = 0

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

    if min < L:
        min = L
    elif min == 0:
        min = L

    if max > R:
        max = R
    elif max =+ 0:
        max = R

ans = max - min + 1

print(ans)

Submission status: Runtime Error

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

answer = []
tmp = []

L, R = map(int, input().split())
for i in range(L,R+1):
    answer.append(i)

for i in range(M-1):
    L, R = map(int, input().split())
    for j in range(L, R+1):
        if j in answer:
            tmp.append(j)
        elif j > answer[-1]:
          continue
    answer = tmp
    tmp = []

print(len(answer))

Submission status: Runtime Error

Solution code:
import sys
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
gates = []
for gate in args[1:]:
  l, r = map(int, gate.split(' '))
  gates.append((l, r))
  
ids = gates[0]
for gate in gates[1:]:
  l, r = gate
  ids = ids[l-ids[0]:r-ids[-1]+1]
  
print(len(ids))

Submission status: Runtime Error

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

n_left = 1
n_right = n
result = 0

for i in range(m):
    l_i,r_i = [int(i) for i in input().split()]
    if(l_i > n_left):
        n_left = l_i
    if(r_i < n_right):
        n_right = r_i
    if(n_left > n_right):
        print(0)
        sys.exit(0)

print(n_right - n_left + 1)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
L,H=map(int,input().split())
c=0

while c<M-1:
    l,h=map(int,input().split())
    if L<=l<=H:
        L=l
    elif l>H:
        print(0)
        continue
    if L<h<H:
        H=h
    c+=1
print(H-L+1)

Submission status: Runtime Error

Solution code:
N, M = [int(i) for i in input().split()]
if M == 1:
    L, R = [int(j) for j in input().split()]
    print(R - L + 1)
else:
    for i in range(M):
        if i == 0:
            L, R = [int(j) for j in input().split()]
            l1 = set([int(l) for l in range(L,R+1)])
        else:
            L, R = [int(j) for j in input().split()]
            l2 = set([int(k) for k in range(L,R+1)])
            mix = l1 & l2
            l2 = l1
print(len(mix))

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.split() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N, M = makeMultiInteger(input_string[0])
    S = makeIntMatrix(input_string[1:])
    c = 0
    ra = range(1, N+1)
        
    min_range = ra
    for s in S:
        l = s[0]
        r = s[1]
        if l >= min_range[0]:
            min_l = l
        if r <= min_range[-1]:
            min_r = r
        min_range = range(min_l, min_r+1)
    #print(min_range)
    if ra[-1] > min_range[-1]:
        return len(min_range)
    else:
        return len(ra)



def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

answer = []
tmp = []

L, R = map(int, input().split())
for i in range(L,R+1):
    answer.append(i)

for i in range(M-1):
    L, R = map(int, input().split())
    for j in range(L, R+1):
        if j in answer:
            tmp.append(j)
        elif j > answer[-1]:
          answer = tmp
          tmp = []
          continue
    answer = tmp
    tmp = []

print(len(answer))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
list = []
for i in range(M):
    L, R = map(int, input().split())
    list.append([L,R])
anslist = [list[0][0],list[0][1]]
arukadouka = True
if M = 1:
    arukadouka = False
cnt = 0
for i in range(1,M):
    if arukadouka == True:
        if anslist[0] > list[i][1]:
            arukadouka = False
        elif anslist[1] < list[i][0]:
            arukadouka = False
        else:
            if (anslist[0]<=list[i][0]) and (anslist[1]<=list[i][1]):
                anslist[0] = list[i][0]
            elif (anslist[0]<=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[0] = list[i][0]
                anslist[1] = list[i][1]
            elif (anslist[0]>=list[i][0]) and (anslist[1]<=list[i][1]):
                pass
            elif (anslist[0]>=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[1] = list[i][1]
        cnt += 1
print(anslist[1]-anslist[0]+1)

Submission status: Runtime Error

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

max = 0
min = 0

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

    if min < L:
        min = L
    elif min == 0:
        min = L

    if max > R:
        max = R
    elif max =+ 0:
        max = R

ans = max - min + 1

print(ans)

Submission status: Runtime Error

Solution code:
isstr=input()
n,m = [int(i) for i in isstr.split(' ')]
l=[0]*m
r=[0]*m
for i in range(m):
    l[i],r[i]=[int(i) for i in input().split(' ')]
a=[0]*n
for i in range(m):
    for j in range(l[i],r[i]+1):
        a[j+1]=1
print(sum(a))

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
G=[]
a=[]
S=[]
for i in range(M):
    l,r=map(int,input().split())
    a=[x for x in range(l,r+1)]
    G.append(a)
for i in range(M-1):
    G_diff=set(G[i]).intersection(set(G[i+1]))
print(len(G_diff))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if M == 1:
    L, R = map(int,input().split())
    l = [k+L for k in range(R-L+1)]
    print(len(l))
else:
    L, R = map(int,input().split())
    l = [k+L for k in range(R-L+1)]
    l_tmp = copy.deepcopy(l)

    L, R = map(int,input().split())
    l = [k+L for k in range(R-L+1)]

    l_set = list(set(l) & set(l_tmp))
    l_tmp = copy.deepcopy(l)
    for i in range(M-2):
        L, R = map(int,input().split())
        l = [k+L for k in range(R-L+1)]

        l_set = list(set(l) & set(l_set))
        l_tmp = copy.deepcopy(l)

    print(len(l_set))

Submission status: Runtime Error

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

for i in range(M):
  L, R = map(int, input().split())
  pass_card = [i for i in range(L, R+1)]
  if  i == 0:
    ans = pass_card
  elif i > 0:
    ans = list(set(pass_card[i - 1]) & set(pass_card[i]))

print(len(ans))

Submission status: Runtime Error

Solution code:
n,m=(int(i) for i in input().split())
a=0
b=0
for i in range(m):
  l,r=(int(g) for g in input().split())
  if l>a,r<b:
    a=l
    b=r
  elif l>a and r>=b:
    a=l
  elif r<b and l<=a:
    b=r
if n>a and n<b:
  print(n-a)
elif n>a and n>=b:
  print(b-a)
else:
  print(0)

Submission status: Runtime Error

Solution code:
import bisect
N,M = map(int,input().split())
L_list,M_list = [],[]
for i in range(M):
    l,m = map(int,input().split())
    L_list.append(l)
    M_list.append(m)

L_list.sort()
M_list.sort()
lmax = L_list[-1]
mmin = M_list[0]
min = bisect.bisect_left(n,lmax)
max = bisect.bisect_left(n,mmin)
if min == max:
    ans = 1
else:
    ans = max - min
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N = int(s[0])
    M = int(s[1])
    L = []
    R = []
    for i in range(M):
        s = input().rstrip().split(' ')
        L.append(int(s[0]))
        R.append(int(s[1]))

    res = list(range(1,N+1))

    for i in range(M):
        res = list(range(max(res[0],L[i]), min(R[i],res[-1])+1))

    print(len(res))

Submission status: Runtime Error

Solution code:
n,m=(int(i) for i in input().split())
a=0
b=0
for i in range(m):
  l,r=(int(i) for i in input().split())
  if l>a,r<b:
    a=l
    b=r
  elif l>a and r>=b:
    a=l
  elif r<b and l<=a:
    b=r
if n>a and n<b:
  print(n-a)
elif n>a and n>=b:
  print(b-a)
else:
  print(0)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
    

"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

#Distance Transformation
    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)

"""



N, M = list(map(int, input().split()))
L=[0]*M
R=[0]*M


A = set([i for i in range(1, N+1)])

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


def main():
    #print([i for i in range(L[i], R[i]+1)])
    print(len(A))
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

n, m = map(int, input().split())

count = np.array([0] * n)

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

print(len(np.where(count==m)))

Submission status: Runtime Error

Solution code:
isstr=input()
n,m = [int(i) for i in isstr.split(' ')]
l=[0]*m
r=[0]*m
for i in range(m):
    l[i],r[i]=[int(i) for i in input().split(' ')]
a=[0]*n
for i in range(m):
    a[l[i]-1]=1
    a[l[i]]=1
    a[r[i]-1]=1
print(sum(a))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
l = []
max = 0
min = 100001

for i in range(m):
    l += [input()]
    if int(l[i][0]) > max:  
        max = int(l[i][0])
    if int(l[i][2]) < min:
        min = int(l[i][2])
       
if min - max >= 0:
    print(min - max + 1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
import numpy as np
ar = np.zeros([M, N]).astype(np.int8)
for i in range(M):
  l, r = map(int, input().split())
  ar[i, l-1:r] = 1
print(int(ar.min(axis=0).sum()))

Submission status: Runtime Error

Solution code:
n,m =map(int,input().split())
count = 0
memory = 0
flag = True
for i in range(m):
  l,r = map(int,input().split())
  while flag = True:
    if l != r:
      l += 1
      count += 1
    else:
      flag = False
  if count == r-l:
    memory += 1
  flag =True

Submission status: Runtime Error

Solution code:
import sys
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
gates = []
for gate in args[1:]:
  l, r = map(int, gate.split(' '))
  gates.append((l, r))
  
ids = gates[0]
for gate in gates[1:]:
  l, r = gate
  ids = [x for x in ids in range(l, r+1)]
  
print(len(ids))

Submission status: Runtime Error

Solution code:
n,m =map(int,input().split())
count = 0
memory = 0
for i in range(1,m+1):
  l,r = map(int,input().split())
  while flag = True:
    if l != r:
      l += 1
      count += 1
    else:
      flag = False
  if count == r-l:
    memory += 1
  flag =True

Submission status: Runtime Error

Solution code:
import sys
args = sys.stdin.readlines():
n, m = map(int, args[0].split(' '))
gates = []
for gate in args[1:]:
  l, r = map(int, gate.split(' '))
  gates.append((l, r))
  
ids = gates[0]
for gate in gates[1:]:
  l, r = gate
  ids = [x for x in ids in range(l, r+1)]
  
print(len(ids))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
import numpy as np
ar = np.zeros([M, N])
for i in range(M):
  l, r = map(int, input().split())
  ar[i, l-1:r] = 1
print(int(ar.min(axis=0).sum()))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L=[]
R=[]
for i in range(M):
  L_n,R_n = map(int,input().split())
  L.append(L_n)
  R.append(R_n)
Nlist = list(range(L[0],R[0]+1))
  
for i in range(1,M):
  a = L[i]
  b = R[i]
  if Nlist[0]<a or Nlist[-1]>b:
    Nlist = [k for k in Nlist if (k > (a-1)) and (k < (b+1))]
print(len(Nlist))

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 + 10)
for i in range(m):
    x,y = map(int,input().split())
    a[x] = 1
    a[y + 1] = -1
for i in range(n + 3):
    a[i + 1] += a[i]
print(a.count(max(a)))

Submission status: Runtime Error

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

def prison(N,M,L,R):
  count=0
  for n in range(N):
    num_of_truth=0
    for l,r in zip(L,R):
      if l<=(n+1)<=r:
        num_of_truth+=1
    if num_of_truth==M:
      count+=1
  return count
 
print(prison(N,M,L,R))

Submission status: Runtime Error

Solution code:
#include <stdio.h>

int main(void) {
    int n, m, l, r, c[100000], count=0;

    scanf("%d %d", &n, &m);
    for (int i=1; i<=n; i++) {
        c[i] = 0;
    }
    for(int i=0; i<m; i++) {
        scanf("%d %d", &l, &r);
        for (l; l<=r; l++) {
            c[l]++;
        }
    }
    for(int i=1; i<=n; i++) {
        if(c[i] == m) {
            count++;
        }
    }
    printf("%d\n", count);
    return 0;
}

Submission status: Runtime Error

Solution code:
ss=input().split(" ")
N=int(ss[0])
M=int(ss[1])
x=0
y=N
for i in range(M):
  tt=input().split(" ")
  if int(tt[0])>x:
    x=int(tt[0])
  if int(tt[1])<y:
    y=int(tt[1])
print(int(y-x+1)

Submission status: Runtime Error

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

def prison(N,M,L,R):
    count=0
    for n in range(N):
        num_of_truth=0
        for l,r in zip(L,R):
            if l<=(n+1)<=r:
                num_of_truth+=1
        if num_of_truth==M:
            count+=1
    return count

print(prison(N,M,L,R))

Submission status: Runtime Error

Solution code:
n,m =map(int,input().split())
count = 0
memory = 0
for x in range(1,m+1):
  l,m = map(int,input().split())
  while flag = True:
    if l != r:
      l += 1
      count += 1
    else:
      flag = False
  if count == r-l:
    memory += 1
  flag =True

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
C={i for i in range(1,N+1)}

for _ in range(M):
    LR=list(map(int,input().split()))
    C=C&{k for k in range(max(LR[0],min(C)),min(LR[1]+1,max(C)+1))}
print(len(C))