Submission #947911

#TimeUsernameProblemLanguageResultExecution timeMemory
947911TrieTrSum Zero (RMI20_sumzero)C++14
61 / 100
390 ms50000 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][LG];
int main() {
    fastio; local();
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> arr[i];
    ll sum = 0;
    for(int i = 1; i <= n + 1; i++) for(int k = 0; k < LG; k++) jump[i][k] = n + 1;
    pos[0] = n + 1;
    for(int i = n; i > 0; i--) {
    	sum += arr[i];
    	jump[i][0] = pos[sum] - 1;
    	jump[i][0] = (jump[i][0] == -1 ? jump[i + 1][0] : min(jump[i][0], jump[i + 1][0]));
    	pos[sum] = i;
	}
	for(int k = 1; k < LG; k++) {
		for(int i = 1; i + (1 << k) - 1 <= n; i++) {
			jump[i][k] = jump[i][k - 1];
			if(jump[i][k] <= n) jump[i][k] = jump[jump[i][k] + 1][k - 1];
		}
	}
	cin >> q;
	for(int i = 1; i <= q; i++) {
		int l, r; cin >> l >> r;
		int res = 0;
		for(int k = LG - 1; k >= 0; k--) {
			while(jump[l][k] <= r) {
				l = jump[l][k] + 1; res += (1 << k);
			}
		}
		cout << res << '\n';
	}
}

Compilation message (stderr)

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...