Submission #682843

#TimeUsernameProblemLanguageResultExecution timeMemory
682843MilosMilutinovicSum Zero (RMI20_sumzero)C++14
61 / 100
139 ms65536 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  vector<int> R(n);
  map<long long, int> mp;
  long long s = 0;
  mp[0] = n;
  for (int i = n - 1; i >= 0; i--) {
    s += c[i];
    if (mp.count(s)) {
      R[i] = mp[s] - 1;
    } else {
      R[i] = n;
    }
    mp[s] = i;
    if (i + 1 < n) {
      R[i] = min(R[i], R[i + 1]);
    }
  }
  const int L = 25;
  vector<vector<int>> pos(n, vector<int>(L));
  for (int i = 0; i < n; i++) {
    pos[i][0] = R[i];
  }
  for (int j = 1; j < L; j++) {
    for (int i = 0; i < n; i++) {
      if (pos[i][j - 1] + 1 < n) {
        pos[i][j] = pos[pos[i][j - 1] + 1][j - 1];
      } else {
        pos[i][j] = n;
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    --l; --r;
    int ans = 0;
    for (int i = L - 1; i >= 0; i--) {
      if (l < n &&
          pos[l][i] <= r) {
        ans += (1 << i);
        l = pos[l][i] + 1;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...