Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.

In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj  where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.

For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.

-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.

-----Output-----
For each query, print the required number of subarrays.

-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)

-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1

Output:
2
2
0
8

-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy.

Source: codechef | Difficulty: MEDIUM
Tags: Complete search

Constructive algorithms

Data structures

Ad-hoc


Solutions:
n = int(input())
a = list(map(int, input().split()))
cnt = {}
for i in range(n):
	mn = a[i]
	for j in range(i, n):
		mn = min(mn, a[j])
		if mn in cnt:
			cnt[mn] += 1
		else:
			cnt[mn] = 1
q = int(input())
for i in range(q):
	k = int(input())
	if k in cnt:
		print(cnt[k])
	else:
		print(0)


n = int(input())
al = [int(num) for num in input().split(' ', n - 1)]
for k in range(int(input())):
	cnt = 0
	val = int(input())
	for i in range(n):
		for j in range(i, n):
			sb = al[i:j + 1]
			if min(sb) == val:
				cnt += 1
	print(cnt)


n = int(input())
a = list(map(int, input().split()))
for q in range(int(input())):
	(k, ans) = (int(input()), 0)
	for i in range(n):
		for j in range(i, n):
			if min(a[i:j + 1]) == k:
				ans += 1
	print(ans)


input()
arr = list(map(int, input().split()))
Q = int(input())
for item in range(Q):
	K = int(input())
	total = 0
	for i in range(len(arr)):
		hasK = False
		if arr[i] >= K:
			if arr[i] == K:
				hasK = True
				total += 1
			j = i + 1
			while j < len(arr) and arr[j] >= K:
				if arr[j] == K:
					hasK = True
				if hasK:
					total += 1
				j += 1
	print(total)


n = int(input())
x = list(map(int, input().split()))
q = int(input())
for i in range(q):
	c = 0
	k = int(input())
	for j in range(n):
		for l in range(j, n):
			if min(x[j:l + 1]) == k:
				c += 1
	print(c)


n = int(input())
arr = [int(i) for i in input().split()]
q = int(input())
for _ in range(q):
	sub = 0
	k = int(input())
	for i in range(n):
		for j in range(i + 1, n + 1):
			if min(arr[i:j]) == k:
				sub = sub + 1
	print(sub)


n = int(input())
a = list(map(int, input().split()))
for k in range(int(input())):
	cnt = 0
	val = int(input())
	for i in range(n):
		for j in range(i, n):
			sb = a[i:j + 1]
			if min(sb) == val:
				cnt += 1
	print(cnt)


n = int(input())
l = list(map(int, input().split()))
for i in range(int(input())):
	c = 0
	quer = int(input())
	for i in range(n):
		for j in range(i + 1, n + 1):
			if min(l[i:j]) == quer:
				c += 1
	print(c)


import collections

def func(arr):
	d = collections.defaultdict(int)
	mind = {}
	n = len(arr)
	for i in range(n):
		for j in range(i, n):
			if i == j:
				d[arr[i]] += 1
				mind[i, j] = arr[i]
			else:
				mind[i, j] = min(mind[i, j - 1], arr[j])
				d[mind[i, j]] += 1
	return d
n = int(input())
s = input().split(' ')
arr = [int(i) for i in s]
q = int(input())
d = func(arr)
while q:
	k = int(input())
	print(d[k])
	q -= 1


n = int(input())
A = list(map(int, input().strip().split()))
D = dict()
for l in range(len(A)):
	for r in range(l + 1, len(A) + 1):
		v = min(A[l:r])
		D[v] = 1 if not v in D else D[v] + 1
q = int(input())
for i in range(q):
	v = int(input())
	print(0 if not v in D else D[v])


n = int(input())
arr = [int(i) for i in input().split()]
p = sorted(arr)
mn = p[0]
mx = p[-1]
for i in range(int(input())):
	k = int(input())
	if k < mn or k > mx:
		print(0)
	else:
		ans = 0
		for i in range(n):
			c_r = 0
			c_l = 0
			if arr[i] == k:
				L = arr[:i]
				R = arr[i + 1:]
				L = L[::-1]
				for j in L:
					if arr[i] > j:
						break
					else:
						c_l += 1
				for j in R:
					if j > arr[i]:
						c_r += 1
					else:
						break
				ans += c_l * (c_r + 1) + c_r + 1
		print(ans)


