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>
using namespace std;
#define ll long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 4e5 + 5;
int n, q, res;
int arr[N];
unordered_map<ll, int> pos;
int nxt[N];
int h[N], jump[N];
int i;
void make_leaf(int& id, int& p) {
h[id] = h[p] + 1;
if(h[jump[p]] - h[p] == h[jump[jump[p]]] - h[jump[p]]) jump[id] = jump[jump[p]];
else jump[id] = p;
}
int main() {
fastio;
cin >> n;
for(i = 1; i <= n; i++) cin >> arr[i];
ll sum = 0;
nxt[n + 1] = n + 1;
pos[0] = n + 1;
for(i = n; i > 0; i--) {
sum += arr[i];
nxt[i] = pos[sum] - 1;
nxt[i] = (nxt[i] == -1 ? nxt[i + 1] : min(nxt[i], nxt[i + 1]));
pos[sum] = i;
}
for(i = n; i > 0; i--) {
int u = nxt[i];
if(h[u] > 0 || u > n) continue;
int p = nxt[u + 1];
make_leaf(u, p);
}
cin >> q;
while(q--) {
int l, r; cin >> l >> r;
l = nxt[l];
if(l > r) {cout << "0\n"; continue;}
res = 0;
while(l <= r && l != 0) {
if(jump[l] <= r && jump[l] != 0) {
res += h[l] - h[jump[l]];
l = jump[l];
}
else {
res++;
l = nxt[l + 1];
}
}
cout << res << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |