//cre: mingga
// cay vl deo code nua
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e18;
const int N = 2e5 + 7;
int n, k;
pair<int, int> a[N], b[N];
int d[N];
vector<int> vec;
struct Segtree {
vector<int> st;
int n;
Segtree() {};
Segtree(int n): n(n) {
st.resize(n * 4 + 4, 0);
}
void update(int i, int l, int r, int u, int x) {
if(l > u or r < u) return;
if(l == r) {
st[i] = x;
return;
}
int m = (l + r) >> 1;
update(i * 2, l, m, u, x);
update(i * 2 + 1, m + 1, r, u, x);
st[i] = max(st[i * 2], st[i * 2 + 1]);
}
int get(int i, int l, int r, int u, int v) {
if(l > v or r < u) return 0;
if(u <= l and r <= v) return st[i];
int m = (l + r) >> 1;
return max(get(i * 2, l, m, u, v), get(i * 2 + 1, m + 1, r, u, v));
}
void update(int u, int x) {
update(1, 1, n, u, x);
}
int get(int l, int r) {
if(l > r) return 0;
return get(1, 1, n, l, r);
}
};
struct BIT {
vector<int> bit;
int n;
BIT() {}
BIT(int n) : n(n) {
bit.resize(n + 1, 0);
}
int get(int x) {
int ans = 0;
for(x; x > 0; x -= (x & -x)) ans += bit[x];
return ans;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
void update(int x, int u) {
for(x; x <= n; x += (x & -x)) bit[x] += u;
}
};
bool state[N];
int get_id(int x) {
return lower_bound(all(vec), x) - vec.begin() + 1;
}
vector<int> ev[N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se, vec.pb(a[i].fi), vec.pb(a[i].se);
for(int i = 1; i <= k; i++) cin >> d[i], vec.pb(d[i]);
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
for(int i = 1; i <= n; i++) {
b[i].fi = get_id(a[i].fi);
b[i].se = get_id(a[i].se);
if(b[i].fi > b[i].se) swap(b[i].fi, b[i].se);
}
Segtree st(sz(vec) + 1);
for(int i = 1; i <= k; i++) {
d[i] = get_id(d[i]);
st.update(d[i], i);
}
for(int i = 1; i <= n; i++) {
int lst = st.get(b[i].fi, b[i].se - 1);
if(lst) state[i] = 1;
else if(a[i].fi >= a[i].se) state[i] = 1;
ev[lst + 1].pb(i);
}
BIT bit(sz(vec));
int ans = 0;
for(int i = k + 1; i > 0; i--) {
if(i <= k) bit.update(d[i], 1);
for(int id : ev[i]) {
int sus = bit.get(b[id].se, sz(vec)) & 1;
sus ^= state[id];
if(sus) ans += max(a[id].fi, a[id].se);
else ans += min(a[id].fi, a[id].se);
}
}
cout << ans << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:90:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:91:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |