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>
#define ends saradanciu
using namespace std;
const int BASE = 16;
const int LMAX = 6;
const int NMAX = 4e5+2;
int n,q,c,x,y,nxt[LMAX][NMAX];
int ends[NMAX],pwr[LMAX];
int main()
{
pwr[0] = 1;
for(int i = 1; i < LMAX; i++){
pwr[i] = pwr[i-1]*BASE;
}
cin >> n;
map<int, int> mp;
int pref = 0;
mp[pref] = 0;
for(int i = 1; i <= n; i++){
cin >> c;
pref += c;
if(mp.count(pref)){
ends[mp[pref]+1] = i;
}
mp[pref] = i;
}
nxt[0][n+1] = n+2;
int mini = n+2;
for(int i = n; i >= 1; i--){
if(ends[i] != 0){
mini = min(mini, ends[i]);
}
nxt[0][i] = mini+1;
}
for(int j = 1; j <= 4; j++){
for(int i = n+1; i >= 1; i--){
nxt[1][i] = nxt[(j != 1)][nxt[(j != 1)][i]];
}
for(int i = 1; i <= n+1; i++){
if(nxt[1][i] == 0){
nxt[1][i] = n+2;
}
}
}
for(int j = 2; j < LMAX; j++){
for(int i = n+1; i >= 1; i--){
nxt[j][i] = nxt[j-1][nxt[j-1][i]];
}
for(int i = 1; i <= n+1; i++){
if(nxt[j][i] == 0){
nxt[j][i] = n+2;
}
}
}
cin >> q;
while(q--){
cin >> x >> y;
int ans = 0;
for(int i = LMAX-1; i >= 0; i--){
while(nxt[i][x]-1 <= y){
ans += pwr[i];
x = nxt[i][x];
}
}
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... |