input()
arr = list(map(int, input().split(' ')))
subarrays = []
temp_arr = []
for i in range(len(arr) - 1):
	for j in range(i, len(arr)):
		temp_arr.append(arr[j])
		subarrays.append(temp_arr.copy())
	temp_arr = []
subarrays.append([arr[-1]])
for query in range(int(input())):
	num = int(input())
	total = 0
	for subarr in subarrays:
		if min(subarr) == num:
			total += 1
	print(total)


from sys import stdin, stdout
import numpy as np
n = int(input())
a = list(map(int, input().split()))
q = int(input())
while q:
	q -= 1
	k = int(input())
	ans = 0
	for i in range(n):
		flag = False
		count = 0
		forward = 0
		backward = 1
		if a[i] == k:
			if i != a.index(k):
				flag = True
			for j in range(i, n):
				if a[j] >= k:
					forward += 1
				else:
					break
			for j in range(i - 1, -1, -1):
				if flag:
					if a[j] > k:
						backward += 1
					else:
						break
				elif a[j] >= k:
					backward += 1
				else:
					break
			count += forward * backward
			ans += count
	print(ans)


from sys import stdin, stdout
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
q = int(input())
while q:
	q -= 1
	k = int(input())
	ans = 0
	for i in range(n):
		for j in range(i + 1, n + 1):
			if min(a[i:j]) == k:
				ans += 1
	print(ans)


n = int(input())
l = list(map(int, input().split()))
q = int(input())
for _ in range(q):
	cnt = 0
	k = int(input())
	for i in range(n):
		for j in range(i, n):
			if min(l[i:j + 1]) == k:
				cnt += 1
	print(cnt)


n = int(input())
s1 = input().split(' ')
l = []
for _ in s1:
	l.append(int(_))
i = 0
sub = []
while i < n:
	s = i
	f = i + 1
	while f <= n:
		l1 = l[s:f]
		sub.append(l1)
		f += 1
	i += 1
q = int(input())
k = 0
while k < q:
	c = 0
	q1 = int(input())
	for e in sub:
		if min(e) == q1:
			c += 1
	print(c)
	k += 1


import sys
import numpy as np

def find_subarray(min_target, lis):
	arr = np.array(lis)
	where_arr = np.where(arr == min_target)[0]
	count = 0
	if where_arr.size == 0:
		return count
	max_where_arr = max(where_arr)
	for (pos, value) in enumerate(lis):
		if value < min_target or pos > max_where_arr:
			pass
		else:
			for index in range(pos, len(lis)):
				if min(lis[pos:index + 1]) == min_target:
					count += 1
	return count

def main():
	length_array = sys.stdin.readline()
	lis = [int(number) for number in sys.stdin.readline().split()]
	number_inquiries = sys.stdin.readline()
	for min_target in [int(inquiry) for inquiry in sys.stdin.readlines()]:
		print(find_subarray(min_target, lis))
main()


import math

def sumfunc(num):
	return math.floor(num * (num + 1) / 2)
caseNum = int(input())
numList = []
numList = list(map(int, input().split()))
listLen = len(numList)
inqNum = int(input())
for eachInquiry in range(inqNum):
	minNum = int(input())
	(geqSubList, gSubList) = ([], [])
	(geqStart, geqEnd, gStart, gEnd) = (-1, -1, -1, -1)
	for i in range(listLen):
		if numList[i] > minNum:
			if i == 0 or numList[i - 1] <= minNum:
				gStart = i
			if i == listLen - 1 or numList[i + 1] <= minNum:
				gEnd = i
				gSubList.append(gEnd - gStart + 1)
		if numList[i] >= minNum:
			if i == 0 or numList[i - 1] < minNum:
				geqStart = i
			if i == listLen - 1 or numList[i + 1] < minNum:
				geqEnd = i
				geqSubList.append(geqEnd - geqStart + 1)
	if geqStart == -1:
		print(0)
	else:
		geqSum = sum(map(sumfunc, geqSubList))
		if len(gSubList) == 0:
			print(geqSum)
		else:
			gSum = sum(map(sumfunc, gSubList))
			print(geqSum - gSum)


