Submission #1152119

#TimeUsernameProblemLanguageResultExecution timeMemory
1152119vako_pSum Zero (RMI20_sumzero)C++20
61 / 100
298 ms23184 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

const int mxN = 4e5 + 5;
int n,q;
ll* a = new ll[mxN];
int* a1 = new int[mxN];
pair<ll,int>* b = new pair<ll,int>[mxN];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		a[i] += a[i - 1];
		b[i] = {a[i], i};
	}
	b[0] = {0, 0};
	sort(b, b + n + 1);
	for(int i = 0; i < n; i++){
		if(b[i].first == b[i + 1].first) a[b[i].second] = b[i + 1].second;
		else a[b[i].second] = n + 1;
	}
	a[n + 1] = a[b[n].second] = n + 1;
	delete[] b;
	for(int i = 0; i <= n + 1; i++) a1[i] = a[i];
	delete[] a;
	int sz = 12;
	int p[n + 5][sz];
	for(int i = 0; i < sz; i++) p[n + 1][i] = n + 1; 
	for(int i = n; i >= 0; i--){
		p[i][0] = min(a1[i], p[i + 1][0]);
		for(int j = 1; j < sz; j++) p[i][j] = p[p[i][j - 1]][j - 1];
	}
	cin >> q;
	int l,r,ans;
	while(q--){
		cin >> l >> r;
		ans = 0;
		l--;
		for(int i = sz - 1; i >= 0; i--){
			while(p[l][i] <= r){
				l = p[l][i];
				ans += (1 << i);
			}
		}
		cout << ans << '\n';
	}
}
 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...