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 = n; i >= 0; --i) {
		int ind = lower_bound(ord[p[i-1]].begin(), ord[p[i-1]].end(), i) - ord[p[i-1]].begin();
		if(ind < (int)ord[p[i-1]].size())
			Min(to[i], ord[p[i-1]][ind]);
		Min(to[i], to[i+1]);
	}
	cin >> q;
	while(q--) {
		int l, r;
		cin >> l >> r;
		int v = l, cnt = 0;
		while(to[v] <= r)
			v = to[v] + 1, ++cnt;
		cout << cnt << endl;
	}
	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... |