Submission #1026133

#TimeUsernameProblemLanguageResultExecution timeMemory
1026133NValchanovSum Zero (RMI20_sumzero)C++17
61 / 100
257 ms28296 KiB
#include <bits/stdc++.h> #define endl '\n' using namespace std; typedef long long ll; const int MAXN = 4e5 + 10; const int MAXQ = 4e5 + 10; const int MAXLOG = 19; int n, q; int a[MAXN]; int nxt[MAXN]; int lift[MAXN]; pair < int, int > queries[MAXQ]; int ans[MAXN]; map < int, int > sums; void read() { cin >> n; for(int i = 1; i <= n; i++) { cin >> a[i]; } cin >> q; for(int i = 1; i <= q; i++) { cin >> queries[i].first >> queries[i].second; } } void fill_nxt() { int minpos = MAXN - 1; int cursum = 0; for(int i = n + 1; i >= 1; i--) { cursum += a[i]; if(sums.find(cursum) != sums.end()) minpos = min(minpos, sums[cursum]); sums[cursum] = i; nxt[i] = minpos; } } void solve() { for(int k = MAXLOG - 1; k >= 0; k--) { for(int i = 1; i <= n + 1; i++) { lift[i] = nxt[i]; } lift[MAXN - 1] = MAXN - 1; for(int j = 1; j <= k; j++) { for(int i = 1; i <= n + 1; i++) { lift[i] = lift[lift[i]]; } lift[MAXN - 1] = lift[lift[MAXN - 1]]; } for(int i = 1; i <= q; i++) { if(lift[queries[i].first] <= queries[i].second + 1) { ans[i] += (1 << k); queries[i].first = lift[queries[i].first]; } } } } void print() { for(int i = 1; i <= q; i++) { cout << ans[i] << endl; } cout << endl; } int main() { ios_base :: sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); read(); fill_nxt(); solve(); print(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...