Submission #538976

#TimeUsernameProblemLanguageResultExecution timeMemory
538976BlundergodSum Zero (RMI20_sumzero)C++17
0 / 100
959 ms708 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; void solve() { int n; cin >> n; vector<long long>x(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> x[i]; } vector<long long>p(n + 1, 0); for (int i = 0; i < n; i++) { p[i] = x[i] + p[i - 1]; } auto solveQuery = [&](int l, int r) { map<long long, long long>Prev; long long sum = 0; vector<int>dp(n + 1, 0); for (int i = l; i <= r; i++) { sum += x[i]; dp[i] = dp[i - 1]; if (sum == 0) { if (Prev[sum] == 0) { dp[i] = 1; } } if (Prev[sum] != 0) { dp[i] = max(dp[i], 1 + dp[Prev[sum]]); } Prev[sum] = i; } cout << *max_element(dp.begin(), dp.end()) << endl; }; int q; cin >> q; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; solveQuery(l, r) ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; T = 1; //cin >> T; while (T--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...