fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25.  
  26. //? Good Subarrays Hashing
  27.  
  28. //* We need to find subarrays : (P[R] - P[L-1]) == R - (L-1)
  29. //* P[R] - R == P[L-1] - (L-1)
  30. //* Hence find P[R]-R in past map of P[i]-i
  31.  
  32.  
  33.  
  34.  
  35. vector<int> a;
  36. void consistency(int n) {
  37.  
  38. unordered_map<int,int> mp = {{1, 1}};
  39. int sum = 0;
  40. int ans = 0;
  41.  
  42. for(int i=0; i<n; i++){
  43. sum += a[i];
  44.  
  45. if(mp.count(sum-i)){
  46. ans += mp[sum-i];
  47. }
  48. mp[sum-i]++;
  49. }
  50.  
  51.  
  52. cout << ans << endl;
  53.  
  54. }
  55.  
  56. void solve() {
  57.  
  58. int n;
  59. cin >> n;
  60. a.resize(n);
  61. for(int i=0; i<n; i++) cin >> a[i];
  62. consistency(n) ;
  63.  
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70. int32_t main() {
  71. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  72.  
  73. int t = 1;
  74. cin >> t;
  75. while (t--) {
  76. solve();
  77. }
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5324KB
stdin
2
5
1 5 3 4 -2
9
1 5 -2 2 3 -1 1 0 4
stdout
2
8