Submission #956667

#TimeUsernameProblemLanguageResultExecution timeMemory
956667Batorgil952Sum Zero (RMI20_sumzero)C++14
61 / 100
384 ms38400 KiB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define pb push_back
#define mp make_pair
 
using namespace std;
 
const int N=4e5+5;
int a[N];
int dp[N][18];
unordered_map< ll, int > M;
 
int main(){
	int n, i, j, q, l, r, ans;
	ll s;
	
	scanf("%d",&n);
	
	for(i=1; i<=n; i++){
		scanf("%d",&a[i]);
	}
	
	s=0;
	M[0]=0;
	for(i=1; i<=n; i++){
		s+=a[i];
		if(M[s]!=0){
			dp[i][0]=M[s]+1;
		}
		else{
			if(s==0) dp[i][0]=1;
			else dp[i][0]=-1;
		}
		if(dp[i][0]==-1 && i>1){
			dp[i][0]=dp[i-1][0];
		}
		else if(i>1) dp[i][0]=max(dp[i][0], dp[i-1][0]);
		M[s]=i;
	}
	
	for(i=1; i<=n; i++){
//		cout<<dp[i][1]<<" ";
		for(j=1; j<=17; j++){
			if((dp[i][j-1]-1)>0) dp[i][j]=dp[dp[i][j-1]-1][j-1];
			else dp[i][j]=-1, j=20;
// 			cout<<dp[i][j]<<" ";
		}
// 		cout<<endl;
	}
	
	scanf("%d",&q);
	
	while(q--){
		scanf("%d",&l);
		scanf("%d",&r);
		ans=0;
		for(i=17; i>=0; i--){
			if(r>=0){
				if(dp[r][i]>=l){
					ans+=(1<<(i));
					r=dp[r][i];
					r--;
				}
			}
		}
		printf("%d\n", ans);
	}
	
	return 0;
}

Compilation message (stderr)

sumzero.cpp: In function 'int main()':
sumzero.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
sumzero.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |   scanf("%d",&a[i]);
      |   ~~~~~^~~~~~~~~~~~
sumzero.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |  scanf("%d",&q);
      |  ~~~~~^~~~~~~~~
sumzero.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |   scanf("%d",&l);
      |   ~~~~~^~~~~~~~~
sumzero.cpp:57:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |   scanf("%d",&r);
      |   ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...