Submission #950012

#TimeUsernameProblemLanguageResultExecution timeMemory
950012TrieTrSum Zero (RMI20_sumzero)C++14
0 / 100
5 ms4696 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 lg_step[N]; int depth[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; jump[i][0] = (jump[i][0] == -1 ? jump[i + 1][0] : min(jump[i][0], jump[i + 1][0])); pos[sum] = i; } for(int i = n; i > 0; i--) { int p = jump[i][0]; depth[i] += depth[p] + 1; 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; while(jump[l][0] <= r) { int tmp = l; if(jump[l][1] <= r && jump[l][1] != l) { res += depth[l] - depth[jump[l][1]]; l = jump[l][1] + 1; } else { res++; l = jump[l][0] + 1; } //cout << tmp << ' ' << l << ' ' << res << '\n'; } cout << res << '\n'; } }

Compilation message (stderr)

sumzero.cpp: In function 'int main()':
sumzero.cpp:56:8: warning: unused variable 'tmp' [-Wunused-variable]
   56 |    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...