Submission #401961

#TimeUsernameProblemLanguageResultExecution timeMemory
401961rama_pangSum Zero (RMI20_sumzero)C++17
100 / 100
752 ms12372 KiB
#include <bits/stdc++.h> using namespace std; const int MAX = 400005; const int BASE = 32; const int LOG32 = 4; int N; int A[MAX]; int pow32[LOG32]; int dp[MAX][LOG32]; int main() { ios::sync_with_stdio(0); cin.tie(0); pow32[0] = 1; for (int i = 1; i < LOG32; i++) { pow32[i] = BASE * pow32[i - 1]; } cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } // dp[i][x] = minimum j, such that [i, j - 1] contains BASE^x valid segments { // Compute dp[] for (int i = 0; i <= N + 1; i++) { for (int j = 0; j < LOG32; j++) { dp[i][j] = N + 1; } } long long sum = 0; vector<long long> coords; coords.emplace_back(sum); for (int i = N - 1; i >= 0; i--) { sum += A[i]; coords.emplace_back(sum); } sort(begin(coords), end(coords)); coords.resize(unique(begin(coords), end(coords)) - begin(coords)); sum = 0; vector<int> last(coords.size(), N + 1); const auto Get = [&](long long x) { return lower_bound(begin(coords), end(coords), x) - begin(coords); }; last[Get(sum)] = N; for (int i = N - 1; i >= 0; i--) { sum += A[i]; dp[i][0] = last[Get(sum)]; last[Get(sum)] = i; } for (int i = N - 2; i >= 0; i--) { dp[i][0] = min(dp[i][0], dp[i + 1][0]); } for (int j = 1; j < LOG32; j++) { for (int i = 0; i < N; i++) { int x = i; for (int k = 0; k < BASE; k++) { x = dp[x][j - 1]; } dp[i][j] = x; } } } int Q; cin >> Q; while (Q--) { int L, R; cin >> L >> R; L--; int ans = 0; int x = LOG32 - 1; while (x >= 0) { if (dp[L][x] <= R) { ans += pow32[x]; L = dp[L][x]; } else { x--; } } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...