# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1136208 | thdh__ | Sum Zero (RMI20_sumzero) | C++20 | 0 ms | 0 KiB |
#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;
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], ans[N], l[N], r[N];
unordered_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;
}
cin>>q;
for (int i=0;i<q;i++) cin>>l[i]>>r[i];
for (int i=M-1;i>=0;i--)
{
for (int j=1;j<=n+2;j++) a[j] = nxt[j];
for (int j=1;j<=i;j++)
{
for (int k=1;k<=n+2;k++) a[k] = a[a[k]];
}
for (int j=0;j<q;j++) if (a[l[j]] <= r[j]+1) l[j] = a[l[j]], ans[j] += (1<<i);
}
for (int i=0;i<q;i++) cout<<ans[i]<<endl;
}
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}