제출 #1118382

#제출 시각아이디문제언어결과실행 시간메모리
1118382somefolkSum Zero (RMI20_sumzero)C++14
61 / 100
985 ms23244 KiB
#include <iostream> #include <vector> #include <unordered_map> using namespace std; const int N = 4*1e5+5; const int k = 500; // aprox. sqrt(n) int n, q, s = 0, curr; vector<int> a(N), idx(N), valid(N); unordered_map<int, int> mp; void solve(){ cin >> n; for(int i = 0; i < n; i++){ cin >> a[i]; } for(int i = 0; i < n+1; i++){ idx[i] = n+1; } mp[0] = n; for(int i = n-1; i >= 0; i--){ s += a[i]; if(mp.count(s)) idx[i] = mp[s]; mp[s] = i; } mp.clear(); curr = n+1; for(int i = n-1; i >= 0; i--){ if(idx[i] > -1){ curr = min(curr, idx[i]); } idx[i] = curr; } for(int i = 0; i < n; i++){ curr = i; for(int j = 0; j < k && curr <= n; j++){ curr = idx[curr]; } valid[i] = curr; } cin >> q; while(q--){ int l, r; cin >> l >> r; curr = l-1; int sol = 0; while(valid[curr] <= r){ sol += k; curr = valid[curr]; } while(idx[curr] <= r){ sol++; curr = idx[curr]; } cout << sol << "\n"; } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...