제출 #972606

#제출 시각아이디문제언어결과실행 시간메모리
972606VMaksimoski008Sum Zero (RMI20_sumzero)C++17
61 / 100
108 ms49924 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int LOG = 17; const int maxn = 1e5 + 5; int up[maxn][LOG]; int32_t main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); int n; cin >> n; vector<int> v(n+1), last(n+1, 1e9); for(int i=1; i<=n; i++) cin >> v[i]; map<ll, int> mp; mp[0] = 0; ll sum = 0; vector<pii> I; I.push_back({ -1, -1 }); for(int i=1; i<=n; i++) { sum += v[i]; if(mp.count(sum)) last[i] = mp[sum]; mp[sum] = i; if(last[i] != 1e9) I.push_back({ last[i], i }); } vector<int> pref; pref.push_back(-1); for(int i=1; i<(int)I.size(); i++) { if((int)pref.size() == 0) pref.push_back(I[i].first); else pref.push_back(max((ll)pref[i-1], (ll)I[i].first)); } for(int j=0; j<LOG; j++) for(int i=0; i<=n; i++) up[i][j] = -1; int p=0; for(int i=1; i<=n; i++) { while(p+1 < (int)I.size() && I[p+1].second <= i) p++; up[i][0] = pref[p]; } for(int j=1; j<LOG; j++) for(int i=1; i<=n; i++) if(up[i][j-1] != -1) up[i][j] = up[up[i][j-1]][j-1]; int q; cin >> q; while(q--) { int l, r, ans = 0; cin >> l >> r; l--; for(int j=LOG-1; j>=0; j--) { if(up[r][j] >= l && up[r][j] != -1) { ans += (1 << j); r = up[r][j]; } } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...