#include <bits/stdc++.h>
#define ll long long
#define ok puts("ok");
using namespace std;
const int N = (int)4e5 + 7;
struct T {
int tree[N * 4];
T() {
memset(tree, -1, sizeof tree);
}
void upd(int pos, int val, int v = 1, int tl = -1, int tr = 4e5) {
if (tl == tr) {
tree[v] = val;
return ;
}
int mid = (tl + tr) >> 1;
if (pos <= mid) {
upd(pos, val, v + v, tl, mid);
} else {
upd(pos, val, v + v + 1, mid + 1, tr);
}
tree[v] = max(tree[v + v], tree[v + v + 1]);
}
int get(int l, int r, int v = 1, int tl = -1, int tr = 4e5) {
if (tl > r || tr < l) return -1;
if (l <= tl && tr <= r) return tree[v];
int mid = (tl + tr) >> 1;
return max(get(l, r, v + v, tl, mid), get(l, r, v + v + 1, mid + 1, tr));
}
};
T tr;
struct fen {
int tree[N];
fen() {
memset(tree, 0, sizeof tree);
}
void upd(int pos, int val) {
pos = max(pos, 0);
while (pos < N) {
tree[pos] += val;
pos = pos | (pos + 1);
}
}
int get(int r) {
int res = 0;
while (r >= 0) {
res += tree[r];
r = (r & (r + 1)) - 1;
}
return res;
}
};
fen bit1;
int n, k;
int flip[N];
int a[N], b[N], q[N], border[N];
ll sum = 0;
main() {
scanf("%d %d", &n, &k);
vector<int> vec;
for (int i = 1; i <= n; i++) {
scanf("%d %d", &a[i], &b[i]);
vec.push_back(a[i]);
vec.push_back(b[i]);
if (a[i] > b[i]) {
swap(a[i], b[i]);
flip[i] = 1;
}
}
for (int i = 1; i <= k; i++) {
scanf("%d", &q[i]);
}
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
b[i] = lower_bound(vec.begin(), vec.end(), b[i]) - vec.begin();
}
for (int i = 1; i <= k; i++) {
q[i] = upper_bound(vec.begin(), vec.end(), q[i]) - vec.begin() - 1;
tr.upd(q[i], i);
}
vector<pair<int, int>> v1;
for (int i = 1; i <= n; i++) {
border[i] = tr.get(a[i], b[i] - 1);
v1.push_back({border[i] + 1, i});
if (border[i] > 0) {
flip[i] = 1;
}
}
sort(v1.begin(), v1.end());
int sz = k;
for (int i = v1.size() - 1; i >= 0; i--) {
while (sz >= v1[i].first) {
bit1.upd(q[sz], 1);
sz--;
}
flip[v1[i].second] ^= ((k - sz - bit1.get(b[v1[i].second] - 1)) & 1);
}
for (int i = 1; i <= n; i++) {
sum += (flip[i] ? vec[b[i]] : vec[a[i]]);
}
printf("%lld\n", sum);
}
Compilation message
fortune_telling2.cpp:65:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a[i], &b[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8192 KB |
Output is correct |
2 |
Correct |
8 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8196 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
9 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
8 ms |
8192 KB |
Output is correct |
11 |
Correct |
8 ms |
8192 KB |
Output is correct |
12 |
Correct |
9 ms |
8192 KB |
Output is correct |
13 |
Correct |
9 ms |
8192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8192 KB |
Output is correct |
2 |
Correct |
8 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8196 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
9 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
8 ms |
8192 KB |
Output is correct |
11 |
Correct |
8 ms |
8192 KB |
Output is correct |
12 |
Correct |
9 ms |
8192 KB |
Output is correct |
13 |
Correct |
9 ms |
8192 KB |
Output is correct |
14 |
Correct |
21 ms |
8704 KB |
Output is correct |
15 |
Correct |
36 ms |
9076 KB |
Output is correct |
16 |
Correct |
51 ms |
9332 KB |
Output is correct |
17 |
Correct |
71 ms |
9968 KB |
Output is correct |
18 |
Correct |
68 ms |
10096 KB |
Output is correct |
19 |
Correct |
66 ms |
9964 KB |
Output is correct |
20 |
Correct |
69 ms |
9968 KB |
Output is correct |
21 |
Correct |
62 ms |
10096 KB |
Output is correct |
22 |
Correct |
47 ms |
9968 KB |
Output is correct |
23 |
Correct |
46 ms |
9968 KB |
Output is correct |
24 |
Correct |
45 ms |
9940 KB |
Output is correct |
25 |
Correct |
44 ms |
9972 KB |
Output is correct |
26 |
Correct |
49 ms |
9860 KB |
Output is correct |
27 |
Correct |
57 ms |
10044 KB |
Output is correct |
28 |
Correct |
57 ms |
10036 KB |
Output is correct |
29 |
Correct |
61 ms |
9968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8192 KB |
Output is correct |
2 |
Correct |
8 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8196 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
9 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
8 ms |
8192 KB |
Output is correct |
11 |
Correct |
8 ms |
8192 KB |
Output is correct |
12 |
Correct |
9 ms |
8192 KB |
Output is correct |
13 |
Correct |
9 ms |
8192 KB |
Output is correct |
14 |
Correct |
21 ms |
8704 KB |
Output is correct |
15 |
Correct |
36 ms |
9076 KB |
Output is correct |
16 |
Correct |
51 ms |
9332 KB |
Output is correct |
17 |
Correct |
71 ms |
9968 KB |
Output is correct |
18 |
Correct |
68 ms |
10096 KB |
Output is correct |
19 |
Correct |
66 ms |
9964 KB |
Output is correct |
20 |
Correct |
69 ms |
9968 KB |
Output is correct |
21 |
Correct |
62 ms |
10096 KB |
Output is correct |
22 |
Correct |
47 ms |
9968 KB |
Output is correct |
23 |
Correct |
46 ms |
9968 KB |
Output is correct |
24 |
Correct |
45 ms |
9940 KB |
Output is correct |
25 |
Correct |
44 ms |
9972 KB |
Output is correct |
26 |
Correct |
49 ms |
9860 KB |
Output is correct |
27 |
Correct |
57 ms |
10044 KB |
Output is correct |
28 |
Correct |
57 ms |
10036 KB |
Output is correct |
29 |
Correct |
61 ms |
9968 KB |
Output is correct |
30 |
Correct |
85 ms |
9436 KB |
Output is correct |
31 |
Correct |
152 ms |
10864 KB |
Output is correct |
32 |
Correct |
238 ms |
12904 KB |
Output is correct |
33 |
Correct |
402 ms |
16484 KB |
Output is correct |
34 |
Correct |
51 ms |
10872 KB |
Output is correct |
35 |
Correct |
421 ms |
22252 KB |
Output is correct |
36 |
Correct |
395 ms |
22500 KB |
Output is correct |
37 |
Correct |
408 ms |
22372 KB |
Output is correct |
38 |
Correct |
426 ms |
22244 KB |
Output is correct |
39 |
Correct |
430 ms |
22372 KB |
Output is correct |
40 |
Correct |
319 ms |
22116 KB |
Output is correct |
41 |
Correct |
414 ms |
22368 KB |
Output is correct |
42 |
Correct |
390 ms |
22436 KB |
Output is correct |
43 |
Correct |
179 ms |
21480 KB |
Output is correct |
44 |
Correct |
175 ms |
21480 KB |
Output is correct |
45 |
Correct |
181 ms |
21420 KB |
Output is correct |
46 |
Correct |
238 ms |
20068 KB |
Output is correct |
47 |
Correct |
211 ms |
20068 KB |
Output is correct |
48 |
Correct |
275 ms |
22376 KB |
Output is correct |
49 |
Correct |
300 ms |
22368 KB |
Output is correct |