Submission #597197

#TimeUsernameProblemLanguageResultExecution timeMemory
597197CaroLindaSum Zero (RMI20_sumzero)C++14
61 / 100
458 ms48512 KiB
#include <bits/stdc++.h>

#define ll long long

const int LOG = 20;
const int MAXN = 4e5+10;

using namespace std;

int N, Q, T;
ll C[MAXN];
int d[MAXN];
int dp[LOG][MAXN];
map<ll,int> mp;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	for(int i = 1; i <= N; i++)
		cin >> C[i], C[i] += C[i-1];

	dp[0][0] = 0;
	mp.insert(make_pair(0,0));

	for(int i = 1; i <= N; i++){
		d[i] = d[i-1];
		dp[0][i] = dp[0][i-1];

		if(mp.find(C[i]) != mp.end()){
			int aux = mp[C[i]];
			if( (d[aux]+1 == d[i-1] && aux >= dp[0][i] ) || d[aux]+1 > d[i-1] ){
				d[i] = d[aux]+1;
				dp[0][i] = aux;
			}
		}

		mp[C[i]] = i;
	}


	for(int i = 1; i < LOG; i++)
		for(int j = 1; j <= N; j++)
				dp[i][j] = dp[i-1][dp[i-1][j]];

	cin >> Q;

	for(int i = 1,l , r; i <= Q; i++){
		cin >> l >> r;

		int k = d[r]-d[l-1];

		if(k == 0){
			cout << "0\n";
			continue;
		}

		int aux = k;

		for(int j = LOG-1; j >= 0; j--){
			if(k < (1<<j)) continue;
			r = dp[j][r];
			k -= (1<<j);
		}

		cout << aux-(r < l-1) << "\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...