#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 6e5 + 5, inf = 1e9;
#define pb push_back
#define mp make_pair
#define ll long long
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define sz(x) (int)x.size()
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int a[N], b[N], t[N], st[4 * N], bit[N], n, k, siz;
vi q[N];
void update(int v, int l, int r, int idx, int x){
if(l > idx || r < idx)return;
if(l == idx && r == idx){
st[v] = x;
return;
}
int mid = (l + r) / 2;
update(v * 2, l, mid, idx, x);
update(v * 2 + 1, mid + 1, r, idx, x);
st[v] = max(st[v * 2], st[v * 2 + 1]);
}
int query(int v, int l, int r, int ql, int qr){
if(l > qr || r < ql)return 0;
if(l >= ql && r <= qr)return st[v];
int mid = (l + r) / 2;
return max(query(v * 2, l, mid, ql, qr), query(v * 2 + 1, mid + 1, r, ql, qr));
}
void add(int i){
if(!i)return;
while(i <= siz){
bit[i]++;
i += (i & (-i));
}
}
int sum(int i){
int res = 0;
while(i > 0){
res += bit[i];
i -= (i & (-i));
}
return res;
}
int rangesum(int l, int r){
return sum(r) - sum(l - 1);
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> k;
vi v;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i];
v.pb(a[i]);
v.pb(b[i]);
}
for(int i = 1; i <= k; i++){
cin >> t[i];
v.pb(t[i]);
}
sort(all(v));
v.resize(distance(v.begin(), unique(all(v))));
siz = v.size();
for(int i = 1; i <= k; i++){
t[i] = lower_bound(all(v), t[i]) - v.begin() + 1;
update(1, 1, siz, t[i], i);
}
ll ans = 0;
for(int i = 1; i <= n; i++){
if(a[i] == b[i]){
ans += a[i];
continue;
}
a[i] = lower_bound(all(v), a[i]) - v.begin() + 1;
b[i] = lower_bound(all(v), b[i]) - v.begin() + 1;
int mx = query(1, 1, siz, min(a[i], b[i]), max(a[i], b[i]) - 1);
if(mx && a[i] < b[i])swap(a[i], b[i]);
q[mx].pb(i);
}
for(int i = k; i >= 0; i--){
for(auto x : q[i]){
int cnt = rangesum(max(a[x], b[x]), siz);
if(cnt & 1){
swap(a[x], b[x]);
}
ans += v[a[x] - 1];
}
add(t[i]);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
15 ms |
14592 KB |
Output is correct |
4 |
Correct |
15 ms |
14592 KB |
Output is correct |
5 |
Correct |
14 ms |
14592 KB |
Output is correct |
6 |
Correct |
14 ms |
14592 KB |
Output is correct |
7 |
Correct |
14 ms |
14592 KB |
Output is correct |
8 |
Correct |
14 ms |
14592 KB |
Output is correct |
9 |
Correct |
14 ms |
14592 KB |
Output is correct |
10 |
Correct |
14 ms |
14592 KB |
Output is correct |
11 |
Correct |
14 ms |
14592 KB |
Output is correct |
12 |
Correct |
15 ms |
14592 KB |
Output is correct |
13 |
Correct |
14 ms |
14592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
15 ms |
14592 KB |
Output is correct |
4 |
Correct |
15 ms |
14592 KB |
Output is correct |
5 |
Correct |
14 ms |
14592 KB |
Output is correct |
6 |
Correct |
14 ms |
14592 KB |
Output is correct |
7 |
Correct |
14 ms |
14592 KB |
Output is correct |
8 |
Correct |
14 ms |
14592 KB |
Output is correct |
9 |
Correct |
14 ms |
14592 KB |
Output is correct |
10 |
Correct |
14 ms |
14592 KB |
Output is correct |
11 |
Correct |
14 ms |
14592 KB |
Output is correct |
12 |
Correct |
15 ms |
14592 KB |
Output is correct |
13 |
Correct |
14 ms |
14592 KB |
Output is correct |
14 |
Correct |
28 ms |
15232 KB |
Output is correct |
15 |
Correct |
50 ms |
15864 KB |
Output is correct |
16 |
Correct |
66 ms |
16756 KB |
Output is correct |
17 |
Correct |
82 ms |
17136 KB |
Output is correct |
18 |
Correct |
90 ms |
17272 KB |
Output is correct |
19 |
Correct |
95 ms |
17268 KB |
Output is correct |
20 |
Correct |
82 ms |
17140 KB |
Output is correct |
21 |
Correct |
87 ms |
17396 KB |
Output is correct |
22 |
Correct |
68 ms |
16932 KB |
Output is correct |
23 |
Correct |
73 ms |
16372 KB |
Output is correct |
24 |
Correct |
63 ms |
16372 KB |
Output is correct |
25 |
Correct |
67 ms |
17268 KB |
Output is correct |
26 |
Correct |
69 ms |
16876 KB |
Output is correct |
27 |
Correct |
78 ms |
17012 KB |
Output is correct |
28 |
Correct |
70 ms |
17012 KB |
Output is correct |
29 |
Correct |
84 ms |
17140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
15 ms |
14592 KB |
Output is correct |
4 |
Correct |
15 ms |
14592 KB |
Output is correct |
5 |
Correct |
14 ms |
14592 KB |
Output is correct |
6 |
Correct |
14 ms |
14592 KB |
Output is correct |
7 |
Correct |
14 ms |
14592 KB |
Output is correct |
8 |
Correct |
14 ms |
14592 KB |
Output is correct |
9 |
Correct |
14 ms |
14592 KB |
Output is correct |
10 |
Correct |
14 ms |
14592 KB |
Output is correct |
11 |
Correct |
14 ms |
14592 KB |
Output is correct |
12 |
Correct |
15 ms |
14592 KB |
Output is correct |
13 |
Correct |
14 ms |
14592 KB |
Output is correct |
14 |
Correct |
28 ms |
15232 KB |
Output is correct |
15 |
Correct |
50 ms |
15864 KB |
Output is correct |
16 |
Correct |
66 ms |
16756 KB |
Output is correct |
17 |
Correct |
82 ms |
17136 KB |
Output is correct |
18 |
Correct |
90 ms |
17272 KB |
Output is correct |
19 |
Correct |
95 ms |
17268 KB |
Output is correct |
20 |
Correct |
82 ms |
17140 KB |
Output is correct |
21 |
Correct |
87 ms |
17396 KB |
Output is correct |
22 |
Correct |
68 ms |
16932 KB |
Output is correct |
23 |
Correct |
73 ms |
16372 KB |
Output is correct |
24 |
Correct |
63 ms |
16372 KB |
Output is correct |
25 |
Correct |
67 ms |
17268 KB |
Output is correct |
26 |
Correct |
69 ms |
16876 KB |
Output is correct |
27 |
Correct |
78 ms |
17012 KB |
Output is correct |
28 |
Correct |
70 ms |
17012 KB |
Output is correct |
29 |
Correct |
84 ms |
17140 KB |
Output is correct |
30 |
Correct |
162 ms |
19388 KB |
Output is correct |
31 |
Correct |
218 ms |
25448 KB |
Output is correct |
32 |
Correct |
296 ms |
27752 KB |
Output is correct |
33 |
Correct |
483 ms |
36832 KB |
Output is correct |
34 |
Correct |
143 ms |
20976 KB |
Output is correct |
35 |
Correct |
477 ms |
36840 KB |
Output is correct |
36 |
Correct |
475 ms |
36932 KB |
Output is correct |
37 |
Correct |
497 ms |
36576 KB |
Output is correct |
38 |
Correct |
486 ms |
36684 KB |
Output is correct |
39 |
Correct |
486 ms |
36872 KB |
Output is correct |
40 |
Correct |
448 ms |
37092 KB |
Output is correct |
41 |
Correct |
472 ms |
36576 KB |
Output is correct |
42 |
Correct |
513 ms |
36700 KB |
Output is correct |
43 |
Correct |
310 ms |
31840 KB |
Output is correct |
44 |
Correct |
342 ms |
32352 KB |
Output is correct |
45 |
Correct |
320 ms |
33632 KB |
Output is correct |
46 |
Correct |
319 ms |
29540 KB |
Output is correct |
47 |
Correct |
287 ms |
26848 KB |
Output is correct |
48 |
Correct |
365 ms |
31840 KB |
Output is correct |
49 |
Correct |
344 ms |
32228 KB |
Output is correct |