#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long int;
#define all(a) a.begin(), a.end()
const int Mxn = 6e5 + 2;
struct segtree_sum {
vector<int> segs;
segtree_sum() {segs.assign(Mxn<<1, 0);}
void upd(int i, int x) {
i += Mxn; segs[i] += x;
for (i >>= 1; i; i >>= 1) {
segs[i] = segs[i<<1] + segs[i<<1|1];
}
}
int query(int l, int r, int ans = 0) {
for (l += Mxn, r += Mxn + 1; l < r; l >>= 1, r >>= 1) {
if (l&1) ans += segs[l++];
if (r&1) ans += segs[--r];
}
return ans;
}
};
struct segtree_max {
vector<int> segs;
segtree_max() {segs.assign(Mxn<<1, -1);}
void upd(int i, int x) {
i += Mxn; segs[i] = x;
for (i >>= 1; i; i >>= 1) {
segs[i] = max(segs[i<<1], segs[i<<1|1]);
}
}
int query(int l, int r, int ans = -1) {
for (l += Mxn, r += Mxn + 1; l < r; l >>= 1, r >>= 1) {
if (l&1) ans = max(segs[l++], ans);
if (r&1) ans = max(segs[--r], ans);
}
return ans;
}
};
struct query {
int idx, sufix, val;
bool operator<(const query & X) {return sufix > X.sufix;}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, k, cnt = 0; cin >> n >> k;
ll ans = 0;
map<int, int> cc;
map<ll, ll> anti;
vector<pii> A(n);
for (auto &[x, y] : A) cin >> x >> y;
vector<int> T(k);
for (auto &e : T) cin >> e;
for (auto [x, y] : A) cc[x], cc[y];
for (auto e : T) cc[e];
for (auto &[x, y] : cc) y = cnt++;
for (auto &[x, y] : A) x = cc[x], y = cc[y];
for (auto &e : T) e = cc[e];
for (auto [x, y] : cc) anti[y] = x;
segtree_max St_max;
vector<query> Count(n);
for (int i = 0; i < k; i++) {
St_max.upd(T[i], i);
}
for (int i = 0; i < n; i++) {
auto &[idx, sufix, val] = Count[i];
idx = i;
auto [l, r] = A[i];
if (l > r) {
sufix = St_max.query(r, l - 1);
val = l;
} else {
sufix = St_max.query(l, r - 1);
val = r;
}
}
sort(all(Count));
cnt = 0;
segtree_sum St_sum; vector<int> Ans(n);
for (int i = k - 1; i >= 0; i--) {
while (cnt < n && Count[cnt].sufix == i) {
auto &[idx, sufix, val] = Count[cnt++];
Ans[idx] = St_sum.query(val, Mxn - 1);
}
St_sum.upd(T[i], 1);
}
for (int i = 0; i < n; i++) {
auto [l, r] = A[i];
if (l > r) {
swap(l, r);
if (St_max.query(l, r - 1) == -1) {
if (St_sum.query(r, Mxn - 1)&1) ans += anti[l];
else ans += anti[r];
} else {
if (Ans[i]&1) ans += anti[l];
else ans += anti[r];
}
} else {
if (St_max.query(l, r - 1) == -1) {
if (St_sum.query(r, Mxn - 1)&1) ans += anti[r];
else ans += anti[l];
} else {
if (Ans[i]&1) ans += anti[l];
else ans += anti[r];
}
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9932 KB |
Output is correct |
3 |
Correct |
8 ms |
9980 KB |
Output is correct |
4 |
Correct |
8 ms |
10068 KB |
Output is correct |
5 |
Correct |
7 ms |
10068 KB |
Output is correct |
6 |
Correct |
6 ms |
10068 KB |
Output is correct |
7 |
Correct |
7 ms |
10068 KB |
Output is correct |
8 |
Correct |
7 ms |
10068 KB |
Output is correct |
9 |
Correct |
7 ms |
10068 KB |
Output is correct |
10 |
Correct |
8 ms |
9812 KB |
Output is correct |
11 |
Correct |
7 ms |
9940 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
8 ms |
9940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9932 KB |
Output is correct |
3 |
Correct |
8 ms |
9980 KB |
Output is correct |
4 |
Correct |
8 ms |
10068 KB |
Output is correct |
5 |
Correct |
7 ms |
10068 KB |
Output is correct |
6 |
Correct |
6 ms |
10068 KB |
Output is correct |
7 |
Correct |
7 ms |
10068 KB |
Output is correct |
8 |
Correct |
7 ms |
10068 KB |
Output is correct |
9 |
Correct |
7 ms |
10068 KB |
Output is correct |
10 |
Correct |
8 ms |
9812 KB |
Output is correct |
11 |
Correct |
7 ms |
9940 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
8 ms |
9940 KB |
Output is correct |
14 |
Correct |
36 ms |
13228 KB |
Output is correct |
15 |
Correct |
67 ms |
16716 KB |
Output is correct |
16 |
Correct |
137 ms |
20388 KB |
Output is correct |
17 |
Correct |
159 ms |
25136 KB |
Output is correct |
18 |
Correct |
244 ms |
25088 KB |
Output is correct |
19 |
Correct |
160 ms |
25172 KB |
Output is correct |
20 |
Correct |
158 ms |
25036 KB |
Output is correct |
21 |
Correct |
139 ms |
25096 KB |
Output is correct |
22 |
Correct |
88 ms |
20188 KB |
Output is correct |
23 |
Correct |
72 ms |
18100 KB |
Output is correct |
24 |
Correct |
67 ms |
16308 KB |
Output is correct |
25 |
Correct |
92 ms |
21448 KB |
Output is correct |
26 |
Correct |
88 ms |
19740 KB |
Output is correct |
27 |
Correct |
106 ms |
20788 KB |
Output is correct |
28 |
Correct |
96 ms |
20728 KB |
Output is correct |
29 |
Correct |
128 ms |
23008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9932 KB |
Output is correct |
3 |
Correct |
8 ms |
9980 KB |
Output is correct |
4 |
Correct |
8 ms |
10068 KB |
Output is correct |
5 |
Correct |
7 ms |
10068 KB |
Output is correct |
6 |
Correct |
6 ms |
10068 KB |
Output is correct |
7 |
Correct |
7 ms |
10068 KB |
Output is correct |
8 |
Correct |
7 ms |
10068 KB |
Output is correct |
9 |
Correct |
7 ms |
10068 KB |
Output is correct |
10 |
Correct |
8 ms |
9812 KB |
Output is correct |
11 |
Correct |
7 ms |
9940 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
8 ms |
9940 KB |
Output is correct |
14 |
Correct |
36 ms |
13228 KB |
Output is correct |
15 |
Correct |
67 ms |
16716 KB |
Output is correct |
16 |
Correct |
137 ms |
20388 KB |
Output is correct |
17 |
Correct |
159 ms |
25136 KB |
Output is correct |
18 |
Correct |
244 ms |
25088 KB |
Output is correct |
19 |
Correct |
160 ms |
25172 KB |
Output is correct |
20 |
Correct |
158 ms |
25036 KB |
Output is correct |
21 |
Correct |
139 ms |
25096 KB |
Output is correct |
22 |
Correct |
88 ms |
20188 KB |
Output is correct |
23 |
Correct |
72 ms |
18100 KB |
Output is correct |
24 |
Correct |
67 ms |
16308 KB |
Output is correct |
25 |
Correct |
92 ms |
21448 KB |
Output is correct |
26 |
Correct |
88 ms |
19740 KB |
Output is correct |
27 |
Correct |
106 ms |
20788 KB |
Output is correct |
28 |
Correct |
96 ms |
20728 KB |
Output is correct |
29 |
Correct |
128 ms |
23008 KB |
Output is correct |
30 |
Correct |
290 ms |
36888 KB |
Output is correct |
31 |
Correct |
489 ms |
47428 KB |
Output is correct |
32 |
Correct |
798 ms |
60536 KB |
Output is correct |
33 |
Correct |
1406 ms |
86932 KB |
Output is correct |
34 |
Correct |
249 ms |
34252 KB |
Output is correct |
35 |
Correct |
1444 ms |
86676 KB |
Output is correct |
36 |
Correct |
1457 ms |
86740 KB |
Output is correct |
37 |
Correct |
1529 ms |
86792 KB |
Output is correct |
38 |
Correct |
1491 ms |
86764 KB |
Output is correct |
39 |
Correct |
1403 ms |
86844 KB |
Output is correct |
40 |
Correct |
1272 ms |
86468 KB |
Output is correct |
41 |
Correct |
1436 ms |
86788 KB |
Output is correct |
42 |
Correct |
1371 ms |
86808 KB |
Output is correct |
43 |
Correct |
692 ms |
85984 KB |
Output is correct |
44 |
Correct |
721 ms |
86032 KB |
Output is correct |
45 |
Correct |
688 ms |
85948 KB |
Output is correct |
46 |
Correct |
531 ms |
51916 KB |
Output is correct |
47 |
Correct |
438 ms |
40904 KB |
Output is correct |
48 |
Correct |
854 ms |
65000 KB |
Output is correct |
49 |
Correct |
833 ms |
64816 KB |
Output is correct |