Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解
Posted Tisfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解相关的知识,希望对你有一定的参考价值。
目录
Educational Codeforces Round 111 (Rated for Div. 2)-C. Manhattan Subarrays
传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes
Problem Description
Suppose you have two points p = ( x p , y p ) p = (x_p, y_p) p=(xp,yp) and q = ( x q , y q ) q = (x_q, y_q) q=(xq,yq). Let’s denote the Manhattan distance between them as d ( p , q ) = ∣ x p − x q ∣ + ∣ y p − y q ∣ d(p, q) = |x_p - x_q| + |y_p - y_q| d(p,q)=∣xp−xq∣+∣yp−yq∣.
Let’s say that three points p p p, q q q, r r r form a bad triple if d ( p , r ) = d ( p , q ) + d ( q , r ) d(p, r) = d(p, q) + d(q, r) d(p,r)=d(p,q)+d(q,r).
Let’s say that an array b 1 , b 2 , … , b m b_1, b_2, \\dots, b_m b1,b2,…,bm is good if it is impossible to choose three distinct indices i i i, j j j, k k k such that the points ( b i , i ) (b_i, i) (bi,i), ( b j , j ) (b_j, j) (bj,j) and ( b k , k ) (b_k, k) (bk,k) form a bad triple.
You are given an array a 1 , a 2 , … , a n a_1, a_2, \\dots, a_n a1,a2,…,an. Calculate the number of good subarrays of a a a. A subarray of the array a a a is the array a l , a l + 1 , … , a r a_l, a_{l + 1}, \\dots, a_r al,al+1,…,ar for some 1 ≤ l ≤ r ≤ n 1 \\le l \\le r \\le n 1≤l≤r≤n.
Note that, according to the definition, subarrays of length 1 1 1 and 2 2 2 are good.
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 5000 1 \\le t \\le 5000 1≤t≤5000) — the number of test cases.
The first line of each test case contains one integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \\le n \\le 2 \\cdot 10^5 1≤n≤2⋅105) — the length of array a a a.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \\dots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \\le a_i \\le 10^9 1≤ai≤109).
It’s guaranteed that the sum of n n n doesn’t exceed 2 ⋅ 1 0 5 2 \\cdot 10^5 2⋅105.
Output
For each test case, print the number of good subarrays of array a a a.
Sample Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Sample Onput
10
12
3
Note
In the first test case, it can be proven that any subarray of a a a is good. For example, subarray [ a 2 , a 3 , a 4 ] [a_2, a_3, a_4] [a2,a3,a4] is good since it contains only three elements and:
In the second test case, for example, subarray [ a 1 , a 2 , a 3 , a 4 ] [a_1, a_2, a_3, a_4] [a1,a2,a3,a4] is not good, since it contains a bad triple ( a 1 , 1 ) (a_1, 1) (a1,1), ( a 2 , 2 ) (a_2, 2) (a2,2), ( a 4 , 4 ) (a_4, 4) (a4,4):
So, d ( ( a 1 , 1 ) , ( a 4 , 4 ) ) = d ( ( a 1 , 1 ) , ( a 2 , 2 ) ) + d ( ( a 2 , 2 ) , ( a 4 , 4 ) ) d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)) d((a1,1),(a4,4))=d((a1,1),(a2,2))+d((a2,2),(a4,4)).
题目大意
给你 n n n个数,问你这 n n n个数组成的序列中,有多少个子序列(连续)满足:
- 这个子序列中任选3个数(下标为 i , j , k i,j,k i,j,k 且不同),都不满足:
- d ( P [ i ] , P [ j ] ) + d ( P [ j ] , P [ k ] ) = d ( P [ i ] , P [ k ] ) d(P[i], P[j]) + d(P[j], P[k]) = d(P[i], P[k]) d(P[i],P[j])+d(P[j]以上是关于Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #717 (Div. 2)-A. Tit for Tat-题解
区间倍增dpD. Cut——Codeforces Round #717 (Div. 2)
Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解
Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解
Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解
Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解