# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
947816 | TrieTr | Sum Zero (RMI20_sumzero) | C++14 | 4 ms | 4956 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][LG];
int main() {
fastio; local();
cin >> n;
for(int i = 1; i <= n; i++) cin >> arr[i];
ll sum = 0;
for(int i = 1; i <= n + 1; i++) for(int k = 0; k < LG; k++) jump[i][k] = n + 2;
jump[0][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 k = 1; k < LG; k++) {
for(int i = 1; i + (1 << k) - 1 <= n; i++) {
jump[i][k] = jump[i][k - 1];
if(jump[i][k] <= n) jump[i][k] = jump[jump[i][k] + 1][k - 1];
}
}
cin >> q;
for(int i = 1; i <= q; i++) {
int l, r; cin >> l >> r;
int res = 0;
for(int k = LG - 1; k >= 0; k--) {
while(jump[l][k] <= r) {
l = jump[l][k] + 1; res += (1 << k);
}
}
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... |