CF1550C.Manhattan Subarrays

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

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)=∣xp−xq∣+∣yp−yq∣d(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 1≤l≤r≤n1 \le l \le r \le n.

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

假设你有两个点 p=(xp,yp)p = (x_p, y_p) 和 q=(xq,yq)q = (x_q, y_q)。我们记它们之间的曼哈顿距离为 d(p,q)=∣xp−xq∣+∣yp−yq∣d(p, q) = |x_p - x_q| + |y_p - y_q|。

若三个点 pp、qq、rr 满足 d(p,r)=d(p,q)+d(q,r)d(p, r) = d(p, q) + d(q, r),则称它们构成一个坏三元组(bad triple)。

若对于数组 b1,b2,…,bmb_1, b_2, \dots, b_m,无法选出三个互不相同的下标 ii、jj、kk,使得点 (bi,i)(b_i, i)、(bj,j)(b_j, j) 和 (bk,k)(b_k, k) 构成一个坏三元组,则称该数组为好数组(good array)。

现给定一个数组 a1,a2,…,ana_1, a_2, \dots, a_n。请计算 aa 的好子数组(good subarray)的个数。数组 aa 的一个子数组定义为对某个满足 1≤l≤r≤n1 \le l \le r \le n 的 l,rl, r,所构成的数组 al,al+1,…,ara_l, a_{l + 1}, \dots, a_r。

注意:根据定义,长度为 11 和 22 的子数组均为好子数组。

输入格式

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

The first line of each test case contains one integer nn (1≤n≤2⋅1051 \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 (1≤ai≤1091 \le a_i \le 10^9).

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

第一行包含一个整数 tt(1≤t≤50001 \le t \le 5000)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

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

对于每个测试用例,输出数组 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))=∣4−3∣+∣2−4∣=3d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 $ \lt $ 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)) $ \lt $ 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)) $ \lt $ 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))=∣6−9∣+∣1−4∣=6d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
  • d((a1,1),(a2,2))=∣6−9∣+∣1−2∣=4d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
  • d((a2,2),(a4,4))=∣9−9∣+∣2−4∣=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)).

在第一个测试用例中,可以证明数组 aa 的任意子数组都是“好”的。例如,子数组 [a2,a3,a4][a_2, a_3, a_4] 是“好”的,因为它仅包含三个元素,且满足:

  • d((a2,2),(a4,4))=∣4−3∣+∣2−4∣=3d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 $ \lt $ 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)) $ \lt $ 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)) $ \lt $ d((a3,3),(a2,2))+d((a2,2),(a4,4))d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));

在第二个测试用例中,例如子数组 [a1,a2,a3,a4][a_1, a_2, a_3, a_4] 不是“好”的,因为它包含一个“坏”三元组 (a1,1)(a_1, 1)、(a2,2)(a_2, 2)、(a4,4)(a_4, 4):

  • d((a1,1),(a4,4))=∣6−9∣+∣1−4∣=6d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
  • d((a1,1),(a2,2))=∣6−9∣+∣1−2∣=4d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
  • d((a2,2),(a4,4))=∣9−9∣+∣2−4∣=2d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;

因此,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))。

输入解题思路,AI测评打分。不知道怎么写?

首页