CF1550C.Manhattan Subarrays

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Suppose you have two points p=(xp,yp)p = (x_p, y_p) and q=(xq,yq)q = (x_q, y_q) . Let's denote the Manhattan distance between them as d(p,q)=xpxq+ypyqd(p, q) = |x_p - x_q| + |y_p - y_q| .

Let's say that three points pp , qq , rr form a bad triple if d(p,r)=d(p,q)+d(q,r)d(p, r) = d(p, q) + d(q, r) .

Let's say that an array b1,b2,,bmb_1, b_2, \dots, b_m is good if it is impossible to choose three distinct indices ii , jj , kk such that the points (bi,i)(b_i, i) , (bj,j)(b_j, j) and (bk,k)(b_k, k) form a bad triple.

You are given an array a1,a2,,ana_1, a_2, \dots, a_n . Calculate the number of good subarrays of aa . A subarray of the array aa is the array al,al+1,,ara_l, a_{l + 1}, \dots, a_r for some 1lrn1 \le l \le r \le n .

Note that, according to the definition, subarrays of length 11 and 22 are good.

输入格式

The first line contains one integer tt ( 1t50001 \le t \le 5000 ) — the number of test cases.

The first line of each test case contains one integer nn ( 1n21051 \le n \le 2 \cdot 10^5 ) — the length of array aa .

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ai1091 \le a_i \le 10^9 ).

It's guaranteed that the sum of nn doesn't exceed 21052 \cdot 10^5 .

输出格式

For each test case, print the number of good subarrays of array aa .

输入输出样例

  • 输入#1

    3
    4
    2 4 1 3
    5
    6 9 1 9 6
    2
    13 37

    输出#1

    10
    12
    3

说明/提示

In the first test case, it can be proven that any subarray of aa is good. For example, subarray [a2,a3,a4][a_2, a_3, a_4] is good since it contains only three elements and:

  • d((a2,2),(a4,4))=43+24=3d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 << d((a2,2),(a3,3))+d((a3,3),(a4,4))=3+1+2+1=7d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7 ;
  • d((a2,2),(a3,3))d((a_2, 2), (a_3, 3)) << d((a2,2),(a4,4))+d((a4,4),(a3,3))d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3)) ;
  • d((a3,3),(a4,4))d((a_3, 3), (a_4, 4)) << d((a3,3),(a2,2))+d((a2,2),(a4,4))d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4)) ;

In the second test case, for example, subarray [a1,a2,a3,a4][a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a1,1)(a_1, 1) , (a2,2)(a_2, 2) , (a4,4)(a_4, 4) :

  • d((a1,1),(a4,4))=69+14=6d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6 ;
  • d((a1,1),(a2,2))=69+12=4d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4 ;
  • d((a2,2),(a4,4))=99+24=2d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2 ;

So, d((a1,1),(a4,4))=d((a1,1),(a2,2))+d((a2,2),(a4,4))d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)) .

首页