#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(vector<Info>(n_, v_));
}
template<class T>
void init(vector<T> init_) {
n = init_.size();
info.assign(4 << __lg(n), Info());
tag.assign(4 << __lg(n), Tag());
function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
int mx = 0;
void apply(const Tag &t) & {
mx = max(mx, t.mx);
}
};
struct Info {
int mx = 0;
void apply(const Tag &t) & {
mx = max(mx, t.mx);
}
Info operator+(const Info &b) {
return {max(mx, b.mx)};
}
};
struct card{
ll a, b, wa, wb, t = 0;
};
struct normalize{
vector<ll> poi, pot;
void add(ll x){
poi.push_back(x);
}
void start(){
sort(poi.begin(), poi.end());
if(poi.size() > 0) pot.push_back(poi[0]);
for(int i = 1; i < (int)poi.size(); i++){
if(poi[i] != poi[i-1]){
pot.push_back(poi[i]);
}
}
}
int encode(ll x){
return lower_bound(pot.begin(), pot.end(), x) - pot.begin()+1;
}
};
template <class T>
struct Fenwick {
int n, log;
vector<T> bit;
Fenwick(int n) : n(n), log(32 - __builtin_clz(n + 1)), bit(n + 1, 0) {}
void add(int i, T delta) {
for (; i <= n; i += i & -i) {
bit[i] += delta;
}
}
T sum(int i) {
T res = 0;
for (; i > 0; i -= i & -i) {
res += bit[i];
}
return res;
}
T sum(int l, int r) {
return sum(r)-sum(l-1);
}
int kth(T k) {
T sum = 0;
int pos = 0;
for (int l = log - 1; l >= 0; l--) {
if (pos + (1 << l) <= n && sum + bit[pos + (1 << l)] <= k) {
pos += 1 << l;
sum += bit[pos];
}
}
return pos;
}
};
void solve(){
int n, k;
cin >> n >> k;
vector<card> a(n+1);
normalize norm;
for(int i = 1; i <= n; i++){
cin >> a[i].a >> a[i].b;
a[i].wa = a[i].a; a[i].wb = a[i].b;
if(a[i].a > a[i].b){
swap(a[i].a, a[i].b);
a[i].t = 1;
}
norm.add(a[i].a);
norm.add(a[i].b);
}
vector<ll> q(k+1);
for(int i = 1; i <= k; i++){
cin >> q[i];
norm.add(q[i]);
}
norm.start();
for(int i = 1; i <= n; i++){
a[i].a = norm.encode(a[i].a);
a[i].b = norm.encode(a[i].b);
}
for(int i = 1; i <= k; i++){
q[i] = norm.encode(q[i]);
}
int N = norm.pot.size();
LazySegmentTree<Info, Tag> T(N+1);
for(int i = 1; i <= k; i++){
T.rangeApply(q[i], q[i]+1, {i});
}
vector<int> p(n+1, 0);
struct event{
ll v, id;
};
vector<vector<event>> sweep(k+2);
for(int i = 1; i <= n; i++){
p[i] = T.rangeQuery(a[i].a, a[i].b).mx;
sweep[p[i]+1].push_back({a[i].b, i});
}
vector<int> side(n+1, 0);
Fenwick<int> bit(N+1);
for(int i = k; i >= 1; i--){
bit.add(q[i], 1);
for(auto it : sweep[i]){
side[it.id] = bit.sum(it.v, N)%2;
}
}
ll ans = 0;
for(int i = 1; i <= n; i++){
if(p[i] == 0){
side[i] ^= a[i].t;
}
if(side[i]){
ans += a[i].wb;
}else{
ans += a[i].wa;
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |