Submission #1098271

#TimeUsernameProblemLanguageResultExecution timeMemory
1098271NeltSum Zero (RMI20_sumzero)C++17
22 / 100
927 ms22068 KiB
#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 ll lg = 20, N = 4e5 + 5; ll up[lg][N]; void solve() { ll n; cin >> n; ll a[n + 1], pref[n + 1]; pref[0] = 0; for (ll i = 1; i <= n; i++) cin >> a[i], pref[i] = pref[i - 1] + a[i]; map<ll, ll> last; ll mn = n + 1; for (ll i = n; i >= 1; i--) { last[pref[i]] = i; if (last.count(pref[i - 1])) mn = min(mn, last[pref[i - 1]]); up[0][i] = mn + 1; } // for (ll i = 1; i <= n; i++) cout << up[0][i] << " \n"[i == n]; for (ll i = 0; i < lg; i++) up[i][n + 1] = n + 2; for (ll bit = 1; bit < lg; bit++) for (ll i = 1; i <= n; i++) up[bit][i] = min(n + 2, up[bit - 1][up[bit - 1][i]]); ll q; cin >> q; while (q--) { ll l, r; cin >> l >> r; ll ans = 0; // for (ll bit = lg - 1; bit >= 0; bit--) // if (up[bit][l] <= r + 1) ans += 1 << bit, l = up[bit][l]; while (up[0][l] <= r + 1) ans++, l = up[0][l]; cout << ans << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll t = 1; // precomp(); // cin >> t; for (ll cs = 1; cs <= t; cs++) solve(); // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...