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;
const int maxn = 400005;
const int inf = 1e9;
int n, q;
int a[maxn];
pair<long long, int> suffix[maxn];
int nxt[maxn];
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
suffix[n + 1] = make_pair(0LL, n + 1);
for (int i = n; i >= 1; --i)
{
suffix[i].first = suffix[i + 1].first + a[i];
suffix[i].second = i;
}
sort(suffix + 1, suffix + n + 2);
for (int i = n + 1; i >= 1; --i)
{
nxt[i] = inf;
}
for (int i = 1; i <= n; ++i)
{
if (suffix[i].first == suffix[i + 1].first)
{
nxt[suffix[i].second] = suffix[i + 1].second;
}
}
for (int i = n; i >= 1; --i)
{
nxt[i] = min(nxt[i], nxt[i + 1]);
}
cin >> q;
for (int i = 1; i <= q; ++i)
{
int l, r;
cin >> l >> r;
++r;
int ans = 0;
while (true)
{
int newl = nxt[l];
if (newl > r)
{
break;
}
++ans;
l = newl;
}
cout << ans << "\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... |