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 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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |