제출 #597229

#제출 시각아이디문제언어결과실행 시간메모리
597229CaroLindaSum Zero (RMI20_sumzero)C++14
61 / 100
231 ms25020 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], l[MAXN], r[MAXN];
int dp[2][MAXN];
int ans[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;
	}

	cin >> Q;

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

		ans[i] = d[r[i]]-d[l[i]-1];
	}

	int toGet = 0, toFill = 1;

	for(int i = 0; i < LOG; i++, swap(toGet, toFill)){
		for(int j = 1; j <= Q; j++){
			if((1<<i)&ans[j]){
				r[j] = dp[toGet][r[j]];
			}
		}
		for(int j= 1; j <= N; j++)
			dp[toFill][j] = dp[toGet][dp[toGet][j]];
	}

	for(int i = 1; i <= Q; i++)
		cout << ans[i]-(r[i] < l[i]-1) << "\n";

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...