Submission #1026140

#TimeUsernameProblemLanguageResultExecution timeMemory
1026140NValchanovSum Zero (RMI20_sumzero)C++17
61 / 100
243 ms22420 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 lift[MAXN]; pair < int, int > queries[MAXQ]; int ans[MAXN]; map < ll, int > sums; int i, j, k; void read() { cin >> n; for(i = 1; i <= n; i++) { cin >> a[i]; } cin >> q; for(i = 1; i <= q; i++) { cin >> queries[i].first >> queries[i].second; } } void fill_nxt() { int minpos = MAXN - 1; ll cursum = 0; for(i = n + 1; i >= 1; i--) { cursum += a[i]; if(sums.find(cursum) != sums.end()) minpos = min(minpos, sums[cursum]); sums[cursum] = i; a[i] = minpos; } } void solve() { for(k = MAXLOG - 1; k >= 0; k--) { for(i = 1; i <= n + 1; i++) { lift[i] = a[i]; } lift[MAXN - 1] = MAXN - 1; for(j = 1; j <= k; j++) { for(i = 1; i <= n + 1; i++) { lift[i] = lift[lift[i]]; } lift[MAXN - 1] = lift[lift[MAXN - 1]]; } for(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(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...