Submission #646454

#TimeUsernameProblemLanguageResultExecution timeMemory
646454borgar02Sum Zero (RMI20_sumzero)C++17
61 / 100
330 ms39048 KiB
#include <bits/stdc++.h> using namespace std; unsigned seed = chrono :: system_clock :: now().time_since_epoch().count(); mt19937 rng(seed); using ll = long long; const int N = 4e5 + 5; ll c[N]; int dp[N][8]; pair <int, int> que[N]; int res[N]; int main() { ios_base :: sync_with_stdio(false); cin.tie(0); int n, q; cin >> n; for(int i = 1; i <= n; i++) cin >> c[i]; cin >> q; for(int i = 1; i <= q; i++) { cin >> que[i].first >> que[i].second; que[i].first--; } partial_sum(c + 1, c + n + 1, c + 1); map <ll, int> m; dp[n + 1][0] = n + 1; for(int i = n; i >= 0; i--) { dp[i][0] = (m.count(c[i]) ? min(m[c[i]], dp[i + 1][0]) : dp[i + 1][0]); m[c[i]] = i; } auto precalc = [&](int to, int from) { dp[n + 1][to] = n + 1; for(int i = n; i >= 0; i--) dp[i][to] = dp[dp[i][from]][from]; }; for(int j = 1; j < 8; j++) { dp[n + 1][j] = n + 1; precalc(j, j - 1); } precalc(1, 7); for(int j = 2; j < 8; j++) precalc(j, j - 1); precalc(1, 7); precalc(2, 1); precalc(3, 2); precalc(4, 3); for(int i = 1; i <= q; i++) { for(int j : {4, 3, 2, 1, 7, 6, 5}) if(dp[que[i].first][j] <= que[i].second) res[i] |= (1 << (j <= 4 ? 14 + j : 7 + j)), que[i].first = dp[que[i].first][j]; } for(int j = 1; j < 8; j++) precalc(j, j - 1); precalc(1, 7); for(int j = 2; j < 5; j++) precalc(j, j - 1); for(int i = 1; i <= q; i++) { for(int j : {4, 3, 2, 1, 7, 6, 5}) if(dp[que[i].first][j] <= que[i].second) res[i] |= (1 << (j <= 4 ? 7 + j : j)), que[i].first = dp[que[i].first][j]; } for(int j = 1; j < 5; j++) precalc(j, j - 1); for(int i = 1; i <= q; i++) { for(int j : {4, 3, 2, 1, 0}) if(dp[que[i].first][j] <= que[i].second) res[i] |= (1 << j), que[i].first = dp[que[i].first][j]; cout << res[i] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...