n = int(input())
l = [int(i) for i in input().split()]
q = int(input())
for i in range(q):
	x = int(input())
	cnt = 0
	for i in range(n):
		for j in range(i, n):
			if min(l[i:j + 1]) == x:
				cnt += 1
	print(cnt)


def left(a, pos):
	current = a[pos]
	counter = 1
	pos -= 1
	while 1:
		if pos < 0:
			break
		if current >= a[pos]:
			break
		else:
			counter += 1
		pos -= 1
	return counter

def right(a, pos):
	current = a[pos]
	counter = 1
	pos += 1
	while 1:
		if pos >= len(a):
			break
		if current > a[pos]:
			break
		else:
			counter += 1
		pos += 1
	return counter
n = int(input(''))
a = input('').split(' ')
for i in range(len(a)):
	a[i] = int(a[i])
q = int(input(''))
for i in range(q):
	x = int(input(''))
	ans = 0
	for j in range(len(a)):
		if a[j] == x:
			ans += left(a, j) * right(a, j)
	print(ans)


N = int(input())
array = list(map(int, input().split()))
for _ in range(int(input())):
	K = int(input())
	count = 0
	for i in range(N):
		for j in range(i + 1, N + 1):
			if K == min(array[i:j]):
				count += 1
	print(count)


n = int(input())
arr = [int(i) for i in input().split()]
q = int(input())
for _ in range(q):
	val = int(input())
	if val not in arr:
		print(0)
	else:
		count = 0
		for i in range(1, n + 1):
			for j in range(n - i + 1):
				if min(arr[j:j + i]) == val:
					count += 1
		print(count)


from collections import defaultdict
n = int(input())
a = [int(i) for i in input().split()]
g = defaultdict(int)
for i in range(n):
	for j in range(i, n):
		g[min(a[i:j + 1])] += 1
q = int(input())
for _ in range(q):
	print(g[int(input())])


n = int(input())
arr = [int(x) for x in input().split(' ')]
for __ in range(int(input())):
	count = 0
	val = int(input())
	for i in range(n):
		for j in range(i, n):
			sub_arr = arr[i:j + 1]
			if min(sub_arr) == val:
				count += 1
	print(count)


n = int(input())
l = list(map(int, input().split()))
query = int(input())
for _ in range(query):
	q = int(input())
	a = 0
	for i in range(n):
		for j in range(i + 1, n + 1):
			if min(l[i:j]) == q:
				a += 1
	print(a)


def cal_temp_subarr(start, end, q_pos):
	temp = (q_pos[0] - start + 1) * (end - q_pos[0])
	for i in range(1, len(q_pos)):
		temp += (q_pos[i] - q_pos[i - 1]) * (end - q_pos[i])
	return temp
n = int(input())
arr = list(map(int, input().split()))
q = int(input())
for i in range(q):
	num = int(input())
	ans = 0
	start = -1
	q_pos = []
	for j in range(n):
		if arr[j] > num and start == -1:
			start = j
		elif arr[j] < num:
			if len(q_pos):
				ans += cal_temp_subarr(start, j, q_pos)
			start = -1
			q_pos = []
		elif arr[j] == num:
			if start == -1:
				start = j
			q_pos.append(j)
	if start != -1 and len(q_pos):
		ans += cal_temp_subarr(start, n, q_pos)
	print(ans)


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
pre = [1 for i in range(N)]
post = [1 for i in range(N)]
for i in range(N):
	count = 0
	k = 1
	while i + k < N and A[i] <= A[i + k]:
		count += 1
		k += 1
	pre[i] += count
for i in range(N - 1, -1, -1):
	count = 0
	k = -1
	while i + k >= 0 and A[i] < A[i + k]:
		count += 1
		k -= 1
	post[i] += count
