This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |