#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
const int N = 4e5+5;
const int M = 19;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
int n,q;
int a[N], nxt[N], b[N][M];
map<int,int> p;
void solve()
{
cin>>n;
for (int i=1;i<=n;i++) cin>>a[i];
p[0] = n+1;
nxt[n+1] = n+2;
nxt[n+2] = n+2;
int sum = 0;
for (int i=n;i>=1;i--)
{
sum += a[i];
if (p.find(sum) == p.end()) nxt[i] = nxt[i+1];
else nxt[i] = min(nxt[i+1], p[sum]);
p[sum] = i;
}
// for (int i=1;i<=n;i++) cout<<nxt[i]<<" ";
// cout<<endl;
for (int i=1;i<=n+2;i++) b[i][0] = nxt[i];
for (int i=M-1;i>=1;i--)
{
for (int j=1;j<=n+2;j++) b[j][i] = nxt[j];
for (int j=1;j<=i;j++)
{
for (int k=1;k<=n+2;k++) b[k][i] = b[b[k][i]][i];
}
// for (int j=1;j<=n+2;j++) cout<<b[j][i]<<" ";
// cout<<endl;
}
cin>>q;
while (q--)
{
int l,r;
cin>>l>>r;
int ans = 0;
for (int i=M-1;i>=0;i--) {
if (b[l][i] <= r + 1)
{
ans += (1<<i);
l = b[l][i];
}
}
cout<<ans<<endl;
}
}
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
컴파일 시 표준 에러 (stderr) 메시지
sumzero.cpp:23:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
23 | const int inf = 1e18;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |