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 fast ios::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL)
#define sz(x) (int)((x).size())
#define pb push_back
#define vi vector<int>
#define vb vector<bool>
#define vvb vector<vb>
#define pi pair<int,int>
#define vpi vector<pi>
#define vvi vector<vi>
#define vc vector<char>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pbb() pop_back()
//#define f first
#define s second
#define ll long long
#define int long long
#define ull unsigned long long
#define line cout<<"_____________________________"<<endl;
#define forr(i, a, b) for(int i=a; i<b; i++)
const int N=50000+1, mod=1e9+7, inf=1e18+1;
using namespace std;
//vi dx= { 0, 1, 0 , -1} , dy= {-1,0, 1, 0};
void setIO(string filename)
{
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
int a[400003]={0}, jump[400003][30]={0}, total[400003]={0};
int walk(int u, int h)
{
for(int i=0, x=1; i<30; i++, x*=2ll) if((h&x)==x) u= jump[u][i];
return u;
}
void task()
{
int n;
cin>>n;
forr(i, 0, n) cin>>a[i];
// if no jump: then n+1 th element
for(int i=0; i<30; i++) jump[n+1][i]=jump[n][i]=n;
map<int, int> lastp;
int p=0;
lastp[0]=n;
for(int i=n-1; i>=0; i--)
{
p+=a[i];
int r= lastp[p];
jump[i][0]= jump[i+1][0];
if(r!=0) jump[i][0]=r-1;
lastp[p]=i;
if(jump[i][0] != n) total[i]++;
//cout<<i+1<<" next "<<jump[i][0]+1<<endl;
forr(j, 1, 30)
{
jump[i][j]= jump[jump[i][j-1]+1][j-1];
if(jump[i][j]==n) break;
// cout<<i+1<<" "<<j<<" "<<jump[i][j]+1<<endl;
}
// line;
}
int q;
cin>>q;
while(q--)
{
int L, R;
cin >> L >> R;
L--;
R--;
int res = 0;
for (int j = 18; j >= 0; j--) if (jump[L][j] <= R) {
res += (1 << j);
L = jump[L][j];
}
cout<<res<<endl;
}
}
signed main()
{
fast;
int t;
t=1;
//cin>>t;
while(t--) task();
}
Compilation message (stderr)
sumzero.cpp: In function 'void setIO(std::string)':
sumzero.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((filename + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |