import bisect from collections import defaultdict def main(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 M = int(input[ptr]) ptr += 1 max_A = 10**5 sqrt_max = int(max_A**0.5) # Approximately 317 # Category a: X <= sqrt_max category_a = defaultdict(list) # Category b: X > sqrt_max category_b = defaultdict(list) for _ in range(N): L = int(input[ptr]) ptr += 1 R = int(input[ptr]) ptr += 1 X = int(input[ptr]) ptr += 1 Y = int(input[ptr]) ptr += 1 if X <= sqrt_max: key = (X, Y) category_a[key].append((L, R)) else: current = Y while current <= max_A: category_b[current].append((L, R)) current += X # Process category_a into events and prefix sums processed_a = {} for key in category_a: X, Y = key intervals = category_a[key] events = [] for L, R in intervals: events.append((L, 1)) events.append((R + 1, -1)) events.sort() positions = [] prefix = [] current_pos = None current_sum = 0 for pos, delta in events: if pos != current_pos: if current_pos is not None: positions.append(current_pos) prefix.append(current_sum) current_pos = pos current_sum += delta if current_pos is not None: positions.append(current_pos) prefix.append(current_sum) positions.append(max_A + 1) prefix.append(0) processed_a[key] = (positions, prefix) # Process queries queries = list(map(int, input[ptr:ptr + M])) ptr += M for A in queries: count_a = 0 for X in range(1, sqrt_max + 1): Y = A % X key = (X, Y) if key not in processed_a: continue positions, prefix = processed_a[key] idx = bisect.bisect_right(positions, A) - 1 if idx >= 0: count_a += prefix[idx] count_b = 0 if A in category_b: for L, R in category_b[A]: if L <= A <= R: count_b += 1 print(count_a + count_b) if __name__ == '__main__': main()