This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #define ll long long
// #define endl "\n"
using namespace std;
// using namespace __gnu_pbds;
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// template <typename T, typename key = less<T>>
// using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
const int lg = 19, N = 4e5 + 5;
int up[N], nxt[N], l[N], r[N], a[N], ans[N];
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
unordered_map<long long, int> last;
int mn = n + 1;
long long sum = 0;
last[0] = n + 1;
for (int i = n; i >= 1; i--)
{
last[sum] = i;
sum += a[i];
if (last.count(sum))
mn = min(mn, last[sum]);
nxt[i] = mn + 1;
}
last.clear();
for (int i = 1; i <= n; i++) a[i] = 0;
nxt[n + 1] = nxt[n + 2] = n + 2;
// for (int i = 1; i <= n; i++) cout << up[0][i] << " \n"[i == n];
int q;
cin >> q;
for (int i = 0; i < q; i++)
cin >> l[i] >> r[i], ans[i] = 0;
for (int bit = lg - 1; bit >= 0; bit--)
{
for (int i = 1; i <= n + 2; i++)
up[i] = nxt[i];
for (int x = 0; x < bit; x++)
for (int i = 1; i <= n; i++)
up[i] = up[up[i]];
for (int i = 0; i < q; i++)
if (up[l[i]] <= r[i] + 1)
ans[i] += 1 << bit, l[i] = up[l[i]];
}
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
// int t = 1;
// precomp();
// cin >> t;
// for (int cs = 1; cs <= t; cs++)
solve();
// cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\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... |