Submission #478263

#TimeUsernameProblemLanguageResultExecution timeMemory
478263Tenis0206Sum Zero (RMI20_sumzero)C++11
22 / 100
77 ms27636 KiB
#include <bits/stdc++.h> using namespace std; int n,q,v[400005]; vector<int> G[400005]; vector<pair<int,int>> Q[400005]; int rez[400005],t; unordered_map<long long,int> mp; vector<int> l; void debug(vector<int> c) { for(auto it : c) { cerr<<it<<' '; } cerr<<'\n'; } void dfs(int nod) { l.push_back(nod); // debug(l); for(auto it : Q[nod]) { int st = 1; int dr = l.size()-1; int poz = 0; while(st<=dr) { int mij = (st+dr)>>1; if(l[mij]-1<=it.first) { poz = mij; dr = mij-1; } else { st = mij+1; } } rez[it.second] = l.size() - poz - 1; } for(auto it : G[nod]) { dfs(it); } l.pop_back(); } int main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("sumzero.in","r",stdin); // freopen("sumzero.out","w",stdout); cin>>n; for(int i=1; i<=n; i++) { cin>>v[i]; } cin>>q; for(int i=1; i<=q; i++) { int st,dr; cin>>st>>dr; Q[st].push_back({dr,i}); } mp[0] = n+1; G[0].push_back(n+1); t = 0; long long sum = 0; for(int i=n; i>=1; i--) { sum+=v[i]; if(mp[sum]) { if(t) { t = min(t,mp[sum]); } else { t = mp[sum]; } } G[t].push_back(i); mp[sum] = i; } dfs(0); for(int i=1; i<=q; i++) { cout<<rez[i]<<'\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...