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 curr_lg[maxn];
int l[maxn];
int r[maxn];
int ans[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)
{
cin >> l[i] >> r[i];
++r[i];
}
for (int lg = 18; lg >= 0; --lg)
{
for (int i = 1; i <= n + 1; ++i)
{
curr_lg[i] = nxt[i];
}
for (int j = lg; j >= 1; --j)
{
for (int k = 1; k <= n; ++k)
{
if (curr_lg[k] <= n + 1)
{
curr_lg[k] = curr_lg[curr_lg[k]];
}
}
}
for (int i = 1; i <= q; ++i)
{
if (curr_lg[l[i]] <= r[i])
{
ans[i] += (1 << lg);
l[i] = curr_lg[l[i]];
}
}
}
for (int i = 1; i <= q; ++i)
{
cout << ans[i] << "\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... |