Submission #682867

#TimeUsernameProblemLanguageResultExecution timeMemory
682867MilosMilutinovicSum Zero (RMI20_sumzero)C++14
61 / 100
448 ms23780 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX = 400000;
const int L = 3;

int n;
int R[MAX];
int pw[L + 1];
int pos[MAX][L];
int i;

unordered_map<long long, int> mp;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> R(n);
  for (i = 0; i < n; i++) {
    cin >> R[i];
  }
  long long s = 0;
  mp[0] = n;
  for (i = n - 1; i >= 0; i--) {
    s += R[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]);
    }
  }
  mp.clear();
  pw[0] = 16;
  for (int i = 1; i < L; i++) {
    pw[i] = pw[i - 1] * 16;
  }
  int j, x, iter;
  for (i = 0; i < n; i++) {
    x = R[i];
    for (j = 0; j < 15; j++) {
      if (x + 1 < n) {
        x = R[x + 1];
      } else {
        x = n;
      }
    }
    pos[i][0] = x;
  }
  for (j = 1; j < L; j++) {
    for (i = 0; i < n; i++) {
      x = pos[i][j - 1];
      for (iter = 0; iter < 15; iter++) {
        if (x + 1 < n) {
          x = pos[x + 1][j - 1];
        } else {
          x = n;
        }
      }
      pos[i][j] = x;
    }
  }
  int q;
  cin >> q;
  int l, r, ans;
  while (q--) {
    cin >> l >> r;
    --l; --r;
    ans = 0;
    for (i = L - 1; i >= 0; i--) {
      while (l < n && pos[l][i] <= r) {
        ans += pw[i];
        l = pos[l][i] + 1;
      }
    }
    while (l < n && R[l] <= r) {
      ans++;
      l = R[l] + 1;
    }
    cout << ans << '\n';
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...