제출 #493596

#제출 시각아이디문제언어결과실행 시간메모리
493596Jeff12345121Sum Zero (RMI20_sumzero)C++14
22 / 100
54 ms512 KiB
#include <bits/stdc++.h> #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]; long long s[nmax]; int dp[nmax],vec[nmax]; pp qs[nmax]; void solve(int l , int r) { for (int i = l; i <= r; i++) { dp[i] = -inf; } dp[l - 1] = 0; for (int i = l; i <= r; i++) vec[s[i]] = -inf; vec[s[l - 1]] = 0; for (int i = l; i <= r; i++) { dp[i] = dp[i - 1]; dp[i] = max( dp[i] , vec[s[i]] + 1 ); vec[s[i]] = max(vec[s[i]] , dp[i]); } out << dp[r] << "\n"; } pair<long,int> Norm[nmax]; void normalize_s() { for (int i = 0; i <= n; i++) Norm[i] = {s[i] , i}; sort(Norm , Norm + 1 + n); int poz = 0; for (int i = 0; i <= n; i++) { if (i == 0 || Norm[i].first != Norm[i - 1].first) { s[Norm[i].second] = ++poz; } else { s[Norm[i].second] = poz; } } } 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]; } normalize_s(); 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...