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;
typedef pair<int, int> pii;
const int MAX = 2e6 + 15;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ": " << "[ " << x << " ]\n"
int n, v[MAX], dp[MAX], m[MAX], q;
int query(int l, int r){
for(int i = l; i <= r; i++) m[i] = -1, dp[i] = 0;
map<int, int> sums;
sums[0] = l - 1, dp[l - 1] = 0;
for(int i = l, s = 0; i <= r; i++){
s += v[i];
if(sums.find(s) != sums.end())
m[i] = sums[s];
sums[s] = i;
}
for(int i = l; i <= r; i++){
if(m[i] == -1) dp[i] = dp[i - 1];
else dp[i] = max(dp[i - 1], dp[m[i]] + 1);
}
return dp[r];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> v[i];
cin >> q;
while(q--){
int l, r; cin >> l >> r;
cout << query(l, r) << '\n';
}
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... |