思路:首先最简单的想法就是构建出查询数组,但他的时间复杂的是,也就是
,不管是时间还是空间上都不行,因此我们不能直接去构建查询数组
那本题其实更像是一种不均匀的分组问题,然后查当前音符是属于哪个组的,如果是均匀分组的话,我们其实就可以用类似于位图的处理方法,用divmod计算除数和余数,确定分组,那对于这种不均匀的分组,我们其实可以用前缀和 + 二分查找来做
怎么想到的?注意到题目给出的查询t数组,其实就是一种前缀和,因此我们首先要处理出一个前缀和数组,并且由于前缀和数组天生具有的单调递增特性,我们就可以用二分查找。
这里我直接用bisect_right库函数,就是二分查找>当前数的第一个数索引,然后-1就是<=当前数的最后一个数索引,由于音符类型是从1开始的,所以说还要+1,那么两个抵消之后,其实就只是bisect_right的结果了,最终回答多次查询,再整体输出即可
代码:
import sys
from bisect import bisect_right
input = lambda: sys.stdin.readline().strip()
import math
inf = 10 ** 18
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LI():
return input().split()
def LII():
return list(map(int, input().split()))
def LFI():
return list(map(float, input().split()))
fmax = lambda x, y: x if x > y else y
fmin = lambda x, y: x if x < y else y
isqrt = lambda x: int(math.sqrt(x))
'''
'''
def solve():
n, q = MII()
b = LII()
t = LII()
pre = [0] * (n + 1)
for i, x in enumerate(b):
pre[i + 1] = pre[i] + x
out = []
for x in t:
p = bisect_right(pre, x)
out.append(str(p))
print("\n".join(out))
tt = 1
# t = II()
for _ in range(tt):
solve()

京公网安备 11010502036488号