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>
#define ll long long
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
#define int ll
using namespace std;
const int N = 4e5 + 5;
int a[N],p[N],last[N];
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
vector <int> vec;
map <int,int> mp;
vec.pb(0);
for(int i=1;i<=n;i++){
cin>>a[i];
p[i]=p[i-1]+a[i];
vec.pb(p[i]);
}
sort(vec.begin(),vec.end());
int val=0;
for (int i=0;i<vec.size();i++){
if (!i || vec[i] != vec[i - 1]) {
++val;
mp[vec[i]] = val;
}
}
for (int i = 0; i <= n; i++)
p[i] = mp[p[i]],last[p[i]] = -1;
int q;
cin>>q;
while(q--){
int l,r,ans=0;
cin>>l>>r;
int R = l-1;
last[p[l - 1]] = l - 1;
for (int i = l; i <= r; i++){
if (last[p[i]]>= R) {
++ans;
R = i;
}
last[p[i]] = i;
}
cout<<ans<<endl;
for (int i=l-1;i<=r;i++)
last[p[i]] = -1;
}
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:32:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i=0;i<vec.size();i++){
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |