#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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
110884 KB |
Output is correct |
2 |
Correct |
58 ms |
110808 KB |
Output is correct |
3 |
Correct |
55 ms |
110844 KB |
Output is correct |
4 |
Correct |
56 ms |
110920 KB |
Output is correct |
5 |
Correct |
55 ms |
110868 KB |
Output is correct |
6 |
Correct |
55 ms |
110952 KB |
Output is correct |
7 |
Correct |
57 ms |
110896 KB |
Output is correct |
8 |
Correct |
55 ms |
110936 KB |
Output is correct |
9 |
Correct |
54 ms |
110916 KB |
Output is correct |
10 |
Correct |
65 ms |
110888 KB |
Output is correct |
11 |
Correct |
73 ms |
110888 KB |
Output is correct |
12 |
Correct |
56 ms |
110924 KB |
Output is correct |
13 |
Correct |
56 ms |
110840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
110884 KB |
Output is correct |
2 |
Correct |
58 ms |
110808 KB |
Output is correct |
3 |
Correct |
55 ms |
110844 KB |
Output is correct |
4 |
Correct |
56 ms |
110920 KB |
Output is correct |
5 |
Correct |
55 ms |
110868 KB |
Output is correct |
6 |
Correct |
55 ms |
110952 KB |
Output is correct |
7 |
Correct |
57 ms |
110896 KB |
Output is correct |
8 |
Correct |
55 ms |
110936 KB |
Output is correct |
9 |
Correct |
54 ms |
110916 KB |
Output is correct |
10 |
Correct |
65 ms |
110888 KB |
Output is correct |
11 |
Correct |
73 ms |
110888 KB |
Output is correct |
12 |
Correct |
56 ms |
110924 KB |
Output is correct |
13 |
Correct |
56 ms |
110840 KB |
Output is correct |
14 |
Correct |
74 ms |
113812 KB |
Output is correct |
15 |
Correct |
94 ms |
117208 KB |
Output is correct |
16 |
Correct |
116 ms |
119348 KB |
Output is correct |
17 |
Correct |
141 ms |
124304 KB |
Output is correct |
18 |
Correct |
139 ms |
124268 KB |
Output is correct |
19 |
Correct |
144 ms |
124188 KB |
Output is correct |
20 |
Correct |
142 ms |
124332 KB |
Output is correct |
21 |
Correct |
136 ms |
124240 KB |
Output is correct |
22 |
Correct |
124 ms |
123420 KB |
Output is correct |
23 |
Correct |
127 ms |
123348 KB |
Output is correct |
24 |
Correct |
125 ms |
123180 KB |
Output is correct |
25 |
Correct |
124 ms |
123556 KB |
Output is correct |
26 |
Correct |
126 ms |
123552 KB |
Output is correct |
27 |
Correct |
144 ms |
124008 KB |
Output is correct |
28 |
Correct |
128 ms |
124080 KB |
Output is correct |
29 |
Correct |
146 ms |
124116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
110884 KB |
Output is correct |
2 |
Correct |
58 ms |
110808 KB |
Output is correct |
3 |
Correct |
55 ms |
110844 KB |
Output is correct |
4 |
Correct |
56 ms |
110920 KB |
Output is correct |
5 |
Correct |
55 ms |
110868 KB |
Output is correct |
6 |
Correct |
55 ms |
110952 KB |
Output is correct |
7 |
Correct |
57 ms |
110896 KB |
Output is correct |
8 |
Correct |
55 ms |
110936 KB |
Output is correct |
9 |
Correct |
54 ms |
110916 KB |
Output is correct |
10 |
Correct |
65 ms |
110888 KB |
Output is correct |
11 |
Correct |
73 ms |
110888 KB |
Output is correct |
12 |
Correct |
56 ms |
110924 KB |
Output is correct |
13 |
Correct |
56 ms |
110840 KB |
Output is correct |
14 |
Correct |
74 ms |
113812 KB |
Output is correct |
15 |
Correct |
94 ms |
117208 KB |
Output is correct |
16 |
Correct |
116 ms |
119348 KB |
Output is correct |
17 |
Correct |
141 ms |
124304 KB |
Output is correct |
18 |
Correct |
139 ms |
124268 KB |
Output is correct |
19 |
Correct |
144 ms |
124188 KB |
Output is correct |
20 |
Correct |
142 ms |
124332 KB |
Output is correct |
21 |
Correct |
136 ms |
124240 KB |
Output is correct |
22 |
Correct |
124 ms |
123420 KB |
Output is correct |
23 |
Correct |
127 ms |
123348 KB |
Output is correct |
24 |
Correct |
125 ms |
123180 KB |
Output is correct |
25 |
Correct |
124 ms |
123556 KB |
Output is correct |
26 |
Correct |
126 ms |
123552 KB |
Output is correct |
27 |
Correct |
144 ms |
124008 KB |
Output is correct |
28 |
Correct |
128 ms |
124080 KB |
Output is correct |
29 |
Correct |
146 ms |
124116 KB |
Output is correct |
30 |
Correct |
351 ms |
164932 KB |
Output is correct |
31 |
Correct |
391 ms |
167604 KB |
Output is correct |
32 |
Correct |
467 ms |
170964 KB |
Output is correct |
33 |
Correct |
610 ms |
177456 KB |
Output is correct |
34 |
Correct |
336 ms |
164356 KB |
Output is correct |
35 |
Correct |
634 ms |
177584 KB |
Output is correct |
36 |
Correct |
621 ms |
177568 KB |
Output is correct |
37 |
Correct |
623 ms |
177736 KB |
Output is correct |
38 |
Correct |
621 ms |
177628 KB |
Output is correct |
39 |
Correct |
624 ms |
177588 KB |
Output is correct |
40 |
Correct |
566 ms |
177324 KB |
Output is correct |
41 |
Correct |
618 ms |
177620 KB |
Output is correct |
42 |
Correct |
625 ms |
177664 KB |
Output is correct |
43 |
Correct |
455 ms |
174136 KB |
Output is correct |
44 |
Correct |
461 ms |
174252 KB |
Output is correct |
45 |
Correct |
467 ms |
175020 KB |
Output is correct |
46 |
Correct |
461 ms |
173244 KB |
Output is correct |
47 |
Correct |
476 ms |
172568 KB |
Output is correct |
48 |
Correct |
521 ms |
176232 KB |
Output is correct |
49 |
Correct |
499 ms |
176056 KB |
Output is correct |