Submission #478282

#TimeUsernameProblemLanguageResultExecution timeMemory
478282Tenis0206Sum Zero (RMI20_sumzero)C++11
61 / 100
83 ms13528 KiB
#include <bits/stdc++.h> using namespace std; int n,q,v[100005]; vector<int> G[100005]; vector<pair<int,int>> Q[100005]; int t; unordered_map<long long,int> mp; int l[100005]; int cnt = 0; void dfs(int nod) { l[cnt++] = nod; for(auto it : Q[nod]) { int st = 1; int dr = cnt-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; } } v[it.second] = cnt - poz - 1; } for(auto it : G[nod]) { dfs(it); } --cnt; } 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<<v[i]<<'\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...