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;
#pragma GCC optimize("O3")
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
const int LOG = 10;
const int mxn = 4e5 + 10;
const int inf = 1e9 + 10;
int i, j, n, m;
long long pref;
vector<pii> range;
unordered_map<long long, int> mp;
int stab[mxn][LOG];
int main() {
cin.tie(0)-> sync_with_stdio(0);
cin >> n;
mp[0] = 0;
for(i = 1; i <= n; i++) {
cin >> j;
pref += j;
auto it = mp.find(pref);
if(it != mp.end())
range.push_back(MP((*it).ss + 1, i));
mp[pref] = i;
}
mp.clear();
for(i = 0; i < mxn; i++)
for(j = 0; j < LOG; j++)
stab[i][j] = inf;
j = 0;
for(i = 1; i <= n; i++) {
while(j < (int)range.size() && range[j].ff < i)
j++;
if(i <= range[j].ff)
stab[i][0] = range[j].ss;
}
for(j = 1; j < LOG; j++)
for(i = 1; i <= n; i++)
if(stab[i][j - 1] + 1 <= n)
stab[i][j] = stab[stab[i][j - 1] + 1][j - 1];
cin >> m;
int l, r, ans;
while(m--) {
cin >> l >> r;
ans = 0;
for(j = LOG - 1; j >= 0; j--) {
while(stab[l][j] <= r) {
l = stab[l][j] + 1;
ans += (1 << j);
}
}
cout << ans << endl;
}
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... |