#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
using namespace std;
template<class T> void Max(T &a, const T &b) { a = max(a, b); }
struct SegmTree {
int n;
vector<int> t;
SegmTree(int n_) : n(n_), t(2 * n, -1) {}
void Update(int pos, int val) {
for (Max(t[pos += n], val); pos /= 2; ) {
t[pos] = max(t[2 * pos], t[2 * pos + 1]);
}
}
int Query(int l, int r) {
int ans = -1;
for (l += n, r += n + 1; l < r; l /= 2, r /= 2) {
if (l & 1) Max(ans, t[l++]);
if (r & 1) Max(ans, t[--r]);
}
return ans;
}
};
struct Fenwick {
int n;
vector<int> t;
Fenwick(int n_) : n(n_), t(n + 1) {}
inline int lsb(int x) {
return x & -x;
}
void Update(int pos, int val) {
for (++pos; pos; pos -= lsb(pos))
t[pos] += val;
}
int Query(int pos) {
int ans = 0;
for (++pos; pos <= n; pos += lsb(pos))
ans += t[pos];
return ans;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
vector<int> all;
vector<vector<int>> v(n, vector<int>(2));
for (int i = 0; i < n; ++i) {
int a, b; cin >> a >> b;
v[i] = {a, b};
all.emplace_back(a);
all.emplace_back(b);
}
vector<int> qrs(k);
for (int i = 0; i < k; ++i) {
int x; cin >> x;
all.emplace_back(x);
qrs[i] = x;
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
auto Norm = [&](int x) {
return lower_bound(all.begin(), all.end(), x) - all.begin();
};
SegmTree st(all.size());
for (int i = 0; i < k; ++i) {
qrs[i] = Norm(qrs[i]);
st.Update(qrs[i], i);
}
vector<pair<int, int>> extra;
vector<vector<pair<int, int>>> query(k);
for (int i = 0; i < n; ++i) {
int l = v[i][0], r = v[i][1];
bool sw = false;
if (l > r) swap(l, r);
else swap(v[i][0], v[i][1]), sw = true;
l = Norm(l), r = Norm(r);
int pos = st.Query(l, r);
if (pos >= 0)
query[pos].emplace_back(i, r);
else {
extra.emplace_back(i, r);
if (sw) swap(v[i][0], v[i][1]);
}
// dbg() name(i) name(v[i][0]) name(v[i][1]) name(pos) endl;
}
int64_t ans = 0;
Fenwick fw(all.size());
for (int i = k - 1; i >= 0; --i) {
fw.Update(qrs[i], +1);
for (auto &p : query[i]) {
int idx, r; tie(idx, r) = p;
int cnt = fw.Query(r);
ans += v[idx][cnt % 2];
// dbg() name(idx) name(cnt) endl;
}
}
for (auto &p : extra) {
int idx, r; tie(idx, r) = p;
int cnt = fw.Query(r);
ans += v[idx][cnt % 2];
// dbg() name(idx) name(cnt) endl;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
14 ms |
2048 KB |
Output is correct |
15 |
Correct |
28 ms |
3828 KB |
Output is correct |
16 |
Correct |
45 ms |
5364 KB |
Output is correct |
17 |
Correct |
59 ms |
7152 KB |
Output is correct |
18 |
Correct |
61 ms |
7288 KB |
Output is correct |
19 |
Correct |
61 ms |
7156 KB |
Output is correct |
20 |
Correct |
59 ms |
7152 KB |
Output is correct |
21 |
Correct |
58 ms |
7024 KB |
Output is correct |
22 |
Correct |
43 ms |
6256 KB |
Output is correct |
23 |
Incorrect |
42 ms |
6000 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
14 ms |
2048 KB |
Output is correct |
15 |
Correct |
28 ms |
3828 KB |
Output is correct |
16 |
Correct |
45 ms |
5364 KB |
Output is correct |
17 |
Correct |
59 ms |
7152 KB |
Output is correct |
18 |
Correct |
61 ms |
7288 KB |
Output is correct |
19 |
Correct |
61 ms |
7156 KB |
Output is correct |
20 |
Correct |
59 ms |
7152 KB |
Output is correct |
21 |
Correct |
58 ms |
7024 KB |
Output is correct |
22 |
Correct |
43 ms |
6256 KB |
Output is correct |
23 |
Incorrect |
42 ms |
6000 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |