Submission #1297627

#TimeUsernameProblemLanguageResultExecution timeMemory
1297627NotLinuxSum Zero (RMI20_sumzero)C++20
61 / 100
1095 ms19156 KiB
#include <bits/stdc++.h> using namespace std; #define sz(x) (int)x.size() #define all(x) x.begin() , x.end() const int SQRT = 300; void solve(){ int n; cin >> n; vector<int>jump1(n+1,n+1); { { vector<long long>pre(n+1,0); for(int i = 1;i<=n;i++){ cin >> pre[i]; pre[i] += pre[i-1]; } map<long long,int>mpa; for(int i = n;i>=0;i--){ if(mpa.count(pre[i])){ jump1[i] = mpa[pre[i]]; } mpa[pre[i]] = i; } } for(int i = n-1;i>=0;i--){ jump1[i] = min(jump1[i] , jump1[i+1]); } } vector<int> jump2(n+1,-1); { deque<int>path; function<void(int)> dfs = [&](int node){ if(jump2[node] == -1)jump2[node] = -2; if(node == n+1 or (sz(path) == SQRT and jump2[path[0]] != -2))return; if(sz(path) == SQRT){ jump2[path[0]] = node; path.pop_front(); } path.push_back(node); dfs(jump1[node]); }; for(int i = 0;i<n;i++){ if(jump2[i] == -1){ path.clear(); dfs(i); } } } int q; cin >> q; while(q--){ int l,r; cin >> l >> r; l--; int ans = 0; while(l <= n and jump2[l] <= r and jump2[l] != -2){ ans += SQRT; l = jump2[l]; } while(l <= n and jump1[l] <= r and jump1[l] != -2){ ans++; l = jump1[l]; } cout << ans << '\n'; } } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0); int testcase=1;//cin >> testcase; while(testcase--)solve(); cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...