Submission #945273

#TimeUsernameProblemLanguageResultExecution timeMemory
945273vjudge1Sum Zero (RMI20_sumzero)C++17
61 / 100
256 ms28244 KiB
#include <bits/stdc++.h> using namespace std; namespace std { template <int D, typename T> struct Vec : public vector<Vec<D - 1, T>> { static_assert(D >= 1, "Dimension must be positive"); template <typename... Args> Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} }; template <typename T> struct Vec<1, T> : public vector<T> { Vec(int n = 0, T val = T()) : std::vector<T>(n, val) {} }; /* Example Vec<4, int64_t> f(n, k, 2, 2); // = f[n][k][2][2]; Vec<2, int> adj(n); // graph */ template <class Fun> class y_combinator_result { Fun fun_; public: template <class T> explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {} template <class... Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template <class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } /* Example auto fun = y_combinator([&](auto self, int x) -> void { self(x + 1); }); */ } // namespace std int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n + 2); for (int i = 2; i <= n + 1; i++) cin >> a[i]; map<int64_t, int> mp; vector<int> prv(n + 2, 0); vector<int> depth(n + 2, 0); vector<int> jump(n + 2); mp[0] = 1; prv[0] = 0; prv[1] = 0; depth[0] = 0; depth[1] = 1; jump[1] = 0; int64_t sum = 0; for (int i = 2; i <= n + 1; i++) { sum += a[i]; if (mp.count(sum)) { prv[i] = mp[sum]; prv[i] = max(prv[i], prv[i - 1]); } else { prv[i] = prv[i - 1]; } depth[i] = depth[prv[i]] + 1; if (depth[prv[i]] - depth[jump[prv[i]]] == depth[jump[prv[i]]] - depth[jump[jump[prv[i]]]]) { jump[i] = jump[jump[prv[i]]]; } else { jump[i] = prv[i]; } mp[sum] = i; } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; r++; int res = 0; while (r >= l) { if (jump[r] >= l) { res += depth[r] - depth[jump[r]]; r = jump[r]; } else if (prv[r] >= l) { res++; r = prv[r]; } else { break; } } cout << res << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...