이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |