Submission #1365748

#TimeUsernameProblemLanguageResultExecution timeMemory
1365748namhhSum Zero (RMI20_sumzero)C++20
100 / 100
162 ms18776 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],nxt[N],mp[N];
ll pre[N];
vector<ll>nen;
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];
		nen.push_back(pre[i]);
	}
	nen.push_back(pre[0]);
	sort(nen.begin(),nen.end());
	nen.erase(unique(nen.begin(),nen.end()),nen.end());
	for(int i = 0; i <= n; i++) pre[i] = lower_bound(nen.begin(),nen.end(),pre[i])-nen.begin();
	for(int i = n; i >= 1; i--){
		mp[pre[i]] = i;
		if(mp[pre[i-1]] != 0) nxt[i] = mp[pre[i-1]];
		else nxt[i] = n+1;
	}
	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++) st[j] = nxt[j]+1;
		st[n+1] = st[n+2] = n+2;
		for(int j = 1; j <= i; j++){
			for(int k = 1; k <= n; k++) st[k] = st[st[k]];
		}
		for(int j = 1; j <= q; j++){
			if(st[l[j]] <= r[j]+1){
				l[j] = st[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...