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: Wrong Answer

Solution code:
N, M = map(int, input().split())
 
A, B = map(int, input().split())
 
for i in range(M-1):
    A2, B2 = map(int, input().split())
 
    if (A < A2 and B2 == A):
        A = B2
        B = B2
    elif (B == A2 and B< B2):
        A = A2
        B = A2
    elif (A < A2 and B2 < A) or (B < A2 and B< B2):
        A = 0
        B = 0
    elif (A2 <= A and A <= B):
        B = B2
    elif (A2 <= B and B <= B2):
        A = A2
    elif (A2 <= A and B <= B2):
        A = A
        B = B
    elif (A2 <= A and B2 <=B):
        A = A
        B = B2
    elif (A <=A2 and B <= B2):
        A = A2
        B = B
    else:
        A = A2
        B = B2
 
 
if A == 0 and B == 0:
  print(0)
else:
  print(B-A+1)

Submission status: Wrong Answer

Solution code:
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    for gate in gates:
        # print(gate)
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]

    print (mi)
    print (ma)
    res = 0
    if mi < ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
number = input().split()
card_list = []

for i in range(int(number[1])):
    card = input().split()
    LR = int(card[1]) - int(card[0]) + 1
    j = 0
    if number[1] == "1":
        print(number[0])
        break
    while LR > 0:
        card_list.append(int(card[0]) + j)
        j += 1
        LR -= 1
        
if number[1] != "1":
    card_lists = [x for x in set(card_list) if card_list.count(x) > int(number[1]) - 1]
    print(len(card_lists))

Submission status: Wrong Answer

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
l = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

sMax = 0
eMin = 0
for s, e in l:
  if sMax < s or sMax == 0:
    sMax = s
  if eMin > e or eMin == 0:
    eMin = e

print(len(range(eMin, sMax))+1)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N,M = [int(x) for x in input().split()]
Llist =[]
Rlist =[]
for i in range (M):
    L,R = [int(x) for x in input().split()]
    Llist.append(L)
    Rlist.append(R)
 
lmax = max(Llist)
Rmax = min(Rlist)

if lmax >= Rmax:
    print(0)
else:
    print(Rmax-lmax-1)

Submission status: Wrong Answer

Solution code:
x = input()
li = x.split()
n = int(li[0])
m = int(li[1])
o = 0
y = input()
e = y.split()
u = int(e[0])
t = int(e[1])
q = u
w = t
for k in range (m - 1):
  y = input()
  e = y.split()
  l = int(e[0])
  r = int(e[1])
  if (q < l and w < r):
    q = l
    w = r
if(q < t):
  if(n < q):
    o =0
  elif(n <= t):
    o = n - q
  else:
    o = t -q + 1
else:
  o = 0
    
  

print(o)

Submission status: Wrong Answer

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

for i in range(m):
    a,b=map(int,input().split())
    y=[]

    for j in range(b-a+1):
        y.append(a)
        a+=1
    
    x.append(y)

if m==1:
    print(b-a+1)
else:
    kyoutu=set(x[0])& set(x[1])
    for k in range(2,m):
        kyoutu=set(kyoutu) & set(x[k])

    print(len(kyoutu))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x=0
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if b[0]<= a[0] <= b[1]:
  x=a[0]+1
print(x)

Submission status: Wrong Answer

Solution code:
import sys
n,m = map(int, input().split())
cl = list()
cr = list()
cn = 0
#big_n = 0
for i in range(m):
    l,r = map(int, input().split())
    cl.append(l)
    cr.append(r)

for j in range(1,n+1): # Loop card
#    print("For card "+str(j))
    for k in range(1,m+1):
#        print("Checking "+str(k))
        l = cl[k-1]
        r = cr[k-1]
#        print("Try "+str(l)+" "+str(r))
        if r >= j:
#            print("Hit wall "+str(r))
            break
        else:
            #Is this final wall?
            if not k >= m:
#              print("Wall "+str(k)+" Passed")
              continue
#            print("Found card pair "+str(j)+":"+str(k))
            cn = cn + 1

print(cn)
    #Validate each cards

Submission status: Wrong Answer

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

for i in range(M):
    l,r = map(int, input().split(' '))
    L_list.append(l)
    R_list.append(r)


