# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
950012 | TrieTr | Sum Zero (RMI20_sumzero) | C++14 | 5 ms | 4696 KiB |
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;
void local() {
#define taskname ""
if (fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
}
#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
template<class X, class Y> bool mini(X &a, const Y &b) {return (a > b) ? a = b, true : false;}
template<class X, class Y> bool maxi(X &a, const Y &b) {return (a < b) ? a = b, true : false;}
const int N = 4e5 + 5;
const int LG = 19;
int n, q;
int arr[N];
map<ll, int> pos;
int jump[N][2]; // 0 : parent, 1 : jump
int lg_step[N];
int depth[N];
int main() {
fastio; local();
cin >> n;
for(int i = 1; i <= n; i++) cin >> arr[i];
ll sum = 0;
jump[n + 1][0] = jump[n + 1][1] = n + 1;
pos[0] = n + 1;
for(int i = n; i > 0; i--) {
sum += arr[i];
jump[i][0] = pos[sum] - 1;
jump[i][0] = (jump[i][0] == -1 ? jump[i + 1][0] : min(jump[i][0], jump[i + 1][0]));
pos[sum] = i;
}
for(int i = n; i > 0; i--) {
int p = jump[i][0];
depth[i] += depth[p] + 1;
if(depth[jump[p][1]] - depth[p] == depth[jump[jump[p][1]][1]] - depth[jump[p][1]]) jump[i][1] = jump[jump[p][1]][1];
else jump[i][1] = p;
}
/*for(int i = n; i >= 1; i--) {
cout << i << ' '<< jump[i][0] << ' ' << jump[i][1] << ' ' << depth[i] << '\n';
}*/
cin >> q;
while(q--) {
int l, r; cin >> l >> r;
int res = 0;
while(jump[l][0] <= r) {
int tmp = l;
if(jump[l][1] <= r && jump[l][1] != l) {
res += depth[l] - depth[jump[l][1]];
l = jump[l][1] + 1;
}
else {
res++;
l = jump[l][0] + 1;
}
//cout << tmp << ' ' << l << ' ' << res << '\n';
}
cout << res << '\n';
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |