2020/04/20

[zerojudge] a450: 棒棒糖比身高

from sys import stdin
import bisect
# 請參考 https://docs.python.org/3/library/bisect.html

n, q = map(int, stdin.readline().split())
f = sorted([int(x) for x in stdin.readline().split()])
# 先排序
# 例如  8 5 10 11 8 會排序成 5 8 8 10 11
# 考慮到會有重複的元素
# 使用 bisect.bisect_left  取左邊位置
#      bisect.bisect_right 取右邊位置
for _ in range(q):
    a, b = map(int, stdin.readline().split())
    a = bisect.bisect_left(f, a)
    b = bisect.bisect_right(f, b)
    r = b - a
    if(r): print(r)
    else: print('The candies are too short')

沒有留言:

張貼留言