이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
constexpr int NMAX = 4e5 + 5;
typedef long long LL;
int N;
int V[NMAX];
int Q;
int Next[20][NMAX];
map <LL, int> mp;
int step = 0;
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for (int i = 1; i <= N; ++ i )
cin >> V[i];
}
void Precalculare () {
Next[0][N+1] = 1000000000;
while ((1<<step) <= N) ++ step;
LL suff = 0;
mp[0] = N;
for (int i = N; i >= 1; -- i ) {
Next[0][i] = Next[0][i+1];
suff += 1LL * V[i];
int ind = mp[suff];
if (ind != 0) Next[0][i] = min(Next[0][i], ind);
mp[suff] = i-1;
}
for (int L = 1; (1<<L) <= N; ++ L ) {
Next[L][N+1] = 1000000000;
for (int i = 1; i <= N; ++ i ) {
if (Next[L-1][i] >= N) Next[L][i] = 1000000000;
else Next[L][i] = Next[L-1][Next[L-1][i]+1];
}
}
}
int Query (int start, int Finish) {
int ans = 0;
for (int i = step-1; i >= 0; -- i ) {
if (Next[i][start] > Finish) continue;
ans += (1<<i);
start = Next[i][start]+1;
}
return ans;
}
void Solve () {
cin >> Q;
for (; Q; -- Q ) {
int L, R;
cin >> L >> R;
cout << Query(L, R) << '\n';
}
}
int main () {
Read();
Precalculare();
Solve();
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... |