Submission #1365742

#TimeUsernameProblemLanguageResultExecution timeMemory
1365742namhhSum Zero (RMI20_sumzero)C++20
61 / 100
191 ms26528 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
const int N = 4e5+5;
const int lg = 18;
int n,q,a[N],st[N],l[N],r[N],ans[N],pr[N],nxt[N];
ll pre[N];
map<ll,int>mp;
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		pre[i] = pre[i-1]+a[i];
	}
	for(int i = n; i >= 1; i--){
		mp[pre[i]] = i;
		if(mp.count(pre[i-1])) nxt[i] = mp[pre[i-1]];
		else nxt[i] = n+1;
	}
	mp.clear();
	nxt[n+1] = n+1;
	for(int i = n; i >= 1; i--) nxt[i] = min(nxt[i],nxt[i+1]);
	cin >> q;
	for(int i = 1; i <= q; i++) cin >> l[i] >> r[i];
	for(int i = lg; i >= 0; i--){
		for(int j = 1; j <= n; j++) pr[j] = nxt[j]+1;
		pr[n+1] = pr[n+2] = n+2;
		for(int j = 1; j <= i; j++){
			for(int k = 1; k <= n; k++){
				st[k] = pr[pr[k]];
				pr[k] = st[k];
			}
		}
		for(int j = 1; j <= q; j++){
			if(pr[l[j]] <= r[j]+1){
				l[j] = pr[l[j]];
				ans[j] += (1 << i);
			}
		}
	}
    for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...