#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
struct segtree {
int n;
T e;
T (*f)(T, T);
vector<T> s;
segtree(int n, T (*f)(T, T), T e):
n(n), e(e), f(f), s(2 * n, e) {
}
segtree(const vector<T> &a, T (*f)(T, T), T e):
segtree(a.size(), f, e) {
for (int i = 2 * n - 1; i; i--) {
s[i] = i < n ? f(s[i * 2], s[i * 2 + 1]) : a[i - n];
}
}
void set(int i, const T &v) {
for (i += n, s[i] = v; i /= 2;) {
s[i] = f(s[i * 2], s[i * 2 + 1]);
}
}
T qry(int l, int r) {
T u(e), v(e);
for (l += n, r += n; l < r; l /= 2, r /= 2) {
if (l & 1) u = f(u, s[l++]);
if (r & 1) v = f(s[--r], v);
}
return f(u, v);
}
};
int f(int x, int y) {
return max(x, y);
}
int g(int x, int y) {
return x ^ y;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<int> A(N), B(N), T(K);
for (int i = 0; i < N; i++) cin >> A[i] >> B[i];
for (int i = 0; i < K; i++) cin >> T[i];
vector<int> v;
v.insert(v.end(), A.begin(), A.end());
v.insert(v.end(), B.begin(), B.end());
v.insert(v.end(), T.begin(), T.end());
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (auto &i : A) i = lower_bound(v.begin(), v.end(), i) - v.begin();
for (auto &i : B) i = lower_bound(v.begin(), v.end(), i) - v.begin();
for (auto &i : T) i = lower_bound(v.begin(), v.end(), i) - v.begin();
int M = v.size();
segtree<int> s1(M, f, -1);
for (int i = 0; i < K; i++) s1.set(T[i], i);
vector<int> a(N);
vector<vector<int>> b(K);
for (int i = 0; i < N; i++) {
a[i] = s1.qry(min(A[i], B[i]), max(A[i], B[i]));
if (a[i] != -1) b[a[i]].push_back(i);
}
ll ans = 0;
segtree<int> s2(M, g, 0);
for (int i = K - 1; i >= 0; i--) {
for (int j : b[i]) {
ans += s2.qry(max(A[j], B[j]), M) ? v[min(A[j], B[j])] : v[max(A[j], B[j])];
}
s2.set(T[i], s2.qry(T[i], T[i] + 1) ^ 1);
}
for (int i = 0; i < N; i++) {
if (a[i] == -1) {
ans += s2.qry(A[i], M) ? v[B[i]] : v[A[i]];
}
}
cout << ans;
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
11 ms |
1704 KB |
Output is correct |
15 |
Correct |
21 ms |
3004 KB |
Output is correct |
16 |
Correct |
33 ms |
4208 KB |
Output is correct |
17 |
Correct |
46 ms |
5748 KB |
Output is correct |
18 |
Correct |
46 ms |
5752 KB |
Output is correct |
19 |
Correct |
46 ms |
5796 KB |
Output is correct |
20 |
Correct |
46 ms |
5752 KB |
Output is correct |
21 |
Correct |
47 ms |
5748 KB |
Output is correct |
22 |
Correct |
34 ms |
4468 KB |
Output is correct |
23 |
Correct |
33 ms |
4224 KB |
Output is correct |
24 |
Correct |
35 ms |
3888 KB |
Output is correct |
25 |
Correct |
34 ms |
5152 KB |
Output is correct |
26 |
Correct |
37 ms |
4688 KB |
Output is correct |
27 |
Correct |
46 ms |
5136 KB |
Output is correct |
28 |
Correct |
38 ms |
5056 KB |
Output is correct |
29 |
Correct |
41 ms |
5244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
11 ms |
1704 KB |
Output is correct |
15 |
Correct |
21 ms |
3004 KB |
Output is correct |
16 |
Correct |
33 ms |
4208 KB |
Output is correct |
17 |
Correct |
46 ms |
5748 KB |
Output is correct |
18 |
Correct |
46 ms |
5752 KB |
Output is correct |
19 |
Correct |
46 ms |
5796 KB |
Output is correct |
20 |
Correct |
46 ms |
5752 KB |
Output is correct |
21 |
Correct |
47 ms |
5748 KB |
Output is correct |
22 |
Correct |
34 ms |
4468 KB |
Output is correct |
23 |
Correct |
33 ms |
4224 KB |
Output is correct |
24 |
Correct |
35 ms |
3888 KB |
Output is correct |
25 |
Correct |
34 ms |
5152 KB |
Output is correct |
26 |
Correct |
37 ms |
4688 KB |
Output is correct |
27 |
Correct |
46 ms |
5136 KB |
Output is correct |
28 |
Correct |
38 ms |
5056 KB |
Output is correct |
29 |
Correct |
41 ms |
5244 KB |
Output is correct |
30 |
Correct |
87 ms |
12576 KB |
Output is correct |
31 |
Correct |
123 ms |
15620 KB |
Output is correct |
32 |
Correct |
169 ms |
19388 KB |
Output is correct |
33 |
Correct |
261 ms |
27396 KB |
Output is correct |
34 |
Correct |
78 ms |
11580 KB |
Output is correct |
35 |
Correct |
256 ms |
27460 KB |
Output is correct |
36 |
Correct |
259 ms |
27684 KB |
Output is correct |
37 |
Correct |
264 ms |
26800 KB |
Output is correct |
38 |
Correct |
264 ms |
26936 KB |
Output is correct |
39 |
Correct |
260 ms |
27704 KB |
Output is correct |
40 |
Correct |
256 ms |
27464 KB |
Output is correct |
41 |
Correct |
261 ms |
27600 KB |
Output is correct |
42 |
Correct |
249 ms |
26664 KB |
Output is correct |
43 |
Correct |
184 ms |
26932 KB |
Output is correct |
44 |
Correct |
183 ms |
26756 KB |
Output is correct |
45 |
Correct |
179 ms |
26804 KB |
Output is correct |
46 |
Correct |
178 ms |
20772 KB |
Output is correct |
47 |
Correct |
169 ms |
19236 KB |
Output is correct |
48 |
Correct |
223 ms |
23848 KB |
Output is correct |
49 |
Correct |
209 ms |
23988 KB |
Output is correct |