all_list=[]
num_list = range(1,len(L_list))

#Listを足しこむ
cnt = 0
all_uniq = range(L_list[0],R_list[0]+1)
print(all_uniq)
for i in range(1,M):
    tmp_ran = range(L_list[i],R_list[i]+1)
    all_uniq = set(all_uniq) & set(tmp_ran)

all_uniq = list(all_uniq)
# print(all_uniq)
for i in all_uniq:
    if(i<=N):
        cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def main():
    N,M= map(int,input().split())
    List=[list(map(int,input().split())) for i in range(M)]
    ListN = list()
    List3 = list()
    for k in range(1,N+1):
        ListN.append(k)
    for i in range(M):
        List2 = list()
        x = List[i][0]
        y = List[i][1]
        for j in range(x,y):
            List2.append(j)
        for Li in List2:
            for Ln in ListN:
                if Li == Ln:
                    List3.append(Ln)
    List3 = list(set(List2))
    print(len(List3))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
              

N, M = map(int, input().split())
#A = input().split()
#A = list(map(int, A))
#print(A)
#A.sort()
#print(A)

cng = []
L_max=0
R_min=1000000000000
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-L)

Submission status: Wrong Answer

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)

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

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    G1 = []
    G2 = []
    ans = 0
    for i in range(m):
        g1, g2 = map(int, input().split())
        G1.append(g1)
        G2.append(g2)

    print(min(G2) - max(G1) + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

inu = []
neko = []
for i in range(m):
    x = set(range(res[i][0],res[i][1]+1))
    inu.append(x)

if m == 1:
    print(len(inu[0]))
else:
    for j in range(1,m):
        if j == 1:
            p = (inu[0].intersection(inu[j]))
            print(p)
        else:
            p = p.intersection(inu[j])
            print(p)
    print(len(p))

Submission status: Wrong Answer

Solution code:
N, M = 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)

if M == 1:
    print(R[0])
elif L[M - 1] != R[0]:
    print(2)
else:
    print(1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
N,M= (int(i) for i in input().split(" "))  

LR= np.array([[int(i) for i in input().split()] for i in range(M)]) 

L=LR[-1,0]
R=LR[0,1]


el=np.sum(np.logical_and(LR>L,R>LR))


print(int(abs(R-L)+1-el))

Submission status: Wrong Answer

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] == R[0]:
    count = 1
elif L[-1] < R[0]:
    count = R[0] - L[M - 1] + 1
else:
    count = 0

print(count)

Submission status: Wrong Answer

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

for i in range(M):
    L,R=map(int,input().split())
    if end-start!=-1:
        if L>start:
            start=L
        if R<end:
            end=R
print(end-start+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, raw_input().split())
a_list = []
if M==1:
    a_list.append(map(int, raw_input().split()))
    print N
else:
    for i in range(M):
        a_list.append(map(int, raw_input().split()))
        tmp_set = set(j for j in range(a_list[i][0], a_list[i][-1]+1))
        if i == 0:
            si = tmp_set
        else:
            si = si & tmp_set

    print len(si)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a = input().split()

firstIntInput = [int(i) for i in a]

a = input().split()

head = int(a[0])
tail = int(a[1])

zeroFlag = 0

for i in range(1,firstIntInput[1]):
	b = input().split()
	intInput = [int(i) for i in b]
	if head == tail or  head > intInput[1] or tail < intInput[0]:
		zeroFlag = 1
		break
	if head < intInput[0]:
		head = intInput[0]
	if tail > intInput[1]:
		tail = intInput[1]

print(tail - head + 1 if zeroFlag == 0 else 0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(M)]
 
ans = 0
t = 0
 
for i in range(M): 
    t = 1
    if li[0][1] < li[i][1] and i != 0:
        t = 0
    else:
        for j in range(N):
            if not li[i][0] <= j+1 <= li[i][1]:
                t = 0
                break
    ans += t
 
print(ans)

Submission status: Wrong Answer

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

all_pass = 0
pass_card = []

for i in range(M):
    pass_card.append([])
    for j in range(i, len(L)):
        pass_card[i].append(L[j])
    for j in range(0, i + 1):
        pass_card[i].append(R[j])