fin = [pre[i] * post[i] for i in range(N)]
d = {}
for i in range(N):
	try:
		d[A[i]] += fin[i]
	except:
		d[A[i]] = fin[i]
while Q:
	Q -= 1
	k = int(input())
	try:
		print(d[k])
	except:
		print(0)


n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(n):
	m = a[i]
	for j in range(i, n):
		if a[j] < m:
			m = a[j]
		ans.append(m)
for q in range(int(input())):
	k = int(input())
	print(ans.count(k))


n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	d[a[i]] = 0
for i in range(n):
	for j in range(i, n):
		l = a[i:j + 1]
		d[min(l)] += 1
q = int(input())
for _ in range(q):
	x = int(input())
	if x in d:
		print(d[x])
	else:
		print(0)


n = int(input())
l = list(map(int, input().split()))
d = {}
for i in range(n):
	a = []
	x = l[i]
	for j in range(i, n):
		a.append(l[j])
		x = min(x, l[j])
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
q = int(input())
while q > 0:
	b = int(input())
	if b in d:
		print(d[b])
	else:
		print(0)
	q -= 1


t = 1
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in range(n):
		k = l[i]
		for j in range(i, n):
			k = min(k, l[j])
			try:
				d[k] += 1
			except:
				d[k] = 1
	q = int(input())
	for _ in range(q):
		k = int(input())
		try:
			print(d[k])
		except:
			print(0)
	t -= 1


def sub(arr, k, f, n):
	i = j = d
	u = v = 0
	i -= 1
	j += 1
	flag = 0
	while i > -1:
		if arr[i] < k:
			break
		if arr[i] == k:
			flag = 1
			break
		u += 1
		i -= 1
	while j < n:
		if arr[j] < k:
			break
		v += 1
		j += 1
	if flag == 0:
		return v + (v + 1) * u + 1
	else:
		return v + 1
n = int(input())
arr = list(map(int, input().strip().split()))
q = int(input())
for j in range(0, q):
	tot = 0
	k = int(input())
	for d in range(0, n):
		if arr[d] == k:
			tot += sub(arr, k, d, n)
	print(tot)


n = int(input())
l = list(map(int, input().split()))
d = {}
for j in range(n):
	m = l[j]
	for z in range(j, n):
		m = min(m, l[z])
		if m in d:
			d[m] += 1
		else:
			d[m] = 1
c = int(input())
for y in range(c):
	k = int(input())
	print(d[k] if k in d else 0)


t = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
	n = int(input())
	count = 0
	for j in range(t):
		c = []
		for k in range(j, t):
			c.append(a[k])
			if min(c) == n:
				count += 1
	print(count)


import sys
import math

def main(arr, k):
	ans = 0
	for i in range(len(arr)):
		curr_min = float('inf')
		for j in range(i, len(arr)):
			curr_min = min(curr_min, arr[j])
			if curr_min == k:
				ans += 1
	return ans
n = int(input())
arr = list(map(int, input().split()))
for i in range(int(input())):
	print(main(arr, int(input())))


array_size = int(input())
arr = input().split(' ')
arr = list(map(int, arr))
query_no = int(input())
for tc in range(query_no):
	k = int(input())
	count = 0
	result = [[]]
	for i in range(len(arr) + 1):
		for j in range(i + 1, len(arr) + 1):
			sub = arr[i:j]
			result.append(sub)
	for i in result:
		if i != []:
			if k == min(i):
				count += 1
	print(count)


import math

def segmentTree_con(low, high, pos):
	if low == high:
		seg_tree[pos] = arr[low]
	else:
		mid = (low + high) // 2
		left_child = 2 * pos + 1
		right_child = 2 * pos + 2
		segmentTree_con(low, mid, left_child)
		segmentTree_con(mid + 1, high, right_child)
		seg_tree[pos] = min(seg_tree[left_child], seg_tree[right_child])

def query(qlow, qhigh, low, high, pos):
	if qlow <= low and qhigh >= high:
		return seg_tree[pos]
	if qlow > high or qhigh < low:
		return maxsize[0]
	mid = (low + high) // 2
	return min(query(qlow, qhigh, low, mid, 2 * pos + 1), query(qlow, qhigh, mid + 1, high, 2 * pos + 2))
