E. Special Elements
Pay attention to the non-standard memory limit in this problem.
In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.
The array a=[a1,a2,…,an]a=[a1,a2,…,an] (1≤ai≤n1≤ai≤n) is given. Its element aiai is called special if there exists a pair of indices ll and rr (1≤l<r≤n1≤l<r≤n) such that ai=al+al+1+…+arai=al+al+1+…+ar. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not).
Print the number of special elements of the given array aa.
For example, if n=9n=9 and a=[3,1,4,1,5,9,2,6,5]a=[3,1,4,1,5,9,2,6,5], then the answer is 55:
- a3=4a3=4 is a special element, since a3=4=a1+a2=3+1a3=4=a1+a2=3+1;
- a5=5a5=5 is a special element, since a5=5=a2+a3=1+4a5=5=a2+a3=1+4;
- a6=9a6=9 is a special element, since a6=9=a1+a2+a3+a4=3+1+4+1a6=9=a1+a2+a3+a4=3+1+4+1;
- a8=6a8=6 is a special element, since a8=6=a2+a3+a4=1+4+1a8=6=a2+a3+a4=1+4+1;
- a9=5a9=5 is a special element, since a9=5=a2+a3=1+4a9=5=a2+a3=1+4.
Please note that some of the elements of the array aa may be equal — if several elements are equal and special, then all of them should be counted in the answer.
The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. Then tt test cases follow.
Each test case is given in two lines. The first line contains an integer nn (1≤n≤80001≤n≤8000) — the length of the array aa. The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n).
It is guaranteed that the sum of the values of nn for all test cases in the input does not exceed 80008000.
Print tt numbers — the number of special elements for each of the given arrays.
5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1
5 1 0 4 0
时间卡的比较紧,如果用map存会超时,用unordered_map没问题
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> //#include <unordered_set> #include <unordered_map> //#include <bits/stdc++.h> //#include <xfunctional> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mk make_pair using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979; const int mod = 998244353; const int N = 2e5+5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m%n); } ll lcm(ll m, ll n) { return m*n / gcd(m, n); } int main() { int T; cin >> T; while (T--) { int n; cin >> n; vector<int> a(n + 1); unordered_map<int, int> mp; rep(i, 1, n) { a[i]=read(); a[i] += a[i - 1]; } rep(i, 0, n) { ll sum=0; rep(j, i+2, n) { if(a[j]-a[i]<=n) mp[a[j] - a[i]]++; } } int cnt = 0; rep(i, 1, n) { if (mp[a[i] - a[i - 1]]) cnt++; } cout << cnt << endl; } return 0; }