Submission #1076440

#TimeUsernameProblemLanguageResultExecution timeMemory
1076440IdanRosenSum Zero (RMI20_sumzero)C++98
61 / 100
178 ms65536 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef std::pair<ll, ll> pii; typedef std::pair<ll, ll> pll; typedef std::pair<ld, ld> pld; #define MAXN 400000 #define LOGN ((int) log2(MAXN) + 1) int jump[MAXN][LOGN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> arr(n); for (auto &i: arr) cin >> i; vector<int> next(n, INT32_MAX); { map<int, set<int> > sums; vector<int> pre_sum(n + 1); pre_sum[0] = 0; for (int i = 0; i < n; i++) { pre_sum[i + 1] = pre_sum[i] + arr[i]; sums[pre_sum[i + 1]].insert(i + 1); } int curr_sum = 0; for (int i = 0; i < n; i++) { if (sums.count(curr_sum)) { auto it = sums[curr_sum].upper_bound(i); if (it != sums[curr_sum].end()) next[i] = *it; } curr_sum += arr[i]; } for (int i = n - 2; i >= 0; i--) next[i] = min(next[i], next[i + 1]); } for (ll i = 0; i < n; i++) jump[i][0] = next[i]; for (ll k = 1; k < LOGN; k++) for (ll i = 0; i < n; i++) jump[i][k] = ((jump[i][k - 1] == INT32_MAX || jump[i][k - 1] == n) ? INT32_MAX : jump[jump[i][k - 1]][k - 1]); int q; cin >> q; while (q-- > 0) { int l, r; cin >> l >> r; --l; int ans = 0; int curr = l; for (int k = LOGN - 1; k >= 0 && curr < n; k--) { if (jump[curr][k] <= r) { curr = jump[curr][k]; ans += pow(2, k); } } cout << ans << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...