#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ldb long double
#define db double
#define fin(x) freopen(x,"r",stdin)
#define fout(x) freopen(x,"w",stdout)
#define fo(i,l,r) for(int i=(l);i<=(r);i++)
#define foi(i,l,r) for(int i=(l);i>=(r);i--)
#define el cout<<'\n';
#define cel cerr<<'\n';
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define gb(x,i) (((x)>>(i))&1)
#define mask(i) (1LL<<(i))
#define TIME (1.0 * clock()/CLOCKS_PER_SEC)
using namespace std;
const int N=4e5+5;
const int bl=350;
const ll base=1e9+7;
const ll inf=1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l,ll r)
{
return l+(1ll*rng()*rng()%(r-l+1)+(r-l+1))%(r-l+1);
}
template<class X,class Y>bool maximize(X &a,Y b)
{
if(a<b) return a=b,true;
return false;
}
template<class X,class Y>bool minimize(X &a, Y b)
{
if(a>b) return a=b,true;
return false;
}
int n,x,nxt[N][20],q;
ll sum[N];
unordered_map<ll,int>cc;
void solve()
{
int l,r,ans=0;
cin>>l>>r,l--;
foi(i,19,0) if(nxt[l][i] and nxt[l][i]<=r) ans+=mask(i),l=nxt[l][i];
cout<<ans,el
}
int main()
{
#define task "o"
if(fopen(task".inp","r"))
{
fin(task".inp");
fout(task".out");
}
srand(time(NULL));
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
fo(i,1,n) cin>>x,sum[i]=sum[i-1]+x;
nxt[n+1][0]=n+1;
foi(i,n,0)
{
if(cc.count(sum[i])) nxt[i][0]=min(nxt[i+1][0],cc[sum[i]]);
else nxt[i][0]=nxt[i+1][0];
cc[sum[i]]=i;
}
fo(x,1,log2(n+1)) fo(i,0,n-mask(x)+1)
{
if(!nxt[i][x-1] or nxt[i][x-1]>n) nxt[i][x]=n+1;
else nxt[i][x]=nxt[nxt[i][x-1]][x-1];
}
cin>>q;
while(q--) solve();
return 0;
}
Compilation message
nicelines.cpp: In function 'int main()':
nicelines.cpp:9:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define fin(x) freopen(x,"r",stdin)
| ~~~~~~~^~~~~~~~~~~~~
nicelines.cpp:56:9: note: in expansion of macro 'fin'
56 | fin(task".inp");
| ^~~
nicelines.cpp:10:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | #define fout(x) freopen(x,"w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~
nicelines.cpp:57:9: note: in expansion of macro 'fout'
57 | fout(task".out");
| ^~~~
/usr/bin/ld: /tmp/ccicisnj.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccA4M8gl.o:nicelines.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccicisnj.o: in function `main':
grader.cpp:(.text.startup+0x68): undefined reference to `solve(int, int)'
collect2: error: ld returned 1 exit status