Submission #537763

#TimeUsernameProblemLanguageResultExecution timeMemory
537763AdamGSSum Zero (RMI20_sumzero)C++17
61 / 100
510 ms23580 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=4e5+7, K=8;
pair<ll,int>T[LIM];
int nxt[LIM][5], pot[11], n, q;
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> n;
  rep(i, n) {
    cin >> T[i+1].st;
    T[i+1].st+=T[i].st;
    T[i+1].nd=i+1;
  }
  stable_sort(T, T+n+1);
  rep(i, n+1) {
    if(T[i].st==T[i+1].st) {
      nxt[T[i].nd+1][0]=T[i+1].nd;
    } else nxt[T[i].nd+1][0]=n+1;
  }
  rep(i, 5) nxt[n+1][i]=n+1;
  for(int i=n; i; --i) {
    nxt[i][0]=min(nxt[i][0], nxt[i+1][0]);
    for(int j=1; j<5; ++j) {
      int akt=i;
      rep(l, K) {
        akt=nxt[akt][j-1]+1;
        if(akt==n+2) break;
      }
      nxt[i][j]=akt-1;
    }
  }
  pot[0]=1;
  for(int i=1; i<=5; ++i) pot[i]=pot[i-1]*K;
  int q;
  cin >> q;
  while(q--) {
    int l, r, ans=0;
    cin >> l >> r;
    for(int i=4; i>=0; --i) {
      while(nxt[l][i]<=r) {
        ans+=pot[i];
        l=nxt[l][i]+1;
      }
    }
    cout << ans << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...