This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=4e5+10, LG=8;
int n, a[N], nxt[N];
pair<ll, int> pf[N];
int st[LG][N];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
pf[0]={0, 0};
for (int i=1; i<=n; ++i) cin >> a[i], pf[i]={pf[i-1].first+a[i], i}, nxt[i]=n+1;
sort(pf, pf+n+1);
for (int i=0; i<=n; ++i){
int j=i;
while (j<n && pf[j+1].first==pf[i].first) ++j;
for (int k=i; k<j; ++k) nxt[pf[k].second+1]=pf[k+1].second;
i=j;
}
vector<pair<int, int>> v, vv;
for (int i=1; i<=n; ++i) if (nxt[i]!=n+1) vv.emplace_back(i, nxt[i]);
sort(vv.begin(), vv.end(), [&](pair<int, int> x, pair<int, int> y){
return x.second<y.second;
});
for (auto &i:vv){
if (v.empty() || i.first>v.back().second) v.push_back(i);
}
for (int i=1; i<n; ++i) st[0][i]=min(nxt[i], nxt[i+1]);
for (int k=2; k<=LG; ++k){
for (int i=1; i+(1<<k)-1<=n; ++i){
st[k-1][i]=min(st[k-2][i], st[k-2][i+(1<<(k-1))]);
}
}
auto get=[&](int x, int y) -> int {
if (x==y) return nxt[x];
int lg=__lg(y-x+1);
return min(st[lg-1][x], st[lg-1][y-(1<<lg)+1]);
};
int q; cin >> q;
while (q--){
int x, y; cin >> x >> y;
int i1=lower_bound(v.begin(), v.end(), make_pair(x, 0))-v.begin();
int i2=lower_bound(v.begin(), v.end(), make_pair(y, 0))-v.begin()-1;
int ans=i2-i1+1;
int f=get(x, y);
if (f<v[i1].first) ++ans;
cout << ans << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |