Submission #950341

#TimeUsernameProblemLanguageResultExecution timeMemory
950341TrieTrSum Zero (RMI20_sumzero)C++14
61 / 100
234 ms22280 KiB
#include<bits/stdc++.h> using namespace std; void local() { #define taskname "test" if (fopen(taskname".inp", "r")) { freopen(taskname".inp", "r", stdin); freopen(taskname".out", "w", stdout); } } #define ll long long #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; int n, q, res; int arr[N]; map<ll, int> pos; bitset<N> vis; int nxt[N]; int h[N], jump[N]; void make_leaf(int& id, int& p) { h[id] = h[p] + 1; if(h[jump[p]] - h[p] == h[jump[jump[p]]] - h[jump[p]]) jump[id] = jump[jump[p]]; else jump[id] = p; } int main() { fastio; local(); cin >> n; for(int i = 1; i <= n; i++) cin >> arr[i]; ll sum = 0; nxt[n + 1] = n + 1; pos[0] = n + 1; for(int i = n; i > 0; i--) { sum += arr[i]; nxt[i] = pos[sum] - 1; if(nxt[i] == -1) nxt[i] = nxt[i + 1]; else mini(nxt[i], nxt[i + 1]); pos[sum] = i; } for(int i = n; i > 0; i--) { int u = nxt[i]; if(vis[u] || u > n) continue; vis.set(u); int p = nxt[u + 1]; h[u] = h[p] + 1; if(h[jump[p]] - h[p] == h[jump[jump[p]]] - h[jump[p]]) jump[u] = jump[jump[p]]; else jump[u] = p; } cin >> q; while(q--) { int l, r; cin >> l >> r; l = nxt[l]; if(l > r) { cout << "0\n"; continue; } res = 0; while(l <= r && l != 0) { if(jump[l] <= r && jump[l] != 0) { res += h[l] - h[jump[l]]; l = jump[l]; } else { res++; l = nxt[l + 1]; } } 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...