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>
using namespace std;
typedef long long ll;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "[";
for (int i = 0; i < int(v.size()); ++i) {
os << v[i];
if (i != v.size() - 1) {
os << ", ";
}
}
os << "]\n";
return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << "[";
os << p.first;
os << ", ";
os << p.second;
os << "]";
return os;
}
#ifndef ONLINE_JUDGE
#define debug(x) cout << (#x) << " is " << (x) << endl
#else
#define debug(...) 42
#endif
const int maxN = 1e5 + 7;
vector<int>b(maxN);
vector<long long>PrevSum(maxN);
vector<long long>dp(maxN);
void compress(vector<long long>&p) {
vector<int>a;
for (int i = 1; i < int(p.size()); i++) {
a.push_back(p[i]);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for (int i = 0; i < int(p.size()); i++) {
int lower = lower_bound(a.begin(), a.end(), p[i]) - a.begin();
b[i] = lower;
b[i]++;
}
}
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 = 1; i <= n; i++) {
p[i] = x[i] + p[i - 1];
}
compress(p);
auto solveQuery = [&](int l, int r) {
for (int i = l; i <= r; i++) {
PrevSum[b[i]] = -1e9;
}
PrevSum[b[l - 1]] = 0;
dp[l - 1] = 0;
for (int i = l; i <= r; i++) {
dp[i] = dp[i - 1];
dp[i] = max( dp[i] , PrevSum[b[i]] + 1 );
PrevSum[b[i]] = max(PrevSum[b[i]] , dp[i]);
}
cout << dp[r] << 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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |