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 1000000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int yy[N];
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)
if (yy[ii[j]] == yy[i_])
j++;
else if (yy[ii[j]] < yy[i_]) {
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], pp[N], qq[N];
static long long ans[N + 1];
int n, q, h, i;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
scanf("%d", &yy[i]);
for (i = 0; i < n; i++)
ii[i] = i;
sort(ii, 0, n);
for (i = 0; i < n; i++)
pp[i] = i - 1, qq[i] = i + 1;
for (h = 0; h < n; h++) {
i = ii[h];
ans[h + 1] = ans[h] + (long long) (i - pp[i]) * (qq[i] - i);
if (pp[i] != -1)
qq[pp[i]] = qq[i];
if (qq[i] != n)
pp[qq[i]] = pp[i];
}
while (q--) {
int y, lower, upper;
scanf("%d", &y);
lower = -1, upper = n;
while (upper - lower > 1) {
h = (lower + upper) / 2;
if (yy[ii[h]] <= y)
lower = h;
else
upper = h;
}
printf("%lld\n", ans[upper]);
}
return 0;
}
Compilation message (stderr)
pilot.c: In function 'main':
pilot.c:37:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
pilot.c:39:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d", &yy[i]);
| ^~~~~~~~~~~~~~~~~~~
pilot.c:56:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d", &y);
| ^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |