제출 #537853

#제출 시각아이디문제언어결과실행 시간메모리
537853vladislav11Sum Zero (RMI20_sumzero)C++14
61 / 100
875 ms65536 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int n; vector<ll> a; vector<ll> nxt; vector< vector<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; } 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] ); } /*for ( int i=0; i<=n; i++ ) cout << a[i] << ' '; cout << '\n'; for ( int i=0; i<=n; i++ ) cout << nxt[i] << ' '; cout << '\n';*/ jmp.assign( n+1, vector<int>( 21, n+1 ) ); for ( int i=0; i<=n; i++ ) jmp[i][0] = nxt[i]==-1 ? n+1 : nxt[i]; for ( int p=1; p<21; p++ ) for ( int i=0; i<=n; i++ ) if ( jmp[i][p-1]+1 <= n ) jmp[i][p] = jmp[ jmp[i][p-1]+1 ][p-1]; /*for ( int i=0; i<=n; i++ ) { cout << i << ": "; for ( int p=0; p<3; p++ ) cout << jmp[i][p] << ' '; cout << '\n'; }*/ int q; cin >> q; while ( q-- ) { int l, r; cin >> l >> r; int ans = 0; for ( int p=20; p>=0 && l <= r; p-- ) { if ( jmp[l][p] <= r ) { ans += 1<<p; l = jmp[l][p]+1; } //cout << p << " - " << 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...