n = int(input())
arr = list(map(int, input().split()))
maxsize = [10 ** 7]
query_list = []
for q in range(int(input())):
	query_list.append(int(input()))
ans_query = [0 for _ in range(len(query_list))]
if n == 1:
	for num in query_list:
		if num in arr:
			print(1)
		else:
			print(0)
else:
	size = math.ceil(math.log(n, 2))
	seg_tree = [maxsize for _ in range(2 ** size * 2 - 1)]
	segmentTree_con(0, n - 1, 0)
	for start in range(n):
		end = start - 1
		for j in range(n - start):
			end += 1
			ret = query(start, end, 0, n - 1, 0)
			for k in range(len(query_list)):
				if ret == query_list[k]:
					ans_query[k] += 1
	for ans in ans_query:
		print(ans)


import itertools, sys

def solve(N, A, K):
	count_sets = lambda g, i: (i + 1) * (len(g) - i)
	groups = [list(g) for (k, g) in itertools.groupby(A, lambda x: x >= K) if k]
	total = 0
	for g in groups:
		if g.count(K) > 0:
			idx = [i for (i, a) in enumerate(g) if a == K]
			for i in range(len(idx)):
				if i == 0:
					total += (idx[i] + 1) * (len(g) - idx[i])
				else:
					total += (idx[i] - idx[i - 1]) * (len(g) - idx[i])
	return total

def solve_brute(N, A, K):
	return sum((1 for i in range(N) for j in range(i, N) if min(A[i:j + 1]) == K))
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
for _ in range(Q):
	K = int(sys.stdin.readline())
	print(solve_brute(N, A, K))


N = int(input())
L = list(map(int, input().split()))
Q = int(input())
M = {}
for start in range(N):
	minn = 10 ** 10
	for end in range(start, N):
		minn = min(minn, L[end])
		if minn in M:
			M[minn] += 1
		else:
			M[minn] = 1
for q in range(Q):
	x = int(input())
	if x in M:
		print(M[x])
	else:
		print(0)


N = int(input())
A = [int(x) for x in input().split()]
for _ in range(int(input())):
	K = int(input())
	total = 0
	for i in range(N):
		a = 0
		count = 0
		forward = 0
		backward = 1
		if A[i] == K:
			if i != A.index(K):
				a = 1
			for j in range(i, N):
				if A[j] >= K:
					forward += 1
				else:
					break
			for j in range(i - 1, -1, -1):
				if a == 1:
					if A[j] > K:
						backward += 1
					else:
						break
				elif a == 0:
					if A[j] >= K:
						backward += 1
					else:
						break
			count += forward * backward
			total += count
	print(total)


def solve(a, k):
	listOfMin = []
	length = len(a)
	for i in range(length):
		for j in range(i + 1, length + 1):
			listOfMin.append(min(a[i:j]))
	count = 0
	for e in listOfMin:
		if e == k:
			count += 1
	return count
sizeOfArray = int(input())
a = []
elements = input().split(' ')
for ele in elements:
	a.append(int(ele))
noOfQueries = int(input())
for i in range(noOfQueries):
	k = int(input())
	print(solve(a, k))


n = int(input())
arr = list(map(int, input().split()))
q = int(input())
for i in range(q):
	num = int(input())
	ans = 0
	for j in range(n):
		if arr[j] == num:
			temp = 1
			count = 1
			for k in range(j + 1, n):
				if min(num, arr[k]) == num:
					count += 1
				else:
					break
			temp *= count
			count = 1
			for k in range(j - 1, -1, -1):
				if min(num, arr[k]) == num:
					if num == arr[k]:
						break
					count += 1
				else:
					break
			temp *= count
			ans += temp
	print(ans)


def getCount(n, ar, lo):
	count = 0
	for i in range(0, n):
		for j in range(i, n):
			sub = ar[i:j + 1]
			if min(sub) == lo:
				count += 1
	return count
nOEl = int(input())
aOEl = [int(x) for x in input().split()]
queC = int(input())
while queC != 0:
	k = int(input())
	print(getCount(nOEl, aOEl, k))
	queC -= 1