and_list = pass_card[0]

for i in range(len(pass_card)):
    and_list = set(and_list) & set(pass_card[i])

print(len(and_list))

Submission status: Wrong Answer

Solution code:
import sys
#from itertools import ifilter

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

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

minNum = 10000
maxNum = -10000

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

    # if i == 0:
    #     for j in range(l, r + 1):
    #         nums.append(j)

    if l < minNum:
        minNum = l
    if r > maxNum:
        maxNum = r

    if minNum >= maxNum:
        print(0)
        sys.exit()

print(maxNum - minNum - 2)

Submission status: Wrong Answer

Solution code:
import sys
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) 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 MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
n, m = MI()
lr = [LI() for _ in range(m)]
l = lr[0][0]
r = lr[0][1]
for i in range(1, m):
    if not (l <= lr[i][0] <= r and l <= lr[i][1] <= r):
        print(0)
        exit()
    if l <= lr[i][0] <= r:
        l = lr[i][0] 
    if l <= lr[i][1] <= r:
        r = lr[i][1]
print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ls = [[int(x) for x in input().split()]for i in range(m)]
lst = []
ans = 0
ans_2 = 0
for i in range(m):
    a,b = ls[i][0],ls[i][1]
    for j in range(n):
        if a<=j<=b:
            lst.append(j)
for k in range(n):
    if (lst.count(k)) == m:
        ans_2 += 1
print(ans_2)

Submission status: Wrong Answer

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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    min = 1
    max = N
    for i in range(M):
        if L[i] > min:
            min = L[i]
        if R[i] < max:
            max = R[i]
    sum = 1
    for num in range(min, max):
        sum += 1
    print(sum)
    return


# Generated by 1.1.4 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: Wrong Answer

Solution code:
import numpy as np
N,M = map(int,input().split())
L = [0] * M
X = [0] * M
for i in range(M):
    L[i], X[i] = map(int, input().split())
l = max(L)
x = min(X)
print(x-l+1)

Submission status: Wrong Answer

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

li = [[0, 99999999999]]

for i in range(M):
    li.append(list(map(int, input().split())))
    if (li[i][0] < li[i+1][0] ):
        L_max = li[i+1][0]
        if (li[i][1] > li[i+1][1]):
            R_min = li[i+1][1]
    elif (li[i ][1] > li[i+1][1]):
            R_min = li[i+1][1]

if (li[M-1][0] < li[M][0] ):
       L_max = li[M][0]
       if (li[M-1][1] > li[M][1]):
          R_min = li[M][1]
elif (li[M-1 ][1] > li[M][1]):
            R_min = li[M][1]

if (L_max > N) or (L_max > R_min):
    print("0")
elif(R_min <= N):
    print(R_min - L_max + 1)
else:
    print(N + 1 - L_max)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def main():
    N,M= map(int,input().split())
    List=[list(map(int,input().split())) for i in range(M)]
    print(List)#[[1, 3], [2, 4]]
    ListN = list()
    List3 = list()
    for k in range(1,N+1):
        ListN.append(k)
    for i in range(M):
        List2 = list()
        x = List[i][0]
        y = List[i][1]
        for j in range(x,y):
            List2.append(j)
        for Li in List2:
            for Ln in ListN:
                if Li == Ln:
                    List3.append(Li)
    List3 = list(set(List2))
    print(len(List3))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#input
a = list(map(int,input().split())) #input from array of integer like "1 2 3"

l = []
r = []
for i in range(a[1]):
  ll,rr = (int(x) for x in input().split())
  l.append(ll)
  r.append(rr)

l.reverse()

print(r[0]-l[0]+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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] == R[0]:
    count = 1
elif L[-1] < R[0]:
    count = R[0] - L[M - 1] + 1
elif L[-1] == R[-1]:
    count = 1
else:
    count = 0

print(count)

Submission status: Wrong Answer

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

maxi_L = 0
mini_R = 10000000
A,B = (int(x) for x in input().split())
L_R.append([A,B])
L_R.append([A,B])
if A > maxi_L:
    maxi_L = A
if B < mini_R:
    mini_R = B
