# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1246752 | hainam2k9 | Sum Zero (RMI20_sumzero) | C++20 | 284 ms | 20964 KiB |
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 4e5+5;
const string NAME = "";
int n,q,a[MAXN],nxt[MAXN],depth[MAXN],jump[MAXN];
map<ll,int> pos;
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<=n; ++i)
cin >> a[i];
nxt[n+1]=n+2;
pos[0]=n+1;
ll sum=0;
for(int i = n; i>=0; --i){
sum+=a[i];
nxt[i]=(pos[sum]==0 ? n+1 : pos[sum]-1);
nxt[i]=min(nxt[i],nxt[i+1]);
pos[sum]=i;
}
for(int i = 1; i<=n; ++i)
++nxt[i];
pos.clear();
jump[n+1]=n+2, depth[n+1]=0;
jump[n+2]=n+2, depth[n+2]=-1e9;
for(int i = n; i>=0; --i){
int p=nxt[i];
depth[i]=depth[p]+1;
if(depth[p]-depth[jump[p]]==depth[jump[p]]-depth[jump[jump[p]]]) jump[i]=jump[jump[p]];
else jump[i]=p;
}
cin >> q;
while(q--){
int l,r,rs=0;
cin >> l >> r;
while(nxt[l]<=r+1){
if(jump[l]<=r+1) rs+=depth[l]-depth[jump[l]], l=jump[l];
else ++rs, l=nxt[l];
}
cout << rs << "\n";
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |