You are given an array which is initially a permutation of $$$n$$$ elements: $$$A=[a_1, a_2, \dots, a_n]$$$.
The array can be modified using the following operation:
Print the number of different arrays that you can get by performing the given operation any number of times (possibly zero) modulo $$$998244353$$$.
The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 5000$$$).
The following line contains $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$). It is guaranteed that $$$[a_1, a_2, \dots, a_n]$$$ forms a permutation of size $$$n$$$.
Print the answer modulo $$$998244353$$$.
5 1 2 3 4 5
16
5 3 5 2 4 1
9
Given an array of $$$n$$$ distinct elements, $$$L$$$ is said to be a left terminal minima for $$$R$$$ if $$$min(a[L ... R]) = a[L]$$$
For each $$$R$$$ from $$$1$$$ to $$$n$$$, print the number of indices $$$L$$$ ($$$L \lt R$$$) such that $$$L$$$ is a left terminal minima for $$$R$$$.
For each test case, print the answer in a single line
2 9 1 7 2 8 3 9 5 6 4 9 4 3 7 9 1 2 5 6 8
0 1 1 2 2 3 3 4 3 0 0 1 2 0 1 2 3 4
Given an array of $$$n$$$ distinct elements, $$$R$$$ is said to be a right terminal minima for $$$L$$$ if $$$min(a[L ... R]) = a[R]$$$
For each $$$R$$$ from $$$1$$$ to $$$n$$$, print the number of indices $$$L$$$ ($$$L \lt R$$$) such that $$$R$$$ is a right terminal minima for $$$L$$$.
For each test case, print the answer in a single line
2 9 1 7 2 8 3 9 5 6 4 9 4 3 7 9 1 2 5 6 8
0 0 1 0 1 0 1 0 3 0 1 0 0 4 0 0 0 0
You are given an array $$$[p_1, p_2, \dots, p_n]$$$, where all elements are distinct.
You can perform several (possibly zero) operations with it. In one operation, you can choose a contiguous subsegment of $$$p$$$ and remove all elements from that subsegment, except for the minimum element on that subsegment. For example, if $$$p = [3, 1, 4, 7, 5, 2, 6]$$$ and you choose the subsegment from the $$$3$$$-rd element to the $$$6$$$-th element, the resulting array is $$$[3, 1, 2, 6]$$$.
An array $$$a$$$ is called reachable if it can be obtained from $$$p$$$ using several (maybe zero) aforementioned operations. Calculate the number of reachable arrays, and print it modulo $$$998244353$$$.
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$). The second line contains $$$n$$$ distinct integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le 10^9$$$).
Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, print one integer — the number of reachable arrays, taken modulo $$$998244353$$$.
322 142 4 1 3510 2 6 3 4
2 6 12
You are given an array $$$A$$$ of size $$$N$$$, each integer representing the height of a vertical bar of a histogram. The width of each bar is 1.
You are required to find the area of the largest rectangle that can be completely fit inside the histogram.
Let's define $$$left\_extension[i]$$$ to be the number of consecutive bars to the left of $$$i^{th}$$$ bar that are greater than or equal to the $$$i^{th}$$$ bar. Show how to compute the $$$left\_extension$$$ array in $$$O(n \cdot n)$$$. Hint: Start from the previous element, and keep iterating towards the left until you reach a element that is strictly smaller.
Now, let's define $$$right\_extension[i]$$$ to be the number of consecutive bars to the right of the $$$i^{th}$$$ bar that are greater than or equal to the the $$$i^{th}$$$ bar. Show to compute $$$right\_extension$$$ array in $$$O(n \cdot n)$$$.
Finally, let's define $$$cover[i]$$$ to be the size of the largest rectangle which completely contains the $$$i^{th}$$$ vertical bar inside it. Show how to compute the $$$cover$$$ array in $$$O(n)$$$.
Using the $$$cover$$$ array, come up with an algorithm to find the largest rectangle in the histogram in $$$O(n)$$$.
Constraints
$$$1 \leq T \leq 10^3$$$
$$$1 \leq N \leq 10^3$$$
$$$1 \leq A[i] \leq 10^9$$$
Sum of $$$N$$$ over all testcases does not exceed $$$10^3$$$
The first line contains a single integer, $$$T$$$, the number of testcases
The first line of each testcase contains an integer, $$$N$$$, the size of the array
The second line of each testcase contains $$$N$$$ integers
You are required to print 4 lines of output per testcase.
The first line will contain $$$N$$$ integers, the $$$i^{th}$$$ integer corresponding to the value of $$$left\_extension[i]$$$
The second line will contain $$$N$$$ integers, the $$$i^{th}$$$ integer corresponding to the value of $$$right\_extension[i]$$$
The third line will contain $$$N$$$ integers, the $$$i^{th}$$$ integer corresponding to the value of $$$cover[i]$$$
The fourth line will contain a single integer, corresponding to the area of the largest rectangle that can be completely fit inside the histogram
1 7 2 1 4 5 1 3 3
0 1 0 0 4 0 1 0 5 1 0 2 1 0 2 7 8 5 7 6 6 8
You are given an array $$$A$$$ of size $$$N$$$, each integer representing the height of a vertical bar of a histogram. The width of each bar is 1.
You are required to find the area of the largest rectangle that can be completely fit inside the histogram.
Let's define $$$left\_extension[i]$$$ to be the number of consecutive bars to the left of $$$i^{th}$$$ bar that are greater than or equal to the $$$i^{th}$$$ bar. Show how to compute the $$$left\_extension$$$ array in $$$O(n)$$$.
Hint: Consider the array $$$[0, 6, 5, 4, 3, 2, 1]$$$. While computing the left extension of 1, we need to keep going to the left until we find a smaller element. The idea is to delete all the elements that we meet on our journey so that the next time we need to go through less number of elements. When we start at 1, we will be able to delete 2, 3, 4, 5, and 6. We claim that these deletions do not affect the answer for any elements that arrive after 1. This is because, if the incoming element is strictly greater than 1, they would never have been able to cross past 1. And if they were smaller than 1, then they have the power to go at least as far as 1 went, i.e, they would have also deleted 2, 3, 4, 5, 6. If we follow this deletion strategy, we will get a sequence which is strictly increasing, except the last element. And each consecutive element in this sequence is the boundary for the left extension.
So, use a stack of indices to maintain this order. This is known as a monotonic stack. If you want to refer to online resources for more hints, you can search for Stock Span. (The $$$left\_extension$$$ array is similar to the stock span array).
Now, let's define $$$right\_extension[i]$$$ to be the number of consecutive bars to the right of the $$$i^{th}$$$ bar that are greater than or equal to the the $$$i^{th}$$$ bar. Show to compute $$$right\_extension$$$ array in $$$O(n)$$$.
Finally, let's define $$$cover[i]$$$ to be the size of the largest rectangle which completely contains the $$$i^{th}$$$ vertical bar inside it. Show how to compute the $$$cover$$$ array in $$$O(n)$$$.
Using the $$$cover$$$ array, come up with an algorithm to find the largest rectangle in the histogram in $$$O(n)$$$.
Constraints
$$$1 \leq T \leq 10^6$$$
$$$1 \leq N \leq 10^6$$$
$$$1 \leq A[i] \leq 10^9$$$
Sum of $$$N$$$ over all testcases does not exceed $$$10^6$$$
The first line contains a single integer, $$$T$$$, the number of testcases
The first line of each testcase contains an integer, $$$N$$$, the size of the array
The second line of each testcase contains $$$N$$$ integers, the $$$i^{th}$$$ integer corresponding to the value of $$$A[i]$$$
You are required to print 4 lines of output per testcase.
The first line will contain $$$N$$$ integers, the $$$i^{th}$$$ integer corresponding to the value of $$$left\_extension[i]$$$
The second line will contain $$$N$$$ integers, the $$$i^{th}$$$ integer corresponding to the value of $$$right\_extension[i]$$$
The third line will contain $$$N$$$ integers, the $$$i^{th}$$$ integer corresponding to the value of $$$cover[i]$$$
The fourth line will contain a single integer, corresponding to the area of the largest rectangle that can be completely fit inside the histogram
1 7 2 1 4 5 1 3 3
0 1 0 0 4 0 1 0 5 1 0 2 1 0 2 7 8 5 7 6 6 8
You are given an array a consisting of n elements. The imbalance value of some subsegment of this array is the difference between the maximum and minimum element from this segment. The imbalance value of the array is the sum of imbalance values of all subsegments of this array.
For example, the imbalance value of array [1, 4, 1] is 9, because there are 6 different subsegments of this array:
You have to determine the imbalance value of the array a.
The first line contains one integer n (1 ≤ n ≤ 106) — size of the array a.
The second line contains n integers a1, a2... an (1 ≤ ai ≤ 106) — elements of the array.
Print one integer — the imbalance value of a.
3
1 4 1
9