n = int(input())
l = list(map(int, input().split()))
q = int(input())
d = {}
for i in range(n):
	item = l[i]
	for j in range(i, n):
		item = min(item, l[j])
		if item in d:
			d[item] += 1
		else:
			d[item] = 1
for i in range(q):
	k = int(input())
	if k in d.keys():
		print(d[k])
	else:
		print(0)


n = int(input())
arr = input().split()
arr = [int(x) for x in arr]
q = int(input())
while q:
	q -= 1
	ans = 0
	k = int(input())
	for i in range(n):
		temp = arr[i]
		for j in range(i, n):
			temp = min(temp, arr[j])
			ans += int(temp == k)
	print(ans)


import sys
t = int(sys.stdin.readline())
a = []
m = []
a = [int(x) for x in input().split()]
q = int(sys.stdin.readline())
for y in range(q):
	m.append(int(sys.stdin.readline()))
for x in m:
	c = 0
	for i in range(t):
		for j in range(i + 1, t + 1):
			if x == min(a[i:j]):
				c += 1
	print(c)


n = int(input())
a = list(map(int, input().split()))
cnt = {}
left = [1]
stack = [[a[0], 1]]
for i in range(1, n):
	curr = 0
	while True:
		if len(stack) == 0:
			curr = i
			break
		if stack[-1][0] > a[i]:
			curr += stack.pop()[1]
		else:
			break
	stack.append([a[i], curr + 1])
	left.append(curr + 1)
a.reverse()
right = [1]
stack = [[a[0], 1]]
for i in range(1, n):
	curr = 0
	while True:
		if len(stack) == 0:
			curr = i
			break
		if stack[-1][0] >= a[i]:
			curr += stack.pop()[1]
		else:
			break
	stack.append([a[i], curr + 1])
	right.append(curr + 1)
right.reverse()
a.reverse()
for i in range(n):
	if a[i] in cnt:
		cnt[a[i]] += left[i] * right[i]
	else:
		cnt[a[i]] = left[i] * right[i]
q = int(input())
for i in range(q):
	x = int(input())
	if x in cnt:
		print(cnt[x])
	else:
		print(0)


n = int(input())
a = [int(word) for word in input().split()]
q = int(input())
for iq in range(q):
	k = int(input())
	ways = 0
	last_lt = -1
	last_leq = -1
	for i in range(n):
		if a[i] < k:
			last_lt = i
		if a[i] <= k:
			last_leq = i
		ways += last_leq - last_lt
	print(ways)


n = int(input())
A = list(map(int, input().split()))
B = []
for i in range(n):
	x = A[i]
	for j in range(i, n):
		x = min(x, A[j])
		B.append(x)
q = int(input())
for i in range(q):
	k = int(input())
	print(B.count(k))


n = int(input())
A = list(map(int, input().split()))
k = int(input())
B = [0] * (max(A) + 1)
for i in range(n):
	y = A[i]
	for j in range(i, n):
		y = min(y, A[j])
		B[y] += 1
for _ in range(k):
	Q = int(input())
	if Q in A:
		print(B[Q])
	else:
		print(0)


n = int(input())
arr = [int(x) for x in input().split()]
q = int(input())
for _ in range(q):
	p = int(input())
	count = 0
	for i in range(n):
		for j in range(i, n):
			if min(arr[i:j + 1]) == p:
				count += 1
	print(count)


n = int(input())
lis = list(map(int, input().split()))
q = int(input())
ans = []
res = []
for i in range(len(lis) + 1):
	ans = []
	for j in range(i + 1, len(lis) + 1):
		ans = lis[i:j]
		res.append(ans)
while q > 0:
	k = int(input())
	count = 0
	for i in range(len(res)):
		if k == min(res[i]):
			count += 1
	print(count)
	q -= 1


n = int(input())
l = list(map(int, input().split()))
k = []
for _ in range(int(input())):
	m = int(input())
	c = 0
	for i in range(n):
		for j in range(i + 1, n + 1):
			if min(l[i:j]) == m:
				c += 1
	print(c)