#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5, inf = 1e9;
int n, k;
int s[4 * N], sum[4 * N], lst[N];
bool flip[N];
void upd(int i, int x, int id = 1, int l = 1, int r = k) {
if (l == r) {
s[id] = x;
sum[id] = x != inf;
return;
}
int md = (l + r) / 2;
if (i <= md) {
upd(i, x, id * 2, l, md);
} else {
upd(i, x, id * 2 + 1, md + 1, r);
}
s[id] = min(s[id * 2], s[id * 2 + 1]);
sum[id] = sum[id * 2] + sum[id * 2 + 1];
}
int qry(int u, int v, int id = 1, int l = 1, int r = k) {
if (u <= l && r <= v) {
return sum[id];
}
int md = (l + r) / 2;
if (v <= md) {
return qry(u, v, id * 2, l, md);
}
if (md < u) {
return qry(u, v, id * 2 + 1, md + 1, r);
}
return qry(u, v, id * 2, l, md) + qry(u, v, id * 2 + 1, md + 1, r);
}
int walk(int x, int id = 1, int l = 1, int r = k) {
if (l == r) {
return l;
}
int md = (l + r) / 2;
if (s[id * 2 + 1] < x) {
return walk(x, id * 2 + 1, md + 1, r);
}
return walk(x, id * 2, l, md);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> k;
vector<array<int, 3>> pt;
for (int i = 1; i <= n; ++i) {
int a, b; cin >> a >> b;
if (a < b) {
swap(a, b);
flip[i] = 1;
}
pt.push_back({a, b, i});
}
vector<array<int, 2>> ope;
for (int i = 0; i < k; ++i) {
int x; cin >> x;
ope.push_back({x, i + 1});
}
sort(ope.rbegin(), ope.rend());
sort(pt.begin(), pt.end(), [&](auto a, auto b) {
return a[1] > b[1];
});
fill(lst + 1, lst + n + 1, 1);
auto reset = [&]() {
for (int i = 1; i <= k; ++i) {
upd(i, inf);
}
};
reset();
int j = 0;
for (auto [a, b, id] : pt) {
while (j < k && ope[j][0] >= b) {
upd(ope[j][1], ope[j][0]);
++j;
}
if (s[1] < a) {
lst[id] = walk(a) + 1;
}
}
sort(pt.rbegin(), pt.rend());
j = 0;
long long res = 0;
reset();
for (auto [a, b, id] : pt) {
while (j < k && ope[j][0] >= a) {
upd(ope[j][1], ope[j][0]);
++j;
}
if (lst[id] > k) {
res += a;
} else {
int iter = qry(lst[id], k) % 2;
array<int, 2> s = {a, b};
if (flip[id]) {
swap(s[0], s[1]);
}
if (lst[id] != 1 && s[0] == b) {
swap(s[0], s[1]);
}
res += s[iter];
}
}
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
14 |
Correct |
9 ms |
5144 KB |
Output is correct |
15 |
Correct |
18 ms |
7808 KB |
Output is correct |
16 |
Correct |
28 ms |
8364 KB |
Output is correct |
17 |
Correct |
38 ms |
8916 KB |
Output is correct |
18 |
Correct |
37 ms |
8916 KB |
Output is correct |
19 |
Correct |
35 ms |
8916 KB |
Output is correct |
20 |
Correct |
38 ms |
8912 KB |
Output is correct |
21 |
Correct |
32 ms |
8904 KB |
Output is correct |
22 |
Correct |
28 ms |
8544 KB |
Output is correct |
23 |
Correct |
29 ms |
8660 KB |
Output is correct |
24 |
Correct |
30 ms |
8400 KB |
Output is correct |
25 |
Correct |
26 ms |
8656 KB |
Output is correct |
26 |
Correct |
33 ms |
8912 KB |
Output is correct |
27 |
Correct |
35 ms |
8912 KB |
Output is correct |
28 |
Correct |
35 ms |
9024 KB |
Output is correct |
29 |
Correct |
36 ms |
8912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
14 |
Correct |
9 ms |
5144 KB |
Output is correct |
15 |
Correct |
18 ms |
7808 KB |
Output is correct |
16 |
Correct |
28 ms |
8364 KB |
Output is correct |
17 |
Correct |
38 ms |
8916 KB |
Output is correct |
18 |
Correct |
37 ms |
8916 KB |
Output is correct |
19 |
Correct |
35 ms |
8916 KB |
Output is correct |
20 |
Correct |
38 ms |
8912 KB |
Output is correct |
21 |
Correct |
32 ms |
8904 KB |
Output is correct |
22 |
Correct |
28 ms |
8544 KB |
Output is correct |
23 |
Correct |
29 ms |
8660 KB |
Output is correct |
24 |
Correct |
30 ms |
8400 KB |
Output is correct |
25 |
Correct |
26 ms |
8656 KB |
Output is correct |
26 |
Correct |
33 ms |
8912 KB |
Output is correct |
27 |
Correct |
35 ms |
8912 KB |
Output is correct |
28 |
Correct |
35 ms |
9024 KB |
Output is correct |
29 |
Correct |
36 ms |
8912 KB |
Output is correct |
30 |
Correct |
141 ms |
10368 KB |
Output is correct |
31 |
Correct |
161 ms |
12224 KB |
Output is correct |
32 |
Correct |
169 ms |
14264 KB |
Output is correct |
33 |
Correct |
207 ms |
18364 KB |
Output is correct |
34 |
Correct |
102 ms |
10020 KB |
Output is correct |
35 |
Correct |
204 ms |
18708 KB |
Output is correct |
36 |
Correct |
189 ms |
18364 KB |
Output is correct |
37 |
Correct |
215 ms |
18368 KB |
Output is correct |
38 |
Correct |
191 ms |
18876 KB |
Output is correct |
39 |
Correct |
207 ms |
18488 KB |
Output is correct |
40 |
Correct |
164 ms |
18368 KB |
Output is correct |
41 |
Correct |
187 ms |
18840 KB |
Output is correct |
42 |
Correct |
205 ms |
18620 KB |
Output is correct |
43 |
Correct |
144 ms |
17844 KB |
Output is correct |
44 |
Correct |
142 ms |
17852 KB |
Output is correct |
45 |
Correct |
138 ms |
17596 KB |
Output is correct |
46 |
Correct |
167 ms |
17084 KB |
Output is correct |
47 |
Correct |
180 ms |
16312 KB |
Output is correct |
48 |
Correct |
186 ms |
18488 KB |
Output is correct |
49 |
Correct |
186 ms |
18364 KB |
Output is correct |