제출 #401953

#제출 시각아이디문제언어결과실행 시간메모리
401953rama_pangSum Zero (RMI20_sumzero)C++17
61 / 100
557 ms21696 KiB
#include <bits/stdc++.h> using namespace std; int dp[400005][10]; int main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } // dp[i][x] = minimum j, such that [i, j - 1] contains 4^x valid segments { // Compute dp[] for (int i = 0; i <= N + 1; i++) { for (int j = 0; j < 10; 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 < 10; j++) { for (int i = 0; i < N; i++) { dp[i][j] = dp[dp[dp[dp[i][j - 1]][j - 1]][j - 1]][j - 1]; } } } int Q; cin >> Q; while (Q--) { int L, R; cin >> L >> R; L--; int ans = 0; int x = 9; while (x >= 0) { if (dp[L][x] <= R) { ans += 1 << (x + 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...