#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int N = 2e5 + 5;
const int LOG = 17;
int n, k, st[LOG + 1][N];
pair<int, int> p[N], t[N];
void preprocess() {
for (int i = 1; i <= n; ++i) st[0][i] = t[i].S;
for (int j = 1; j <= LOG; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; ++i)
st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
int queryMax(int l, int r) {
int k = __lg(r - l + 1);
return max(st[k][l], st[k][r - (1 << k) + 1]);
}
struct BIT {
int bit[N];
void update(int id, int v) {
for (; id > 0; id -= id & -id)
bit[id] += v;
}
int get(int id) {
int res = 0;
for (; id <= n; id += id & -id)
res += bit[id];
return res;
}
} BIT;
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; ++i)
cin >> p[i].F >> p[i].S;
for (int i = 1; i <= k; ++i) {
cin >> t[i].F;
t[i].S = i;
}
sort(t + 1, t + 1 + k);
sort(p + 1, p + 1 + n, [&](pair<int, int> A, pair<int, int> B){
return max(A.F, A.S) > max(B.F, B.S);
});
preprocess();
int ptr = k;
ll res = 0;
for (int i = 1; i <= n; ++i)
if (p[i].F == p[i].S)
res += p[i].F;
else {
while (ptr >= 1 && t[ptr].F >= max(p[i].F, p[i].S))
BIT.update(t[ptr--].S, 1);
int mn = min(p[i].F, p[i].S), mx = max(p[i].F, p[i].S);
int l = lower_bound(t + 1, t + 1 + n, make_pair(mn, 0)) - t;
int r = upper_bound(t + 1, t + 1 + n, make_pair(mx - 1, n)) - t - 1;
int lst = 0;
if (l <= r && r <= n) lst = queryMax(l, r);
int cnt = BIT.get(lst + 1);
if (lst == 0) {
if (cnt & 1)
res += p[i].S;
else
res += p[i].F;
} else {
if (cnt & 1)
res += mn;
else
res += mx;
}
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) solve();
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |