Submission #1273727

#TimeUsernameProblemLanguageResultExecution timeMemory
1273727nguyenbaoanhSum Zero (RMI20_sumzero)C++20
0 / 100
2 ms1336 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ii pair<int,int> const int N = 200005; const int LOG = 20; int n, q; ll a[N], pre[N]; int nxt[N], up[N][LOG]; int cnt[N][LOG]; // cnt[i][k] = số đoạn khi nhảy 2^k lần từ i int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for(int i=1; i<=n; i++){ cin >> a[i]; pre[i] = pre[i-1] + a[i]; } // build nxt[] unordered_map<ll,int> mp; for(int i=n; i>=0; i--){ // duyệt cả pre[0] if(mp.count(pre[i])){ nxt[i] = mp[pre[i]]; }else nxt[i] = -1; mp[pre[i]] = i; } // build up[][], cnt[][] for(int i=0; i<=n; i++){ if(nxt[i] != -1){ up[i][0] = nxt[i]; cnt[i][0] = 1; }else{ up[i][0] = i+1; cnt[i][0] = 0; } } up[n+1][0] = n+1; cnt[n+1][0] = 0; for(int k=1; k<LOG; k++){ for(int i=0; i<=n+1; i++){ up[i][k] = up[ up[i][k-1] ][k-1]; cnt[i][k] = cnt[i][k-1] + cnt[ up[i][k-1] ][k-1]; } } cin >> q; while(q--){ int l,r; cin >> l >> r; l--; // vì ta làm trên pre[] int pos = l, res = 0; for(int k=LOG-1; k>=0; k--){ if(up[pos][k] <= r){ res += cnt[pos][k]; pos = up[pos][k]; } } cout << res << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...