This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#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;
int nxt[LIM][6], pot[6];
pair<ll,int>T[LIM];
void merge(pair<ll,int> ar1[], pair<ll,int> ar2[], int m, int n)
{
// Iterate through all elements
// of ar2[] starting from the last element
for (int i = n - 1; i >= 0; i--)
{
/* Find the smallest element greater than ar2[i].
Move all elements one position ahead till the
smallest greater element is not found */
int j;
pair<ll,int>last = ar1[m - 1];
for (j = m - 2; j >= 0
&& ar1[j] > ar2[i]; j--)
ar1[j + 1] = ar1[j];
// If there was a greater element
if (j != m - 2 || last > ar2[i])
{
ar1[j + 1] = ar2[i];
ar2[i] = last;
}
}
}
void sortuj(int l, int r) {
if(l==r) return;
int mid=(l+r)/2;
sortuj(l, mid);
sortuj(mid+1, r);
merge(T, T+mid+1, mid-l+1, r-mid);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
pot[0]=1;
for(int i=1; i<6; ++i) pot[i]=pot[i-1]*10;
int n;
cin >> n;
ll sum=0;
rep(i, n+1) nxt[i][0]=n+1;
rep(i, n) {
ll a;
cin >> a;
sum+=a;
T[i+1]={sum, i+1};
}
sortuj(0, n);
rep(i, n) {
if(T[i].st==T[i+1].st) nxt[T[i].nd][0]=T[i+1].nd;
}
for(int i=n-1; i>=0; --i) nxt[i][0]=min(nxt[i][0], nxt[i+1][0]);
rep(i, 6) nxt[n+1][i]=n+1;
for(int j=1; j<6; ++j) {
rep(i, n+1)nxt[i][j]=nxt[nxt[nxt[nxt[nxt[nxt[nxt[nxt[nxt[nxt[i][j-1]][j-1]][j-1]][j-1]][j-1]][j-1]][j-1]][j-1]][j-1]][j-1];
}
int q;
cin >> q;
while(q--) {
int l, r;
cin >> l >> r; --l;
int ans=0, i=5;
while(i>=0) {
if(nxt[l][i]<=r) {
ans+=pot[i];
l=nxt[l][i];
} else --i;
}
cout << ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |