End-Balanced Subarrays

시간 제한1초메모리 제한1024 MB

요약
길이가 2 이상인 부분 배열 가운데 양 끝 원소의 합이 그 사이 원소들의 합과 같은 것의 개수를 센다.
난이도

보통10점 중 6점

유형
누적 합, 해시맵, 배열, 수학
정답자
아직 제출이 없습니다

문제

You are given an array aa of nn integers. A sub-array \[a_l,a_l+1,⋯a_r]\[a\_l, a\_{l+1}, \cdots a\_r] is considered end-balanced if l\<rl\<r and a_l+a_r=a_l+1+...+a_r−1a\_l + a\_r = a\_{l+1} + ... + a\_{r-1}.

For example, the subarrays \[4,9,5]\[4, 9, 5], \[−1,3,5,9]\[-1, 3, 5, 9], and \[0,0]\[0, 0] are considered end-balanced, and the subarrays \[0]\[0], \[−2,−3,−5]\[-2, -3, -5], and \[1,1]\[1, 1] are not.

How many subarrays of aa are end-balanced?

입력

The first line of the input contains a single integer tt (1≤t≤1041 \le t \le 10^4) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2\cdot 10^5) --- the size of the array aa.

The second line of each test case contains nn integers a_1,a_2⋯a_na\_1, a\_2 \cdots a\_n (−109≤a_i≤109-10^9 \le a\_i \le 10^9) --- the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

출력

For each test case, print a single integer --- the number of end-balanced subarrays of aa.

힌트

The end-balanced subarrays in the first test case are:

  • \[a_1,a_2,a_3,a_4]=\[1,2,3,4]\[a\_1, a\_2, a\_3, a\_4] = \[1, 2, 3, 4]
  • \[a_2,a_3,a_4,a_5]=\[2,3,4,5]\[a\_2, a\_3, a\_4, a\_5] = \[2, 3, 4, 5]

The end-balanced subarrays in the second test case are:

  • \[a_1,a_2]=\[0,0]\[a\_1, a\_2] = \[0, 0]
  • \[a_2,a_3]=\[0,0]\[a\_2, a\_3] = \[0, 0]
  • \[a_1,a_2,a_3]=\[0,0,0]\[a\_1, a\_2, a\_3] = \[0, 0, 0]

The end-balanced subarrays in the third test case are:

  • \[a_2,a_3]=\[5,−5]\[a\_2, a\_3] = \[5, -5]
  • \[a_1,a_2,a_3,a_4]=\[−10,5,−5,10]\[a\_1, a\_2, a\_3, a\_4] = \[-10, 5, -5, 10]

예제1

  1. 예제 1

    입력
    7
    5
    1 2 3 4 5
    3
    0 0 0
    4
    -10 5 -5 10
    6
    2 2 2 2 2 2
    7
    1 0 1 0 1 0 1
    5
    1000000000 1000000000 1000000000 1000000000 1000000000
    1
    -1000000000
    
    예상 출력
    2
    3
    2
    3
    5
    2
    0