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 <iostream>
#include <map>
using namespace std;
const int baza = 80, lg = 3;
int v[400005], bl[400005][3];
int query(int u, int v)
{
int rez = 0, put = 80 * 80;
for(int j = 2; j >= 0; j--)
{
for(int i = 1; i <= 79; i++)
if(bl[u][j] <= v)
{
u = bl[u][j];
rez += put;
}
put /= 80;
}
return rez;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, q;
cin>>n;
for(int i = 1; i <= n; i++)
cin>>v[i];
long long mindr = n + 1, sum = 0;
map<long long, int> mp;
mp[0] = n + 1;
for(int i = n; i >= 1; i--)
{
sum += v[i];
bl[i][0] = mindr;
if(mp[sum] != 0)
mindr = min(mindr, mp[sum] - 1LL);
mp[sum] = i;
}
bl[0][0] = mindr;
for(int j = 1; j < 3; j++)
for(int i = 0; i <= n; i++)
{
int nod = i;
for(int k = 1; k <= 80 && nod != n + 1; k++)
nod = bl[nod][j - 1];
bl[i][j] = nod;
}
cin>>q;
for(int i = 1; i <= q; i++)
{
int a, b;
cin>>a>>b;
cout<<query(a - 1, b)<<'\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... |