Submission #1177153

#TimeUsernameProblemLanguageResultExecution timeMemory
1177153altern23Sum Zero (RMI20_sumzero)C++17
61 / 100
1094 ms27124 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll int
#define pii pair<ll, ll>
#define fi first
#define sec second
#define ld long double

const int MAXN = 1e5;
const ll INF = 4e18;
const int MOD = 1e9 + 7;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int tc = 1;	
	// cin >> tc;	
	for(;tc--;){
		ll N; cin >> N;
		vector<pii> a(N + 1);
		ll now = 0;
		for(int i = 1; i <= N; i++){
			ll x; cin >> x;
			now += x;
			a[i] = {now, i};
		}
		
		sort(a.begin(), a.begin() + 1 + N);
		vector<vector<ll>> up(N + 2, vector<ll>(6, N + 1));
		
		for(int i = 1; i <= N; i++){
			if(a[i].fi == a[i - 1].fi){
				up[a[i - 1].sec][0] = a[i].sec;
			}
		}

		for(int LOG = 0; LOG < 6; LOG++){
			up[N + 1][LOG] = N + 1;
		}
		for(int i = N; i >= 0; --i){
			up[i][0] = min(up[i][0], up[i + 1][0]);
		}
		for(int LOG = 1; LOG < 6; LOG++){
			for(int i = 0; i <= N; i++){
				up[i][LOG] = up[up[up[up[i][LOG - 1]][LOG - 1]][LOG - 1]][LOG - 1];
			}
		}
		
		ll Q; cin >> Q;
		for(int i = 1; i <= Q; i++){
			ll l, r; cin >> l >> r;
			--l;
			ll ans = 0;
			for(int LOG = 5; LOG >= 0; --LOG){
				while(up[l][LOG] <= r){
					ans += (1LL << (LOG * 2));
					l = up[l][LOG];
				}
			}
			cout << ans << "\n";
		}
	}   
	
}

/*

*/

Compilation message (stderr)

sumzero.cpp:11:16: warning: overflow in conversion from 'double' to 'int' changes value from '4.0e+18' to '2147483647' [-Woverflow]
   11 | const ll INF = 4e18;
      |                ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...