Submission #537862

#TimeUsernameProblemLanguageResultExecution timeMemory
537862vladislav11Sum Zero (RMI20_sumzero)C++14
61 / 100
113 ms16744 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int n; vector<ll> a; vector<ll> nxt; vector< pair<int,int> > jmp; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a.assign( n+1, 0 ); for ( int i=1; i<=n; i++ ) cin >> a[i]; for ( int i=1; i<=n; i++ ) a[i] += a[i-1]; nxt.assign( n+1, -1 ); map<ll,int> seen; for ( int i=n; i>=0; i-- ) { if ( seen.count(a[i]) ) nxt[i+1] = seen[ a[i] ]; seen[ a[i] ] = i; } a.clear(); seen.clear(); if ( n >= 400000 ) return 0; for ( int i=n-1; i>=0; i-- ) { if ( nxt[i] == -1 ) nxt[i] = nxt[i+1]; else if ( nxt[i+1] != -1 ) nxt[i] = min( nxt[i], nxt[i+1] ); } jmp.assign( n+1, {-1,-1} ); for ( int i=n; i>=0; i-- ) if ( nxt[i] != -1 ) { if ( nxt[i] == n ) jmp[i] = { 1, n }; else if ( jmp[ nxt[i]+1 ].first != -1 ) { if ( jmp[ nxt[i]+1 ].second+1 <= n ) { if ( jmp[ jmp[ nxt[i]+1 ].second+1 ].first == jmp[ nxt[i]+1 ].first ) jmp[i] = { 1+2*jmp[ nxt[i]+1 ].first, jmp[ jmp[ nxt[i]+1 ].second+1 ].second }; else jmp[i] = { 1, nxt[i] }; } else jmp[i] = { 1, nxt[i] }; } else jmp[i] = { 1, n }; } int q; cin >> q; while ( q-- ) { int l, r; cin >> l >> r; int ans = 0; while ( l <= r && jmp[l].second != -1 ) { if ( jmp[l].second <= r ) { ans += jmp[l].first; l = jmp[l].second+1; } else if ( nxt[l] <= r ) { ans++; l = nxt[l]+1; } else break; //cout << ": " << l << '\n'; } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...