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[5000+1]={0};
vvi jump;
int walk(int u, int h)
{
for(int i=0, x=1; i<20; i++, x*=2) if((h&x)==x) u= jump[u][i];
return u;
}
void task()
{
int n;
cin>>n;
forr(i, 0, n) cin>>a[i];
jump.resize(n+2, vi(20, n+1));
// if no jump: then n+1 th element
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;
lastp[p]=i;
forr(j, 1, 20) jump[i][j]= min(jump[jump[i][j-1]][j-1], jump[i+1][j]);
}
int q;
cin>>q;
while(q--)
{
int L, R;
cin >> L >> R;
L--;
int l=0, r=n+1;
while(r>l+1)
{
int m= (l+r)/2, x= walk(L, m);
if(x<=R) l=m;
else r=m;
}
cout<<l<<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... |