fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int n, m;
  9. cin >> n >> m;
  10.  
  11. vector<int> a(n+1), b(m+1);
  12. for (int i = 1; i <= n; i++) cin >> a[i];
  13. for (int i = 1; i <= m; i++) cin >> b[i];
  14.  
  15. long long countZero = 0;
  16.  
  17. auto process = [&](int i, int j) {
  18. long long sum = 0;
  19. while (i <= n && j <= m) {
  20. if (a[i] != b[j]) break;
  21. sum += a[i];
  22. if (sum == 0) countZero++;
  23. i++; j++;
  24. }
  25. };
  26.  
  27. // Các đường chéo xuất phát từ a[1..n] với b[1]
  28. for (int i = 1; i <= n; i++)
  29. process(i, 1);
  30.  
  31. // Các đường chéo xuất phát từ a[1] với b[2..m]
  32. for (int j = 2; j <= m; j++)
  33. process(1, j);
  34.  
  35. cout << countZero;
  36. return 0;
  37. }
  38.  
Runtime error #stdin #stdout 1.13s 2095992KB
stdin
Standard input is empty
stdout
Standard output is empty