제출 #493588

#제출 시각아이디문제언어결과실행 시간메모리
493588Jeff12345121Sum Zero (RMI20_sumzero)C++14
22 / 100
785 ms852 KiB
#include <bits/stdc++.h> #define int long long #define ff first #define ss second #define in cin #define out cout using namespace std; typedef pair<int,int> pp; const int nmax = 5005; const int inf = 1000000005; int n,q; int v[nmax],s[nmax],dp[nmax]; pp qs[nmax]; void solve(int l , int r) { for (int i = l; i <= r; i++) { dp[i] = -inf; } dp[l - 1] = 0; unordered_map<int,int> vec; vec[s[l - 1]] = 0; for (int i = l; i <= r; i++) { dp[i] = dp[i - 1]; if (vec.find(s[i]) != vec.end()) { dp[i] = max( dp[i] , max(0LL , vec[s[i]]) + 1 ); vec[s[i]] = max(vec[s[i]] , dp[i]); } else { dp[i] = max(dp[i] , -inf); vec[s[i]] = dp[i]; } } out << dp[r] << "\n"; } int32_t main() { in >> n; for (int i = 1; i <= n; i++) { in >> v[i]; } for (int i = 1; i <= n; i++) { s[i] = v[i] + s[i - 1]; } in >> q; for (int i = 1; i <= q; i++) { in >> qs[i].ff >> qs[i].ss; } for (int i = 1; i <= q; i++) { solve(qs[i].ff , qs[i].ss); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...