luogu#P5161. WD与数列

WD与数列

Background

WD spends all day immersed in sequences and cannot stop.

Problem Description

WD really likes sequences. He thinks two sequences A,BA, B match if and only if A=B|A| = |B| and for 1i,jA1 \le i, j \le |A|, AiBi=AjBjA_i - B_i = A_j - B_j. That is, they have the same length, and by adding the same number to every element of one sequence, it can become exactly the other sequence.

Now CX gives him a large sequence of length nn. WD wants to know how many pairs of non-overlapping subarrays in the sequence are matching.

Input Format

The first line contains an integer nn, representing the length of the sequence. The second line contains nn integers, representing the numbers in the sequence.

Output Format

Output one integer in a single line, the number of matching subarrays.

5
1 2 3 4 5
13
10
1 0 -1 -1 -2 -2 -3 -3 -4 -5
65

Hint

For the sample, any two non-overlapping subarrays with the same length are matching. For length 11 there are 1010 pairs, and for length 22 there are 33 pairs, so there are 1313 pairs in total.

subtask1(11pts): 1n100subtask1(11pts):~1\le n\le 100

subtask2(34pts): 1n1,000subtask2(34pts):~1\le n\le 1,000

subtask3(55pts): 1n300,000subtask3(55pts):~1\le n\le 300,000

For all data, the absolute value of each number in the sequence is 109\le 10^9. The time limit for subtask3subtask3 is 3s3\text{s}, and for the others it is 1s1\text{s}.

Translated by ChatGPT 5