CF1550C.Manhattan Subarrays
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Suppose you have two points p=(xp,yp) and q=(xq,yq) . Let's denote the Manhattan distance between them as d(p,q)=∣xp−xq∣+∣yp−yq∣ .
Let's say that three points p , q , r form a bad triple if d(p,r)=d(p,q)+d(q,r) .
Let's say that an array b1,b2,…,bm is good if it is impossible to choose three distinct indices i , j , k such that the points (bi,i) , (bj,j) and (bk,k) form a bad triple.
You are given an array a1,a2,…,an . Calculate the number of good subarrays of a . A subarray of the array a is the array al,al+1,…,ar for some 1≤l≤r≤n .
Note that, according to the definition, subarrays of length 1 and 2 are good.
输入格式
The first line contains one integer t ( 1≤t≤5000 ) — the number of test cases.
The first line of each test case contains one integer n ( 1≤n≤2⋅105 ) — the length of array a .
The second line of each test case contains n integers a1,a2,…,an ( 1≤ai≤109 ).
It's guaranteed that the sum of n doesn't exceed 2⋅105 .
输出格式
For each test case, print the number of good subarrays of array a .
输入输出样例
输入#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 a is good. For example, subarray [a2,a3,a4] is good since it contains only three elements and:
- d((a2,2),(a4,4))=∣4−3∣+∣2−4∣=3 < d((a2,2),(a3,3))+d((a3,3),(a4,4))=3+1+2+1=7 ;
- d((a2,2),(a3,3)) < d((a2,2),(a4,4))+d((a4,4),(a3,3)) ;
- d((a3,3),(a4,4)) < d((a3,3),(a2,2))+d((a2,2),(a4,4)) ;
In the second test case, for example, subarray [a1,a2,a3,a4] is not good, since it contains a bad triple (a1,1) , (a2,2) , (a4,4) :
- d((a1,1),(a4,4))=∣6−9∣+∣1−4∣=6 ;
- d((a1,1),(a2,2))=∣6−9∣+∣1−2∣=4 ;
- d((a2,2),(a4,4))=∣9−9∣+∣2−4∣=2 ;
So, d((a1,1),(a4,4))=d((a1,1),(a2,2))+d((a2,2),(a4,4)) .