#include <bits/stdc++.h>
/*
    Strat code: 
    - Thinking about how to solve the full problems: 15 min
    - Thinking about subtasks + code: 30 min
    - Stress: 10 - 15 min
    => Total need 2h15m
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 16;
int n, k;
int a[N], b[N], t[N];
int flip[N];
vector <int> comp;
struct STMAX {
    vector <int> st;
    STMAX (int _n) {
        st.assign(4 * (_n + 1), 0);
    }
    void update(int i, int l, int r, int pos, int val) {
        if (l == r) {
            st[i] = val;
            return;
        }
        int mid = (l + r) / 2;
        if (pos <= mid) update(2 * i, l, mid, pos, val);
        else update(2 * i + 1, mid + 1, r, pos, val);
        st[i] = max(st[2 * i], st[2 * i + 1]);
    }
    int get(int i, int l, int r, int u, int v) {
        if (u > r || v < l) return 0;
        if (u <= l && r <= v) return st[i];
        int mid = (l + r) / 2;
        return max(get(2 * i, l, mid, u, v), get(2 * i + 1, mid + 1, r, u, v));
    }
} stmax(3 * N);
struct ST {
    vector <int> st;
    ST (int _n) {
        st.assign(4 * (_n + 1), 0);
    }
    void update(int i, int l, int r, int pos, int val) {
        if (l == r) {
            st[i] += val;
            return;
        }
        int mid = (l + r) / 2;
        if (pos <= mid) update(2 * i, l, mid, pos, val);
        else update(2 * i + 1, mid + 1, r, pos, val);
        st[i] = st[2 * i] + st[2 * i + 1];
    }
    int get(int i, int l, int r, int u, int v) {
        if (u > r || v < l) return 0;
        if (u <= l && r <= v) return st[i];
        int mid = (l + r) / 2;
        return get(2 * i, l, mid, u, v) + get(2 * i + 1, mid + 1, r, u, v);
    }
} st(N);
vector <pii> pos;
struct item {
    int a, b, id;
};
vector <item> need;
bool cmp(item x, item y) {
    return x.a > y.a;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for (int i = 1; i <= k; i++) cin >> t[i];
    for (int i = 1; i <= n; i++) {
        if (a[i] < b[i]) {
            swap(a[i], b[i]);
            flip[i] = 1;
        }
    }
    for (int i = 1; i <= n; i++) {
        comp.push_back(a[i]);
        comp.push_back(b[i]);
    }
    for (int i = 1; i <= k; i++) comp.push_back(t[i]);
    sort(comp.begin(), comp.end());
    comp.erase(unique(comp.begin(), comp.end()), comp.end());
    int sz = comp.size();
    for (int i = 1; i <= n; i++) {
        a[i] = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin() + 1;
        b[i] = lower_bound(comp.begin(), comp.end(), b[i]) - comp.begin() + 1;
    }
    for (int i = 1; i <= k; i++) t[i] = lower_bound(comp.begin(), comp.end(), t[i]) - comp.begin() + 1;
    for (int i = 1; i <= k; i++) {
        stmax.update(1, 1, sz, t[i], i); // vi tri xa nhat
    }
    for (int i = 1; i <= k; i++) pos.emplace_back(t[i], i);
    sort(pos.begin(), pos.end(), greater<pii>());
    for (int i = 1; i <= n; i++) need.push_back({a[i], b[i], i});
    sort(need.begin(), need.end(), cmp);
    int l = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        while(l < k && pos[l].fi >= need[i].a) {
            st.update(1, 1, k, pos[l].se, 1);
            ++l;
        }
        int maxL = stmax.get(1, 1, sz, need[i].b, need[i].a - 1);
        if (maxL != 0) {
            int val = st.get(1, 1, k, maxL + 1, k);
            val %= 2;
            int t = (val ^ 1);
            if (t) ans += comp[need[i].a - 1];
            else ans += comp[need[i].b - 1];
        }
        else {
            int val = st.get(1, 1, k, 1, k);
            val %= 2;
            int t = (val ^ (1 - flip[need[i].id]));
            if (t) ans += comp[need[i].a - 1];
            else ans += comp[need[i].b - 1];
        }
    }
    cout << ans;
    return 0;
}
Compilation message (stderr)
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(".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... |