제출 #398659

#제출 시각아이디문제언어결과실행 시간메모리
398659abacabaSum Zero (RMI20_sumzero)C++14
0 / 100
15 ms676 KiB
// 石室詩士施氏, 嗜獅, 誓食十獅。
// 氏時時適市視獅。
// 十時, 適十獅適市。
// 是時, 適施氏適市。
// 氏視是十獅, 恃矢勢, 使是十獅逝世。
// 氏拾是十獅屍, 適石室。
// 石室濕, 氏使侍拭石室。
// 石室拭, 氏始試食是十獅。
// 食時, 始識是十獅, 實十石獅屍。
// 試釋是事。

#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 = upper_bound(ord[p[i]].begin(), ord[p[i]].end(), i) - ord[p[i]].begin();
		if(ind < (int)ord[p[i]].size())
			Min(to[i], ord[p[i]][ind]);
		Min(to[i], to[i+1]);
	}
	// for(int i = 0; i <= n; ++i)
	// 	cout << p[i] << ' ';
	// cout << endl;
	// for(int i = 0; i <= n; ++i)
	// 	cout << to[i] << ' ';
	// cout << endl;
	// return 0;
	cin >> q;
	while(q--) {
		int l, r;
		cin >> l >> r;
		int v = l - 1, cnt = 0;
		while(to[v] <= r)
			v = to[v], ++cnt;
		cout << cnt << endl;
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...