Query on queues

2.1

11 votes
Basics of Stacks, Ready, Hard, Stack, Data Structures, Approved, Grammar-Verified, Stacks
Problem

You are given an array containing N integers and you have to answer K queries. Each query contains an integer X which is the index of the ith (1 based index) element of the queue.

Write a program to determine the following for each query:

  • The number of segments containing the index X as the leftmost or the rightmost element. and the number at the index X is each element of that segment.

Note:

Segment formation example: You have 3 numbers 1, 2, and 3.
The possible segments for 3 are [3], [2,3] and [1,2,3]. Similarly, the possible segments for 2 are [2],[1,2].

Input format

  • First line: T (number of test cases)
  • First line in each test case: Two space-separated integers N and K
  • Second line in each test case: N space-separated integers (denoting the elements of the queue)
  • Next K lines in each test case: X

Output format

Print the answer to each query.

Constraints

1T50
1N,K105
1Each element of the queue109
1XN

Sample Input
1
4 2
4 2 1 3
1
4
Sample Output
4
3
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

For first query all possible valid segments are [4], [4,1] ,[4,1,2] , [4,1,2,3] hence answer is 4.

Editor Image

?