Minimum and Maximum

All submissions for this problem are available.
### Read problems statements in [Hindi](http://www.codechef.com/download/translated/COOK108/hindi/MNMXAR.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/COOK108/mandarin/MNMXAR.pdf), [Russian](http://www.codechef.com/download/translated/COOK108/russian/MNMXAR.pdf), [Vietnamese](http://www.codechef.com/download/translated/COOK108/vietnamese/MNMXAR.pdf), and [Bengali](http://www.codechef.com/download/translated/COOK108/bengali/MNMXAR.pdf) as well. Chef has a $permutation$ $P_1, P_2, \ldots, P_N$ of integers $1$ through $N$. He needs you to find the value of the sum $$S = \sum\limits_{i=1}^{N} \sum\limits_{j=i}^{N} \mathrm{getMin}\,(i, j) \; \wedge \; \mathrm{getMax}\,(i, j) \,,$$ where $\wedge$ denotes the bitwise AND operation and $$\mathrm{getMin}\,(i, j) = \mathrm{min}\,(P_i, P_{i+1}, \ldots, P_{j1}, P_j) \,,$$ $$\mathrm{getMax}\,(i, j) = \mathrm{max}\,(P_i, P_{i+1}, \ldots, P_{j1}, P_j) \,.$$ ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains a single integer $N$.  The second line contains $N$ spaceseparated integers $P_1, P_2, \ldots, P_N$. ### Output For each test case, print a single line containing one integer $S$. ### Constraints  $1 \le T \le 2$  $1 \le N \le 10^5$  $1 \le P_i \le N$ for each valid $i$  $P_1, P_2, \ldots, P_N$ is a permutation ### Example Input ``` 1 3 2 1 3 ``` ### Example Output ``` 8 ```Author:  ezio_26 
Editorial  https://discuss.codechef.com/problems/MNMXAR 
Tags  cook108, ezio_26, lazypropagation, segmenttree, stack, taran_1407 
Date Added:  11072019 
Time Limit:  3 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 