제출 #928916

#제출 시각아이디문제언어결과실행 시간메모리
928916winter0101Sum Zero (RMI20_sumzero)C++14
61 / 100
239 ms29252 KiB
#include<bits/stdc++.h> using namespace std; #define all(fl) fl.begin(),fl.end() #define pb push_back #define fi first #define se second #define for1(i,j,k) for(int i=j;i<=k;i++) #define for2(i,j,k) for(int i=j;i>=k;i--) #define for3(i,j,k,l) for(int i=j;i<=k;i+=l) #define lb lower_bound #define ub upper_bound #define sz(a) (int)a.size() #define pii pair<int,int> #define pli pair<long long,int> #define gcd __gcd #define lcm(x,y) x*y/__gcd(x,y) #define pil pair<int,long long> const int maxn=4e5+9; vector<int>g[maxn]; int bit[maxn]; int n,tme=0; vector<int>in,out; void dfs(int u){ in[u]=++tme; for (auto v:g[u]){ dfs(v); } out[u]=tme; } void add(int pos,int val){ for(;pos<=n;pos+=(pos-(pos&(pos-1))))bit[pos]+=val; } void add(int l,int r,int val){ add(l,val); add(r+1,-val); } int get(int pos){ int sum=0; for(;pos>=1;pos-=(pos-(pos&(pos-1))))sum+=bit[pos]; return sum; } int lep[maxn]; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); //freopen("temp.INP","r",stdin); //freopen("temp.OUT","w",stdout); cin>>n; vector<pli>a(n+1); for1(i,1,n){ cin>>a[i].fi; a[i].fi+=a[i-1].fi; a[i].se=i; } a[0].fi=a[0].se=0; sort(all(a)); vector<int>nxt(n+1); for1(i,0,n){ nxt[a[i].se+1]=n+1; if (i<n&&a[i].fi==a[i+1].fi){ nxt[a[i].se+1]=a[i+1].se; } } vector<pli>().swap(a); for2(i,n-1,1)nxt[i]=min(nxt[i],nxt[i+1]); auto f=[&](int u){ int l=u,r=n,ans=u; while (l<=r){ int mid=(l+r)/2; if (nxt[mid]==nxt[u]){ l=mid+1; ans=mid; } else r=mid-1; } return ans; }; for2(i,n,1){ if (i<n&&nxt[i]==nxt[i+1]){ continue; } if (nxt[i]+1>n){ g[0].pb(i); } else { g[f(nxt[i]+1)].pb(i); } } in.resize(n+1),out.resize(n+1); dfs(0); for1(i,0,n)vector<int>().swap(g[i]); int q; cin>>q; for1(i,1,q){ int r; cin>>lep[i]>>r; g[r].pb(i); } int p=1; for1(i,1,n){ while (nxt[p]<i&&p<i){ p++; //cerr<<p<<'\n'; } if (nxt[p]==i){ for1(j,p,i){ if (nxt[j]>i)break; p=j; if (f(j)==j){ add(in[j],out[j],1); } } } for (auto v:g[i]){ lep[v]=get(in[f(lep[v])]); } vector<int>().swap(g[i]); } for1(i,1,q)cout<<lep[i]<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...