This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* user: test
* fname: Test
* lname: Testulescu
* task: SumZero
* score: 61.0
* date: 2020-11-29 14:58:36.504236
*/
#include <bits/stdc++.h>
#define inf 1000000000
using namespace std;
vector <int> compute_lift(int n)
{
vector <pair <long long, int>> mp(n + 1);
mp[0] = { 0, 0 };
long long sp = 0;
vector <int> ans(n + 1, inf);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
sp += x;
mp[i] = { sp, i };
}
sort(mp.begin(), mp.end());
for (int i = 1; i < (int)mp.size(); i++)
if (mp[i].first == mp[i - 1].first)
ans[mp[i - 1].second] = mp[i].second;
for (int i = n - 1; i >= 0; i--)
ans[i] = min(ans[i], ans[i + 1]);
return ans;
}
int main()
{
int n;
cin >> n;
vector <vector <int>> lift;
lift.emplace_back(compute_lift(n));
for (int i = 1; i < 8; i++) {
lift.emplace_back(n + 1, inf);
for (int j = 0; j < n; j++) {
int act = j;
for (int k = 0; k < 4; k++)
if (act != inf)
act = lift[i - 1][act];
lift[i][j] = act;
}
}
int q;
cin >> q;
for (int test = 0; test < q; test++) {
int L, R;
cin >> L >> R;
L--;
int rez = 0;
for (int i = (int)lift.size() - 1; i >= 0; i--)
while (lift[i][L] <= R)
rez += (1 << (2 * i)), L = lift[i][L];
cout << rez << '\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... |