#include <stdio.h>
#include <string.h>
#define N 200000
#define N_ (1 << 19) /* N_ = pow2(ceil(log2(N * 2 + 5))) */
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;
}
}
long long st1[N_ * 2], st2[N_ * 2]; int sz[N_ * 2], lz[N_], h_, n_;
void build(int n) {
int i;
h_ = 0;
while (1 << h_ <= n)
h_++;
n_ = 1 << h_;
for (i = 0; i < n_; i++)
sz[n_ + i] = 1;
for (i = n_ - 1; i > 0; i--)
sz[i] = sz[i << 1 | 0] + sz[i << 1 | 1];
}
void put(int i, int x) {
st1[i] += (long long) x * sz[i], st2[i] += (long long) x * sz[i] * (sz[i] + 1) / 2;
if (i < n_)
lz[i] += x;
}
void pus(int i) {
if (lz[i])
put(i << 1 | 0, lz[i]), put(i << 1 | 1, lz[i]), lz[i] = 0;
}
void pul(int i) {
if (!lz[i]) {
int l = i << 1, r = l | 1;
st1[i] = st1[l] + st1[r], st2[i] = st2[l] + st1[l] * sz[r] + st2[r];
}
}
void push(int i) {
int h;
for (h = h_; h > 0; h--)
pus(i >> h);
}
void pull(int i) {
while (i > 1)
pul(i >>= 1);
}
void update(int l, int r, int x) {
int l_ = l += n_, r_ = r += n_;
push(l_), push(r_);
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
put(l++, x);
if ((r & 1) == 0)
put(r--, x);
}
pull(l_), pull(r_);
}
long long query(int r) {
int l, s;
long long x;
l = 0;
push(l += n_), push(r += n_);
x = 0, s = 0;
for (; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0)
x += st2[r] + st1[r] * s, s += sz[r], r--;
return x;
}
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;
build(n * 2 + 4);
for (i = 0; i < n; i = j) {
j = i + 1;
while (j < n && aa[ii[j]] == aa[ii[i]])
j++;
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];
}
}
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) {
update(d - k, d - 1, -1);
d -= k;
}
if (h < j) {
d++;
update(d, d, -1);
i_ = ii[h];
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:113:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:115:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
5604 KB |
Output is correct |
2 |
Correct |
201 ms |
5912 KB |
Output is correct |
3 |
Correct |
110 ms |
3264 KB |
Output is correct |
4 |
Correct |
229 ms |
5948 KB |
Output is correct |
5 |
Correct |
225 ms |
5956 KB |
Output is correct |
6 |
Correct |
234 ms |
6088 KB |
Output is correct |
7 |
Correct |
226 ms |
6084 KB |
Output is correct |
8 |
Correct |
240 ms |
6144 KB |
Output is correct |
9 |
Correct |
232 ms |
6084 KB |
Output is correct |
10 |
Correct |
228 ms |
6312 KB |
Output is correct |
11 |
Correct |
170 ms |
12300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
190 ms |
5604 KB |
Output is correct |
19 |
Correct |
201 ms |
5912 KB |
Output is correct |
20 |
Correct |
110 ms |
3264 KB |
Output is correct |
21 |
Correct |
229 ms |
5948 KB |
Output is correct |
22 |
Correct |
225 ms |
5956 KB |
Output is correct |
23 |
Correct |
234 ms |
6088 KB |
Output is correct |
24 |
Correct |
226 ms |
6084 KB |
Output is correct |
25 |
Correct |
240 ms |
6144 KB |
Output is correct |
26 |
Correct |
232 ms |
6084 KB |
Output is correct |
27 |
Correct |
228 ms |
6312 KB |
Output is correct |
28 |
Correct |
170 ms |
12300 KB |
Output is correct |
29 |
Correct |
177 ms |
12212 KB |
Output is correct |
30 |
Correct |
137 ms |
3280 KB |
Output is correct |
31 |
Correct |
291 ms |
5884 KB |
Output is correct |
32 |
Correct |
780 ms |
13632 KB |
Output is correct |
33 |
Correct |
328 ms |
6424 KB |
Output is correct |
34 |
Correct |
348 ms |
6616 KB |
Output is correct |
35 |
Correct |
200 ms |
3908 KB |
Output is correct |
36 |
Correct |
117 ms |
3044 KB |
Output is correct |
37 |
Correct |
140 ms |
3384 KB |
Output is correct |
38 |
Correct |
306 ms |
10668 KB |
Output is correct |
39 |
Correct |
283 ms |
10572 KB |
Output is correct |
40 |
Correct |
308 ms |
10660 KB |
Output is correct |
41 |
Correct |
288 ms |
10692 KB |
Output is correct |
42 |
Correct |
286 ms |
10616 KB |
Output is correct |
43 |
Correct |
355 ms |
13536 KB |
Output is correct |
44 |
Correct |
327 ms |
13476 KB |
Output is correct |
45 |
Correct |
347 ms |
12140 KB |
Output is correct |
46 |
Correct |
328 ms |
11848 KB |
Output is correct |
47 |
Correct |
332 ms |
11844 KB |
Output is correct |
48 |
Correct |
419 ms |
12972 KB |
Output is correct |
49 |
Correct |
391 ms |
13124 KB |
Output is correct |
50 |
Correct |
403 ms |
12868 KB |
Output is correct |
51 |
Correct |
395 ms |
12956 KB |
Output is correct |
52 |
Correct |
385 ms |
13056 KB |
Output is correct |
53 |
Correct |
399 ms |
12976 KB |
Output is correct |
54 |
Correct |
370 ms |
13032 KB |
Output is correct |