#include <stdio.h>
#include <string.h>
#define N 200000
#define M 200000
#define N1 (N * 2 + M)
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(M))) */
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N1], yy[N1], tt[N];
int compare_x(int i, int j) { return xx[i] != xx[j] ? xx[i] - xx[j] : i - j; }
int compare_t(int i, int j) { return tt[j] - tt[i]; }
int (*compare)(int, int);
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 = compare(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 st[N_ * 2], n_;
void build(int *aa, int n) {
int i;
n_ = 1;
while (n_ < n)
n_ <<= 1;
memset(st, -1, n_ * 2 * sizeof *st);
for (i = 0; i < n; i++)
st[n_ + aa[i]] = max(st[n_ + aa[i]], i);
for (i = n_ - 1; i > 0; i--)
st[i] = max(st[i << 1 | 0], st[i << 1 | 1]);
}
int st_query(int l, int r) {
int x = -1;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x = max(x, st[l++]);
if ((r & 1) == 0)
x = max(x, st[r--]);
}
return x;
}
int ft[M];
void ft_update(int i, int n, int x) {
while (i < n) {
ft[i] += x;
i |= i + 1;
}
}
int ft_query(int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int main() {
static int ii[N1];
int n, n1, m, i, i_, j, l, r, x, y, tmp, flip;
long long sum;
scanf("%d%d", &n, &m), n1 = n * 2 + m;
for (i = 0; i < n1; i++)
scanf("%d", &xx[i]);
for (i = 0; i < n1; i++)
ii[i] = i;
compare = compare_x, sort(ii, 0, n1);
for (i = 0, x = 0, y = -1; i < n1; i++) {
i_ = ii[i];
if (i_ >= n * 2 && x != xx[i_])
x = xx[i_], y++;
yy[i_] = y;
}
build(yy + n * 2, m);
for (i = 0; i < n; i++) {
l = yy[i << 1 | 0], r = yy[i << 1 | 1];
if (l > r)
tmp = l, l = r, r = tmp;
tt[i] = st_query(l + 1, r);
}
for (i = 0; i < n; i++)
ii[i] = i;
compare = compare_t, sort(ii, 0, n);
sum = 0;
for (i = 0, j = m - 1; i < n; i++) {
i_ = ii[i];
while (j > tt[i_])
ft_update(m - 1 - yy[n * 2 + j--], m, 1);
flip = 0;
if (tt[i_] != -1 && xx[i_ << 1 | 0] < xx[i_ << 1 | 1])
flip ^= 1;
if (ft_query(m - 2 - yy[i_ << 1 | 0]) % 2 != 0)
flip ^= 1;
sum += xx[i_ << 1 | flip];
}
printf("%lld\n", sum);
return 0;
}
Compilation message
fortune_telling2.c: In function 'main':
fortune_telling2.c:97:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d%d", &n, &m), n1 = n * 2 + m;
| ^~~~~~~~~~~~~~~~~~~~~
fortune_telling2.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d", &xx[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
408 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
408 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
312 KB |
Output is correct |
14 |
Correct |
12 ms |
1108 KB |
Output is correct |
15 |
Correct |
30 ms |
1996 KB |
Output is correct |
16 |
Correct |
37 ms |
2636 KB |
Output is correct |
17 |
Correct |
50 ms |
3680 KB |
Output is correct |
18 |
Correct |
47 ms |
3696 KB |
Output is correct |
19 |
Correct |
46 ms |
3516 KB |
Output is correct |
20 |
Correct |
50 ms |
3624 KB |
Output is correct |
21 |
Correct |
48 ms |
3492 KB |
Output is correct |
22 |
Correct |
36 ms |
3088 KB |
Output is correct |
23 |
Correct |
36 ms |
3072 KB |
Output is correct |
24 |
Correct |
41 ms |
3176 KB |
Output is correct |
25 |
Correct |
39 ms |
3104 KB |
Output is correct |
26 |
Correct |
47 ms |
3384 KB |
Output is correct |
27 |
Correct |
47 ms |
3688 KB |
Output is correct |
28 |
Correct |
43 ms |
3680 KB |
Output is correct |
29 |
Correct |
43 ms |
3640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
408 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
312 KB |
Output is correct |
14 |
Correct |
12 ms |
1108 KB |
Output is correct |
15 |
Correct |
30 ms |
1996 KB |
Output is correct |
16 |
Correct |
37 ms |
2636 KB |
Output is correct |
17 |
Correct |
50 ms |
3680 KB |
Output is correct |
18 |
Correct |
47 ms |
3696 KB |
Output is correct |
19 |
Correct |
46 ms |
3516 KB |
Output is correct |
20 |
Correct |
50 ms |
3624 KB |
Output is correct |
21 |
Correct |
48 ms |
3492 KB |
Output is correct |
22 |
Correct |
36 ms |
3088 KB |
Output is correct |
23 |
Correct |
36 ms |
3072 KB |
Output is correct |
24 |
Correct |
41 ms |
3176 KB |
Output is correct |
25 |
Correct |
39 ms |
3104 KB |
Output is correct |
26 |
Correct |
47 ms |
3384 KB |
Output is correct |
27 |
Correct |
47 ms |
3688 KB |
Output is correct |
28 |
Correct |
43 ms |
3680 KB |
Output is correct |
29 |
Correct |
43 ms |
3640 KB |
Output is correct |
30 |
Correct |
78 ms |
7876 KB |
Output is correct |
31 |
Correct |
128 ms |
9736 KB |
Output is correct |
32 |
Correct |
158 ms |
11980 KB |
Output is correct |
33 |
Correct |
269 ms |
16672 KB |
Output is correct |
34 |
Correct |
81 ms |
6616 KB |
Output is correct |
35 |
Correct |
257 ms |
16672 KB |
Output is correct |
36 |
Correct |
252 ms |
16728 KB |
Output is correct |
37 |
Correct |
282 ms |
16672 KB |
Output is correct |
38 |
Correct |
242 ms |
16048 KB |
Output is correct |
39 |
Correct |
285 ms |
16764 KB |
Output is correct |
40 |
Correct |
240 ms |
15840 KB |
Output is correct |
41 |
Correct |
256 ms |
16228 KB |
Output is correct |
42 |
Correct |
259 ms |
16584 KB |
Output is correct |
43 |
Correct |
178 ms |
15208 KB |
Output is correct |
44 |
Correct |
183 ms |
15196 KB |
Output is correct |
45 |
Correct |
201 ms |
15188 KB |
Output is correct |
46 |
Correct |
198 ms |
14028 KB |
Output is correct |
47 |
Correct |
209 ms |
14396 KB |
Output is correct |
48 |
Correct |
255 ms |
16844 KB |
Output is correct |
49 |
Correct |
231 ms |
16688 KB |
Output is correct |