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 <stdio.h>
#define N 400000
#define L 18 /* L = floor(log2(N + 1)) */
int min(int a, int b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long aa[N + 1];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = aa[ii[j]] != aa[i_] ? (aa[ii[j]] < aa[i_] ? -1 : 1) : ii[j] - i_;
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
int main() {
static int ii[N + 1], jj[L + 1][N + 2];
int n, q, i, j, l, k;
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%lld", &aa[i]), aa[i] += aa[i - 1];
for (i = 0; i <= n; i++)
ii[i] = i;
sort(ii, 0, n + 1);
jj[0][n + 1] = n + 1;
for (i = 0; i <= n; i++)
jj[0][ii[i]] = i == n || aa[ii[i + 1]] != aa[ii[i]] ? n + 1 : ii[i + 1];
for (i = n; i >= 0; i--)
jj[0][i] = min(jj[0][i], jj[0][i + 1]);
for (l = 0; l < L; l++)
for (i = 0; i <= n + 1; i++)
jj[l + 1][i] = jj[l][jj[l][i]];
scanf("%d", &q);
while (q--) {
scanf("%d%d", &i, &j), i--;
k = 0;
for (l = L; l >= 0; l--)
if (jj[l][i] <= j)
k += 1 << l, i = jj[l][i];
printf("%d\n", k);
}
return 0;
}
Compilation message (stderr)
sumzero.c: In function 'main':
sumzero.c:42:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
sumzero.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%lld", &aa[i]), aa[i] += aa[i - 1];
| ^~~~~~~~~~~~~~~~~~~~~
sumzero.c:56:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
sumzero.c:58:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &i, &j), i--;
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |