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 endl '\n'
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 10, maxlog = 3, base = 100;
int n, dp[maxlog][maxn], q;
void solve()
{
cin >> n;
ll x, sum = 0;
int cl = -1;
unordered_map < ll, int > last;
for (int i = 1; i <= n; i ++)
{
cin >> x;
sum = sum + x;
if (last[sum] != 0 || sum == 0)
cl = max(cl, last[sum]);
last[sum] = i;
dp[0][i] = cl;
}
dp[0][0] = -1;
for (int j = 1; j < maxlog; j ++)
{
dp[j][0] = -1;
for (int i = 1; i <= n; i ++)
{
dp[j][i] = dp[j - 1][i];
for (int f = 1; f < base && dp[j][i] != -1; f ++)
dp[j][i] = dp[j - 1][dp[j][i]];
}
}
/**for (int j = 0; j < 4; j ++, cout << endl)
for (int i = 0; i <= n; i ++)
cout << dp[i][j] << " ";*/
cin >> q;
int l, r, jump, st, bit, f;
for (int i = 1; i <= q; i ++)
{
cin >> l >> r;
jump = 0, st = 1;
for (bit = 1; bit < maxlog; bit ++)
st = st * base;
for ( bit = maxlog - 1; bit >= 0; bit --)
{
//cout << l << " " << jump << endl;
while(dp[bit][r] >= l - 1)
{
jump += st;
r = dp[bit][r];
}
st /= base;
}
cout << jump << endl;
}
}
void speed()
{
ios_base::sync_with_stdio((false));
cin.tie(NULL);
cout.tie(NULL);
}
int main()
{
speed();
///freopen("input.txt", "r", stdin);
solve();
return 0;
}
/**
*/
Compilation message (stderr)
sumzero.cpp: In function 'void solve()':
sumzero.cpp:41:30: warning: unused variable 'f' [-Wunused-variable]
41 | int l, r, jump, st, bit, f;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |