This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, k;
pair<int, int> a[maxN];
int b[maxN];
vector<int> vc;
int st[4 * maxN];
int last[maxN];
vector<int> ST[4 * maxN];
int get_id(int val)
{
return upper_bound(vc.begin(), vc.end(), val) - vc.begin();
}
void build(int id, int l, int r)
{
if(l == r)
{
st[id] = last[l];
return;
}
int mid = (l + r) / 2;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v)
{
if(l > v || r < u) return 0;
if(l >= u && r <= v) return st[id];
int mid = (l + r) / 2;
return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
vector<int> combine(vector<int> a, vector<int> b)
{
vector<int> res;
for(int v : a) res.pb(v);
for(int v : b) res.pb(v);
sort(res.begin(), res.end());
return res;
}
void Build(int id, int l, int r)
{
if(l == r)
{
ST[id].pb(b[l]);
return;
}
int mid = (l + r) / 2;
Build(id * 2, l, mid);
Build(id * 2 + 1, mid + 1, r);
ST[id] = combine(ST[id * 2], ST[id * 2 + 1]);
}
int Get(int id, int l, int r, int u, int v, int val)
{
if(l > v || r < u) return 0;
if(l >= u && r <= v) return ST[id].size() - (lower_bound(ST[id].begin(), ST[id].end(), val) - ST[id].begin());
int mid = (l + r) / 2;
return Get(id * 2, l, mid, u, v, val) + Get(id * 2 + 1, mid + 1, r, u, v, val);
}
void ReadInput()
{
cin >> n >> k;
for(int i=1; i<=n; i++)
{
cin >> a[i].fi >> a[i].se;
int _min = min(a[i].fi, a[i].se), _max = max(a[i].fi, a[i].se);
vc.pb(_min);
vc.pb(_max - 1);
}
for(int i=1; i<=k; i++)
{
cin >> b[i];
vc.pb(b[i]);
}
sort(vc.begin(), vc.end());
vc.erase(unique(vc.begin(), vc.end()), vc.end());
for(int i=1; i<=k; i++)
{
int pos = get_id(b[i]);
last[pos] = i;
}
build(1, 1, maxN);
Build(1, 1, k);
}
void Solve()
{
int res = 0;
for(int i=1; i<=n; i++)
{
int _min = min(a[i].fi, a[i].se), _max = max(a[i].fi, a[i].se);
int l = get_id(_min), r = get_id(_max - 1);
int id = get(1, 1, maxN, l, r) + 1;
int cnt = Get(1, 1, k, id, k, _max);
if(cnt & 1)
{
if(id == 1) res += a[i].se;
else res += min(a[i].fi, a[i].se);
}
else
{
if(id == 1) res += a[i].fi;
else res += max(a[i].fi, a[i].se);
}
}
cout << res;
}
int32_t main()
{
//freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
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... |