제출 #857439

#제출 시각아이디문제언어결과실행 시간메모리
857439PetiSum Zero (RMI20_sumzero)C++14
61 / 100
252 ms22436 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 400005; long long v[maxn]; int nxt[maxn], jump[maxn], lvl[maxn]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin>>n; v[0] = 0; for(int i = 1; i <= n; i++) cin>>v[i]; for(int i = 1; i <= n; i++) v[i] += v[i-1]; map<long long, int> mp; nxt[n+1] = n+1; int last = n+1; for(int i = n; i >= 0; i--){ nxt[i] = mp.count(v[i]) ? mp[v[i]] : n+1; mp[v[i]] = i; if(nxt[i] < last){ last = nxt[i]; } else{ nxt[i] = last; } } mp.clear(); jump[n+1] = n+1; lvl[n+1] = 0; for(int i = n; i >= 0; i--){ int x = nxt[i]; if(lvl[x] == lvl[jump[x]]){ lvl[i] = lvl[x]+1; jump[i] = jump[jump[x]]; } else { lvl[i] = 1; jump[i] = nxt[i]; } } int q; cin>>q; while(q--){ int l, r; cin>>l>>r; --l; int ans = 0; while(nxt[l] <= r) { if(jump[l] <= r){ ans += (1<<lvl[l]) - 1; l = jump[l]; } else{ ans++; l = nxt[l]; } } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...