for i in range(2,M+1):
    A,B = (int(x) for x in input().split())
    L_R.append([A,B])
    if A > maxi_L:
        maxi_L = A
    if B < mini_R:
        mini_R = B
#mini = 1000000
#for i in range(len(L_R)):
#    if L_R[i][0] < mini:
#        mini = L_R[i][0]
#maxi = 0
#for i in range(len(L_R)):
#    if L_R[i][1] > maxi:
#        maxi = L_R[i][1]

#num = 0
#maxi = max(maxi,N)
#for x in range(mini,maxi+1):
#    n = 0
#    for i in range(1,M+1):
#        if L_R[i][0] <= x <= L_R[i][1]:
#            n += 1
#    if n == M:
#        num += 1
#    else:
#        pass


mini_R  - maxi_L + 1
print(mini_R  - maxi_L + 1)

Submission status: Wrong Answer

Solution code:
#coding utf-8
N,M=map(int,input().split())
L=[0 for i in range(M)]
R=[0 for i in range(M)]
for i in range(M):
  L[i],R[i]=map(int,input().split())

l=0
r=1000001
for i in range(M):
  if L[i]>l:
    l=L[i]
  if R[i]<r:
    r=R[i]
ans=0

if N<l:
  ans=0
elif N>=l and N<=r:
  ans = N-l+1
elif N>r:
  ans = r-l+1
print(ans)

Submission status: Wrong Answer

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

r_l, r_r = l_list[0], r_list[0]
for l, r in zip(l_list[1:], r_list[1:]):
    if l <= r_r <= r :
        r_l = l
    elif l <= r_l <= r:
        r_r = r
    elif r_l <= l and r <= r_r:
        r_l, r_r = l, r
    else:
        r_l, r_r = 0, -1
        break

print(r_r - r_l + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
f = []
for i in range(m):
    a = list(map(int, input().split()))
    f.append(a)
print(int(f[0][1] - f[m-1][0] + 1))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
        for i in range(m):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
    
    lr = []
    for i in range(1, m+1):
        token = lines[i].split(" ")
        l = int(token[0])
        r = int(token[1])
        lr.append({"l":l, "r":r})

    return (n, m, lr)


def solve(n, m, lr):



    l = lr[0]["l"]
    r = lr[0]["r"]
    c = {"l":l, "r":r}

    for i in range(m):

        if lr[i]["l"] <= c["r"]:
            c["r"] = c["r"]
            c["l"] = max(c["l"],  lr[i]["l"])
        elif c["l"] <= lr[i]["r"]:
            c["r"] = min(c["r"],  lr[i]["r"])
            c["l"] = c["l"]
        else:
            return 0

        if debug:
            log("c=%s" % c)
    
    result = c["r"] - c["l"] + 1
    if debug:
        log("result=%s" % result)

    return c["r"] - c["l"] + 1



def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

Solution code:
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    for gate in gates:
        # print(gate)
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]
    res = 0
    if mi > ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
conunt = 0
for _ in range(M):
    a, b = map(int, input().split())
    if a < N and a < N:
      conunt = conunt + 1
