Submission #972590

#TimeUsernameProblemLanguageResultExecution timeMemory
972590VMaksimoski008Sum Zero (RMI20_sumzero)C++17
0 / 100
4 ms2904 KiB
// #include <bits/stdc++.h> // using namespace std; // using ll = long long; // using pii = pair<int, int>; // #define int long long // const int LOG = 20; // const int maxn = 4e5 + 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; // vector<pii> qus(q); // vector<int> ans(q), curr(q); // for(int i=0; i<q; i++) { // cin >> qus[i].first >> qus[i].second; // qus[i].first--; // curr[i] = qus[i].second; // } // for(int &x : ans) cout << x << '\n'; // return 0; // } #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define int long long 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=0; j<LOG; j++) { if(up[r][j] >= l && up[r][j] != -1) { ans += (1 << j); r = up[r][j]; } } cout << ans + 1 << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...