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;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define f first
#define se second
template <typename T> void Max(T &a, T b) {
	a = max(a, b);
}
template <typename T> void Min(T &a, T b) {
	a = min(a, b);
}
const int inf = 0x3f3f3f3f;
const int N = 5e3 + 15;
int n, q, p[N];
int to[N];
map <int, vector <int>> ord;
int nxt[N];
int main() {
	memset(to, 0x3f, sizeof(to));
	memset(nxt, 0x3f, sizeof(nxt));
	ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
	// freopen("input.txt", "r", stdin);
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		p[i] += p[i-1];
		ord[p[i]].pb(i);
	}
	ord[0].pb(0);
	for(int i = 0; i <= n; ++i) {
		int ind = upper_bound(ord[p[i]].begin(), ord[p[i]].end(), i) - ord[p[i]].begin();
		if(ind < ord[p[i]].size())
			to[i] = ord[p[i]][ind];
		nxt[i] = to[i];
	}
	for(int i = n; i >= 0; --i) {
		Min(nxt[i], nxt[i+1]);
		Min(to[i], nxt[i]);
	}
	cin >> q;
	while(q--) {
		int l, r;
		cin >> l >> r;
		int v = l, cnt = 0;
		while(to[v] <= r)
			v = to[v], ++cnt;
		cout << cnt << endl;
	}
	return 0;
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:52:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |   if(ind < ord[p[i]].size())
      |      ~~~~^~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |