#include <bits/stdc++.h>
using namespace std;
int a[200005], b[200005], c[200005], res[200005], t[2400005], tt[2400005], dd[600005], z = 0;
map<int, int> m;
vector<pair<int, int>> g[200005];
void update(int v, int tl, int tr, int x, int y) {
if (tl == tr) {
t[v] = max(t[v], y);
return;
}
int mid = (tl + tr) / 2;
if (mid >= x) update(v * 2, tl, mid, x, y);
else update(v * 2 + 1, mid + 1, tr, x, y);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (tl == l && tr == r) {
return t[v];
}
int mid = (tl + tr) / 2;
return max(sum(v * 2, tl, mid, l, min(r, mid)), sum(v * 2 + 1, mid + 1, tr, max(l, mid + 1), r));
}
void update2(int v, int tl, int tr, int x) {
if (tl == tr) {
tt[v]++;
return;
}
int mid = (tl + tr) / 2;
if (mid >= x) update2(v * 2, tl, mid, x);
else update2(v * 2 + 1, mid + 1, tr, x);
tt[v] = tt[v * 2] + tt[v * 2 + 1];
}
int sum2(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (tl == l && tr == r) {
return tt[v];
}
int mid = (tl + tr) / 2;
return sum2(v * 2, tl, mid, l, min(r, mid)) + sum2(v * 2 + 1, mid + 1, tr, max(l, mid + 1), r);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
vector<int> v;
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
v.push_back(a[i]);
v.push_back(b[i]);
}
for (int i = 1; i <= q; i++) {
cin >> c[i];
v.push_back(c[i]);
}
sort(v.begin(), v.end());
for (int w : v) {
if (!m.count(w)) {
m[w] = ++z;
dd[z] = w;
}
}
for (int i = 1; i <= n; i++) a[i] = m[a[i]], b[i] = m[b[i]];
for (int i = 1; i <= q; i++) {
c[i] = m[c[i]];
update(1, 1, z, c[i], i);
}
for (int i = 1; i <= n; i++) {
if (a[i] == b[i]) res[i] = 1;
else {
if (a[i] < b[i]) {
int h = sum(1, 1, z, a[i], b[i] - 1) + 1;
if (h != 1) {
res[i] = 1;
}
g[h].push_back({i, b[i]});
}
else {
int h = sum(1, 1, z, b[i], a[i] - 1) + 1;
g[h].push_back({i, a[i]});
}
}
}
for (int i = q; i >= 1; i--) {
update2(1, 1, z, c[i]);
for (auto w : g[i]) {
int h = sum2(1, 1, z, w.second, z);
if (h & 1) res[w.first] ^= 1;
}
}
long long int d = 0;
for (int i = 1; i <= n; i++) {
if (res[i] == 0) d += dd[a[i]];
else d += dd[b[i]];
}
cout << d;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
5 ms |
13136 KB |
Output is correct |
4 |
Correct |
4 ms |
13136 KB |
Output is correct |
5 |
Correct |
4 ms |
13136 KB |
Output is correct |
6 |
Correct |
4 ms |
11088 KB |
Output is correct |
7 |
Correct |
4 ms |
13136 KB |
Output is correct |
8 |
Correct |
4 ms |
13136 KB |
Output is correct |
9 |
Correct |
3 ms |
13136 KB |
Output is correct |
10 |
Correct |
3 ms |
13048 KB |
Output is correct |
11 |
Correct |
4 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
5 ms |
13136 KB |
Output is correct |
4 |
Correct |
4 ms |
13136 KB |
Output is correct |
5 |
Correct |
4 ms |
13136 KB |
Output is correct |
6 |
Correct |
4 ms |
11088 KB |
Output is correct |
7 |
Correct |
4 ms |
13136 KB |
Output is correct |
8 |
Correct |
4 ms |
13136 KB |
Output is correct |
9 |
Correct |
3 ms |
13136 KB |
Output is correct |
10 |
Correct |
3 ms |
13048 KB |
Output is correct |
11 |
Correct |
4 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
22 ms |
14672 KB |
Output is correct |
15 |
Correct |
49 ms |
17100 KB |
Output is correct |
16 |
Correct |
74 ms |
19656 KB |
Output is correct |
17 |
Correct |
102 ms |
22216 KB |
Output is correct |
18 |
Correct |
97 ms |
19336 KB |
Output is correct |
19 |
Correct |
100 ms |
21232 KB |
Output is correct |
20 |
Correct |
102 ms |
19552 KB |
Output is correct |
21 |
Correct |
103 ms |
19656 KB |
Output is correct |
22 |
Correct |
76 ms |
20424 KB |
Output is correct |
23 |
Correct |
67 ms |
15512 KB |
Output is correct |
24 |
Correct |
61 ms |
18376 KB |
Output is correct |
25 |
Correct |
74 ms |
21068 KB |
Output is correct |
26 |
Correct |
72 ms |
19912 KB |
Output is correct |
27 |
Correct |
86 ms |
20424 KB |
Output is correct |
28 |
Correct |
74 ms |
17604 KB |
Output is correct |
29 |
Correct |
96 ms |
21448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
5 ms |
13136 KB |
Output is correct |
4 |
Correct |
4 ms |
13136 KB |
Output is correct |
5 |
Correct |
4 ms |
13136 KB |
Output is correct |
6 |
Correct |
4 ms |
11088 KB |
Output is correct |
7 |
Correct |
4 ms |
13136 KB |
Output is correct |
8 |
Correct |
4 ms |
13136 KB |
Output is correct |
9 |
Correct |
3 ms |
13136 KB |
Output is correct |
10 |
Correct |
3 ms |
13048 KB |
Output is correct |
11 |
Correct |
4 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
22 ms |
14672 KB |
Output is correct |
15 |
Correct |
49 ms |
17100 KB |
Output is correct |
16 |
Correct |
74 ms |
19656 KB |
Output is correct |
17 |
Correct |
102 ms |
22216 KB |
Output is correct |
18 |
Correct |
97 ms |
19336 KB |
Output is correct |
19 |
Correct |
100 ms |
21232 KB |
Output is correct |
20 |
Correct |
102 ms |
19552 KB |
Output is correct |
21 |
Correct |
103 ms |
19656 KB |
Output is correct |
22 |
Correct |
76 ms |
20424 KB |
Output is correct |
23 |
Correct |
67 ms |
15512 KB |
Output is correct |
24 |
Correct |
61 ms |
18376 KB |
Output is correct |
25 |
Correct |
74 ms |
21068 KB |
Output is correct |
26 |
Correct |
72 ms |
19912 KB |
Output is correct |
27 |
Correct |
86 ms |
20424 KB |
Output is correct |
28 |
Correct |
74 ms |
17604 KB |
Output is correct |
29 |
Correct |
96 ms |
21448 KB |
Output is correct |
30 |
Correct |
224 ms |
28112 KB |
Output is correct |
31 |
Correct |
320 ms |
37820 KB |
Output is correct |
32 |
Correct |
409 ms |
44488 KB |
Output is correct |
33 |
Correct |
715 ms |
66840 KB |
Output is correct |
34 |
Correct |
193 ms |
29636 KB |
Output is correct |
35 |
Correct |
712 ms |
65692 KB |
Output is correct |
36 |
Correct |
745 ms |
68352 KB |
Output is correct |
37 |
Correct |
717 ms |
69304 KB |
Output is correct |
38 |
Correct |
779 ms |
66748 KB |
Output is correct |
39 |
Correct |
668 ms |
66488 KB |
Output is correct |
40 |
Correct |
601 ms |
67764 KB |
Output is correct |
41 |
Correct |
729 ms |
65324 KB |
Output is correct |
42 |
Correct |
776 ms |
65220 KB |
Output is correct |
43 |
Correct |
477 ms |
58012 KB |
Output is correct |
44 |
Correct |
485 ms |
58876 KB |
Output is correct |
45 |
Correct |
509 ms |
60384 KB |
Output is correct |
46 |
Correct |
414 ms |
40884 KB |
Output is correct |
47 |
Correct |
406 ms |
31412 KB |
Output is correct |
48 |
Correct |
544 ms |
47184 KB |
Output is correct |
49 |
Correct |
509 ms |
52732 KB |
Output is correct |