이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <string.h>
#define N 200000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int aa[N], ii[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) {
int c = aa[ii[j]] != aa[i_] ? aa[ii[j]] - aa[i_] : 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 xx[N * 2 + 4];
void update(int l, int r, int x) {
int i;
for (i = l; i <= r; i++)
xx[i] += x;
}
long long query(int r) {
int i;
long long ans;
ans = 0;
for (i = 0; i <= r; i++)
ans += (long long) xx[i] * (r - i + 1);
return ans;
}
int main() {
int n, h, i, i_, j, j_, k, d;
long long ans;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]);
ii[i] = i;
}
sort(ii, 0, n);
ans = 0;
for (i = 0; i < n; i = j) {
j = i + 1;
while (j < n && aa[ii[j]] == aa[ii[i]])
j++;
memset(xx, 0, (n * 2 + 4) * sizeof *xx);
d = n + 2;
update(d, d, 1);
for (i_ = -1, h = i; h <= j; h++) {
j_ = h == j ? n : ii[h], k = j_ - i_ - 1;
if (k > 0) {
ans += query(d - 2) - query(d - k - 2);
update(d - k, d - 1, 1);
d -= k;
}
if (h < j) {
d++;
ans += query(d - 1) - query(d - 2);
update(d, d, 1);
i_ = ii[h];
}
}
}
printf("%lld\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.c: In function 'main':
Main.c:59:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d", &aa[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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |