#P12808. [AMPPZ 2019] Bookstore
[AMPPZ 2019] Bookstore
Description
You own a very peculiar bookstore, which sells old books, but you store all of them on a single shelf, in random order, and you do not care about the books’ content. Nor do your clients – they tend to come into the store and simply ask for “all the books on that shelf, starting from this one and ending here”. To be precise, every client buys some connected (and non-empty) fragment of books from the shelf.
Sometimes, though, you get more picky clients, who expect more from a book – actually, they expect it to have the right size. A picky client wants a fragment of shelf in which all the books have their height not smaller than and not greater than .
Given a sequence of integers – the heights of all the books on the shelf – determine the number of possible connected fragments which satisfy these requirements.
Also, we mentioned that the books are in random order. Formally, the input sequence was generated with the following program, for some values of and with .
srand48(N + M);
for (int i = 0; i < N; ++i)
a[i] = 1 + lrand48() % M;
You do not actually need to know how the RAND48 library works. It is enough to assume that the function lrand48 returns 31-bit non-negative integers picked uniformly at random.
Input Format
The first line of input contains the number of test cases (). The test cases follow, each one in the following format:
- The first line of a test case contains the number of books and the number of picky clients (, ).
- The second line contains a sequence of positive integers not exceeding 1 000 000 – the heights of all the books, from the first (leftmost) to the last (rightmost) one.
- The final lines describe the clients’ requirements. The -th of these lines contains two integers (), describing a client that wants books to be not smaller than and not greater than .
The total number of books in all test cases does not exceed 600 000, and the total number of clients in all test cases does not exceed 1 500 000.
Output Format
For every client, output the number of non-empty connected fragments of the book sequence which satisfy the client’s requirements.
2
10 3
9 9 3 2 1 9 6 9 1 7
1 13
6 6
2 9
5 1
66575 45720 67904 18764 35162
20000 80000
55
1
17
7
京公网安备 11011102002149号