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
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[N + 2], jj_[N + 2], dd[N + 2];
static char rank[N + 2];
int n, q, i, j, 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[n + 1] = jj_[n + 1] = n + 1, dd[n + 1] = 0, rank[n + 1] = -1;
for (i = 0; i <= n; i++)
jj[ii[i]] = i == n || aa[ii[i + 1]] != aa[ii[i]] ? n + 1 : ii[i + 1];
for (i = n; i >= 0; i--) {
jj[i] = min(jj[i], jj[i + 1]);
if (jj[i] == n + 1 || rank[jj[i]] != rank[jj_[jj[i]]])
jj_[i] = jj[i], rank[i] = -1;
else
jj_[i] = jj_[jj_[jj[i]]], rank[i] = rank[jj[i]] + 1;
dd[i] = dd[jj[i]] + 1;
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &i, &j), i--;
k = i;
while (jj[k] <= j)
k = jj_[k] <= j ? jj_[k] : jj[k];
printf("%d\n", dd[i] - dd[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:59:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
sumzero.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | 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... |