Submission #950068

#TimeUsernameProblemLanguageResultExecution timeMemory
950068TrieTrSum Zero (RMI20_sumzero)C++14
0 / 100
3 ms4700 KiB
#include<bits/stdc++.h>
using namespace std;

void local() {
    #define taskname ""
    if (fopen(taskname".inp", "r")) {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
}

#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

template<class X, class Y> bool mini(X &a, const Y &b) {return (a > b) ? a = b, true : false;}
template<class X, class Y> bool maxi(X &a, const Y &b) {return (a < b) ? a = b, true : false;}

const int N = 4e5 + 5;
const int LG = 19;

int n, q;
int arr[N];
map<ll, int> pos;
int jump[N][2]; // 0 : parent, 1 : jump
int depth[N];
bool is_main[N];
int main() {
    fastio; local();
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> arr[i];
    ll sum = 0;
    jump[n + 1][0] = jump[n + 1][1] = n + 1;
    pos[0] = n + 1;
    for(int i = n; i > 0; i--) {
    	sum += arr[i];
    	jump[i][0] = pos[sum] - 1;
    	if(jump[i][0] == -1) jump[i][0] = jump[i + 1][0];
    	else {
    		if(!mini(jump[i][0], jump[i + 1][0])) is_main[i] = true;
		}
    	pos[sum] = i;
	}
	for(int i = n; i > 0; i--) {
		if(!is_main[i]) {
			continue;
		}
		int p = jump[jump[i][0]][0];
		jump[i][0] = p;
		depth[i] += depth[p] + is_main[i];	
		if(depth[jump[p][1]] - depth[p] == depth[jump[jump[p][1]][1]] - depth[jump[p][1]]) jump[i][1] = jump[jump[p][1]][1];
		else jump[i][1] = p;
	}
	/*for(int i = n; i >= 1; i--) {
		cout << i << ' '<< jump[i][0] << ' ' << jump[i][1] << ' ' << depth[i] << '\n';
	}*/
	cin >> q;
	while(q--) {
		int l, r; cin >> l >> r;
		int res = 0;
		l = jump[l][0]; res = (l <= r && l != jump[l][0]);
		while(l <= r && is_main[l]) {
			int tmp = l;
			if(jump[l][1] <= r && jump[l][1] != l) {
				res += depth[l] - depth[jump[l][1]];
				l = jump[jump[l][1] + 1][0];
			}
			else {
				res++;
				l = jump[jump[l][0] + 1][0];
			}
		}
		cout << res << '\n';
	}
}

Compilation message (stderr)

sumzero.cpp: In function 'int main()':
sumzero.cpp:64:8: warning: unused variable 'tmp' [-Wunused-variable]
   64 |    int tmp = l;
      |        ^~~
sumzero.cpp: In function 'void local()':
sumzero.cpp:7:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:8:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...