class SegmentTree: def __init__(self, array): self.n = len(array) self.tree = [0] * (2 * self.n) self.build(array) def build(self, array): for i in range(self.n): self.tree[self.n + i] = array[i] for i in range(self.n - 1, 0, -1): self.tree[i] = self.tree[2 * i] + self.tree[2 * i + 1] def update(self, pos, value): pos += self.n self.tree[pos] = value while pos > 1: pos //= 2 self.tree[pos] = self.tree[2 * pos] + self.tree[2 * pos + 1] def query(self, left, right): left += self.n right += self.n sum = 0 while left < right: if left & 1: sum += self.tree[left] left += 1 if right & 1: right -= 1 sum += self.tree[right] left //= 2 right //= 2 return sum def preprocess(A, K): return [1 if x % K == 0 else 0 for x in A] import sys input = sys.stdin.read data = input().split() N = int(data[0]) Q = int(data[1]) A = list(map(int, data[2:N+2])) queries = data[N+2:] query_data = [] for i in range(Q): L = int(queries[3*i]) - 1 R = int(queries[3*i+1]) K = int(queries[3*i+2]) query_data.append((L, R, K)) results = [] for L, R, K in query_data: preprocessed_array = preprocess(A, K) segment_tree = SegmentTree(preprocessed_array) result = segment_tree.query(L, R) results.append(str(result)) sys.stdout.write('\n'.join(results) + '\n')