Submission #875107

#TimeUsernameProblemLanguageResultExecution timeMemory
875107MinaRagy06Sum Zero (RMI20_sumzero)C++17
61 / 100
328 ms26888 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N = 400'005, LG = 4, B = 5;
int nxt[LG][N];
int lst[N];
ll prfx[N], comp[N], a[N];
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		prfx[i] = prfx[i - 1] + a[i];
		comp[i] = prfx[i];
	}
	sort(comp, comp + n + 1);
	memset(lst, -1, sizeof lst);
	int mn = n + 1;
	for (int i = n; i >= 0; i--) {
		int idx = lower_bound(comp, comp + n + 1, prfx[i]) - comp;
		if (lst[idx] != -1) {
			mn = min(mn, lst[idx]);
		}
		lst[idx] = i;
		nxt[0][i] = mn;
	}
	nxt[0][n + 1] = n + 1;
	for (int j = 1; j < LG; j++) {
		for (int i = 0; i <= n + 1; i++) {
			nxt[j][i] = nxt[j - 1][i];
			for (int k = 1; k < (1 << B); k++) {
				nxt[j][i] = nxt[j - 1][nxt[j][i]];
			}
		}
	}
	int q;
	cin >> q;
	while (q--) {
		int l, r;
		cin >> l >> r;
		l--;
		int ans = 0;
		for (int j = LG - 1; j >= 0; j--) {
			while (nxt[j][l] <= r) {
				ans += 1 << (B * j);
				l = nxt[j][l];
			}
		}
		cout << ans << '\n';
	}
	return 0;
}
 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...