print(conunt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
N, M= map(int, raw_input().split())
i = 1
a, b = map(int, raw_input().split()) 
if i == M:
    result = b - a + 1
    print result
    sys.exit()
while 1:
  if i >= M:
    result = b - a + 1
    print result
    sys.exit()
  c, d = map(int, raw_input().split())  
  a = c
  i = i + 1
  temp = b - a
  if temp < 0:
    print "0"
    sys.exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a, b= map(int, input().split())
A=[]
A = [list(input().split())   for i in range(b)]
B=[]
for i in A:
  s=int(i[0])
  v=int(i[1])+1
  for j in range(s,v):
    B.append(j)
C = [x for x in set(B) if B.count(x) > 1]
print(len(C))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
 
A, B = map(int, input().split())
 
for i in range(M-1):
    A2, B2 = map(int, input().split())
 
    if (A < A2 and B2 < A) or (B < A2 and B< B2):
        A = 0
        B = 0
    elif (A2 <= A and A <= B):
        B = B2
    elif (A2 <= B and B <= B2):
        A = A2
    elif (A2 <= A and B <= B2):
        A = A
        B = B
    elif (A2 <= A and B2 <=B):
        A = A
        B = B2
    elif (A <=A2 and B <= B2):
        A = A2
        B = B
    else:
        A = A2
        B = B2
 
 
if A == 0 and B == 0:
  print(0)
else:
  print(B-A+1)

Submission status: Wrong Answer

Solution code:
N, M = [int(x) for x in input().split()]
L = 1
R = N
for i in range(M):
    L_,R_ = [int(x) for x in input().split()]
    L = max(L, L_)
    R = min(R, R_)
    R = max(R, L)

print(R-L + 1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
l = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

sMax = 0
eMin = 0
for s, e in l:
  if sMax < s or sMax == 0:
    sMax = s
  if eMin > e or eMin == 0:
    eMin = e

print(abs(sMax - eMin) +1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# リスト作る
a = [x for x in range(1, N+1)]

# for文での処理
'''
通過できるIDを追加→処理が間に合わない
通過できない要素を削除→removeはerror,del範囲指定できなくなる,
removeするものあればremove
'''
for i in range(M):
	L, R = map(int, input().split())
	for l in range(1, L+1):
		if l in a:
			a.remove(l)
	for r in range(R+2, N+1):
		if r in a:
			a.remove(r)

print(len(a))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
N, M = map(int, input().split())
#複数行入力
#a,b = [int(input()) for i in range(M)]

count=0
for x in range(M):
	L, R = map(int, input().split())
	#if(min_R >= R):
	#	min_R = R
	if(x <= 0):
		keep_L = L
		keep_R = M
	if(L >= keep_L and L <= keep_R):
		count = count+1
	if(R <= keep_R and R >= keep_L):
		count = count+1
print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
l = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

sMax = 0
eMin = 0
for s, e in l:
  if sMax < s:
    sMax = s
  if eMin > e or eMin == 0:
    eMin = e

print(sMax - eMin +1)

Submission status: Wrong Answer

Solution code:
def main():
    N, M = map(int, input().split())
    id_and_gate = []
    for m in range(0, M):
        tmp = [int(x) for x in input().split()]
        id_and_gate.append(tmp)

    id_num = 0
    l = 1
    r = 100000
    for j in range(0, M):
        if id_and_gate[j][0] >= l and id_and_gate[j][0] <= r:
            l = id_and_gate[j][0]
        if id_and_gate[j][1] >= l and id_and_gate[j][1] <= r:
            r = id_and_gate[j][1]

    print(r-l+1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
L = 0
R = 0
Lmax = 0
Rmin = 100000
for i in range(0,M):
  L, R = map(int,input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

print(Rmin - Lmax + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def main():
	Ls = []
	Rs = []
	for i in range(M):
		inputs= list(map(int,input().split()))
		Ls.append(inputs[0])
		Rs.append(inputs[1])

	result = set()
	for i in range(1, M + 1):
		Li = Ls[i - 1]
		LiPlusOne = Li + 1
		LiPlus = [j for j in range(Li + 1, Rs[i - 1] + 1)]

		result = result & set([Li] + LiPlus)

		if len(result) == 0:
			result = set([Li] + LiPlus)
		result = result & set([Li] + LiPlus)

	print(len(result))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
N, M= map(int, raw_input().split())
i = 1
a, b = map(int, raw_input().split()) 
if i == M:
    result = b - a + 1
    print result
    sys.exit()
while 1:
  if i >= M:
    result = b - a + 1
    print result
    sys.exit()
  c, d = map(int, raw_input().split())  
  a = c
  i = i + 1

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

n, m = sys.stdin.readline().rstrip().split(' ')
ll, rr = None, None
for _ in range(int(m)):
  l, r = sys.stdin.readline().rstrip().split(' ')
  l, r = int(l), int(r)
  ll = l if ll is None else max(ll, int(l))
  rr = r if rr is None else min(rr, int(r))
print(rr-ll+1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
        for i in range(m):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
    
    lr = []
    for i in range(1, m+1):
        token = lines[i].split(" ")
        l = int(token[0])
        r = int(token[1])
        lr.append({"l":l, "r":r})

    return (n, m, lr)


def solve(n, m, lr):


    if len(lr) == 1:
        l = lr[0]["l"]
        r = lr[0]["r"]
        return r - l + 1

    l = lr[0]["l"]
    r = lr[0]["r"]
    c = {"l":l, "r":r}

    for i in range(m):

        if lr[i]["l"] <= c["r"]:
            c["r"] = c["r"]
            c["l"] = max(c["l"],  lr[i]["l"])

        if c["l"] <= lr[i]["r"]:
            c["r"] = min(c["r"],  lr[i]["r"])
            c["l"] = c["l"]

        if debug:
            log("c=%s" % c)
    
    result = c["r"] - c["l"] + 1
    if debug:
        log("result=%s" % result)

    return c["r"] - c["l"] + 1



def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

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

con = 0

print(R[0]-L[M-1]+1)

Submission status: Wrong Answer

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

r_l, r_r = l_list[0], r_list[0]
for l, r in zip(l_list[1:], r_list[1:]):
    if l <= r_r <= r :
        r_l = l
    elif l >= r_l >= r:
        r_r = r
    elif r_l <= l and r < r_r:
        r_l, r_r = l, r
    else:
        r_l, r_r = 0, -1
        break

print(r_r - r_l + 1)

Submission status: Wrong Answer

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

ans = [0] * (N+1)
count = 0
prev = []
for i in range(M):
    l, r = map(int, input().split())

    for j in range(l,r+1):
        ans[j] += 1

maxv = max(ans)
print(ans.count(maxv))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
 
A, B = map(int, input().split())
 
for i in range(M-1):
    A2, B2 = map(int, input().split())
 
    if (A < A2 and B2 < A) or (B < A2 and B< B2):
        A = 0
        B = 0
    elif (A2 <= A and A <= B):
        B = B2
 
    elif (A2 <= B and B <= B2):
        A = A2

    elif (A2 <= A and B <= B2):
        A = A
        B = B
    else:
        A = A2
        B = B2
 
 
if A == 0 and B == 0:
  print(0)
else:
  print(B-A+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
ans_l = 1
ans_r = 10 ** 5
l_list = []
r_list = []
for i in range(m):
    l, r = map(int, input().split())
    l_list.append(l)
    r_list.append(r)
for r in r_list:
    if ans_r > r:
        ans_r = r
for l in l_list:
    if ans_l <= l:
        asn_l = l
if ans_r > ans_l:
    print(ans_r - ans_l + 1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
# c

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

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

# c = set(range(a[0][0],a[0][1]+1))
#
# for i, j in enumerate(a):
#     a2, b2 = j
#     d = set(range(a2,b2+1))
#     c = c & d
# 
# print(len(c))

hai = [i+1 for i in range(n)]

for i, j in enumerate(a):
    b, c = j
    print
    if b != 1:
        for k in range(b):
            hai[k] = 0
    for k in range(c+1,len(hai)):
        hai[k] = 0
print(n - hai.count(0))

Submission status: Wrong Answer

Solution code:
import collections

hoge = input().split()
n = int(hoge[0])
m = int(hoge[1])

ok_card = []
for i in range(m):
  hoge = input().split()
  for j in range(int(hoge[1]) - int(hoge[0])+1):
    ok_card.append(int(hoge[0])+j)

c = collections.Counter(ok_card)
print(len([i for i in range(len(c)) if int(c[i+1]) >= m]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# ll = Ls[0]
# rr  =Rs[0]
# for i in range(M):
#     ll = max(ll, Ls[i])
#     rr = min(rr, Rs[i])
#
# print(rr - ll + 1)
print(min(0, min(Rs) - max(Ls) + 1))

Submission status: Wrong Answer

Solution code:
def main():
    N, M = map(int, input().split())
    id_and_gate = []
    for m in range(0, M):
        tmp = [int(x) for x in input().split()]
        id_and_gate.append(tmp)

    id_num = 0
    l = 0
    r = 100000
    for j in range(0, M):
        if id_and_gate[j][0] >= l and id_and_gate[j][0] <= r:
            l = id_and_gate[j][0]
        if id_and_gate[j][1] >= l and id_and_gate[j][1] <= r:
            r = id_and_gate[j][1]

    print(r-l+1)

if __name__ == "__main__":
    main()