# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1273688 | nguyenbaoanh | Sum Zero (RMI20_sumzero) | C++20 | 1 ms | 332 KiB |
//Author: NguyenBaoAnh
//Problem: EXCHANGE
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#define vi vector<int>
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define fi first
#define se second
const int N=2e5, B=1024;
const ll Mod=1e9+7, oo=1e18;
int n, q;
ii up[N+5][20];
ll a[N+5], pre[N+5];
map<ll, ll> mp;
main()
{
//Nothing beats a Jet-2 Holiday!
//And right now, you can save 50 pounds per person.
//That's 200 pounds for a family of 4!
freopen("sumzero.inp", "r", stdin); freopen("sumzero.out", "w", stdout);
ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
cin>>n;
for(int i=1; i<=n; i++) cin>>a[i], pre[i]=pre[i-1]+a[i];
for(int i=n; i>0; i--) {
mp[pre[i]]=i;
if(mp.count(pre[i-1])) up[i][0]={mp[pre[i-1]]+1, 1};
else up[i][0]={i+1, 0};
}
up[n+1][0]={n+1, 0};
for(int j=1; j<20; j++)
for(int i=1; i<=n+1; i++) {
ii t=up[i][j-1];
up[i][j].fi=up[t.fi][j-1].fi;
up[i][j].se=t.se+up[t.fi][j-1].se;
}
cin>>q;
while(q--) {
int l, r; cin>>l>>r;
int res=0;
for(int i=19; i>=0; i--)
while(up[l][i].fi-1<=r && l<=r)
//cout<<i<<' '<<l<<' '<<up[l][i].fi<<'\n',
res+=up[l][i].se, l=up[l][i].fi;
cout<<res<<'\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |