이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ll long long
#define zeros(x) ( (x ^ (x - 1)) & x )
using namespace std;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 rng(seed);
#define rand() rng()
/*const long long mod = 998244353;
long long fct[200005],invfct[200005],inv[200005];
long long put2[200005];
long long lgput (long long a, long long exp)
{
long long rz=1;
while(exp)
if(exp&1)
exp^=1,rz=rz*1LL*a%mod;
else
exp>>=1,a=a*1LL*a%mod;
return rz;
}
long long cmb (long long n, long long k)
{
if(n<k || k<0 || n<0)
return 0;
return fct[n]*1LL*invfct[k]%mod*1LL*invfct[n-k]%mod;
}
void init ()
{
inv[1]=fct[0]=fct[1]=invfct[0]=invfct[1]=put2[0]=1,put2[1]=2;
for(long long i=2;i<=200000;++i)
put2[i]=put2[i-1]*2LL%mod,inv[i]=(mod-mod/i)*1LL*inv[mod%i]%mod,fct[i]=i*1LL*fct[i-1]%mod,invfct[i]=inv[i]*1LL*invfct[i-1]%mod;
}
long long v[200005];*/
const int N=2e5+5;
map<ll,int> last;
int dp[N][20],v[N];
void solve()
{
int n;
ll sum=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i];
last[0]=n+1;
for(int j=0;j<=18;j++)
{
for(int i=0;i<=n+1;i++)
dp[i][j]=-1;
}
for(int i=n;i>=1;i--)
{
sum+=v[i];
dp[i][0]=dp[i+1][0];
if(last[sum] && (last[sum]-1<dp[i][0] || dp[i][0]==-1))
dp[i][0]=last[sum]-1;
last[sum]=i;
}
for(int i=1;i<=18;i++)
{
for(int j=1;j<=n;j++)
dp[j][i]=dp[dp[j][i-1]+1][i-1];
}
int q;
cin>>q;
while(q--)
{
ll l,r,ans=0;
cin>>l>>r;
while(l<=r)
{
bool ok=0;
for(int j=18;j>=0;j--)
{
if(dp[l][j]<=r && dp[l][j]!=-1)
{
ans+=(1<<j);
ok=1;
l=dp[l][j]+1;
break;
}
}
if(!ok)
break;
}
cout<<ans<<"\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
while(t--)
solve();
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... |