This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC target("avx2,popcnt")
#pragma GCC optimize("O3,unroll-loops")
const int MAX_N = 4e5;
const int MAX_Q = 4e5;
using namespace std;
int last0[MAX_N + 2], last[MAX_N + 2], l[MAX_Q], r[MAX_Q], ans[MAX_Q], p[MAX_N + 2];
int sp[MAX_N + 1];
int main() {
int n, q;
cin >> n;
sp[0] = 0;
for ( int i = 1; i <= n; i++ ) {
int a;
cin >> a;
sp[i] = sp[i - 1] + a;
}
cin >> q;
for ( int i = 0; i < q; i++ ) {
cin >> l[i] >> r[i];
ans[i] = 0;
}
for ( int i = 0; i <= n; i++ )
p[i] = i;
sort( p, p + 1 + n, []( int i, int j ) {
if ( sp[i] == sp[j] )
return i < j;
return sp[i] < sp[j];
} );
for ( int i = 1; i <= n; i++ ) {
if ( sp[p[i]] == sp[p[i - 1]] )
last0[p[i - 1] + 1] = p[i];
}
for ( int i = 1; i <= n + 1; i++ ) {
if ( last0[i] == 0 )
last0[i] = n + 1;
}
for ( int i = n; i >= 1; i-- )
last0[i] = last0[i] < last0[i + 1] ? last0[i] : last0[i + 1];\
for ( int niv = log2( n ); niv >= 0; niv-- ) {
for ( int i = 1; i <= n; i++ )
last[i] = last0[i];
for ( int b = 1; b <= niv; b++ ) {
for ( int i = 1; i <= n; i++ )
last[i] = last[i] + 1 > n ? n + 1 : last[last[i] + 1];
last[n + 1] = n + 1;
}
for ( int i = 0; i < q; i++ ) {
if ( last[l[i]] <= r[i] ) {
ans[i] += (1 << niv);
l[i] = last[l[i]] + 1;
}
}
}
for ( int i = 0; i < q; i++ )
cout << ans[i] << "\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... |