#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+3;
int A[N], B[N], C[N];
int x1[N], x2[N], pa[N];
map<int, int> M; int m;
// persistent segment tree
struct node{
node *l, *r;
int tl, tr, cnt;
node(int L, int R, int cnt) : l(nullptr), r(nullptr), tl(L), tr(R), cnt(cnt){};
~node(){};
};
void build(node*& t, int l, int r){
t = new node(l, r, 0);
if(l == r) return;
int mid = (l + r) / 2;
build(t->l, l, mid);
build(t->r, mid + 1, r);
}
node* upd(node* t, int tl, int tr, int q, int delta){
if(tl == tr){
node* ret = new node(tl, tr, (t ? t->cnt : 0) + delta);
return ret;
}
int tm = (tl + tr) / 2;
node* ret = new node(tl, tr, t->cnt);
ret->l = t->l, ret->r = t->r;
if(q <= tm){
ret->l = upd(t->l, tl, tm, q, delta);
}else{
ret->r = upd(t->r, tm + 1, tr, q, delta);
}
ret->cnt = (ret->l ? ret->l->cnt : 0) + (ret->r ? ret->r->cnt : 0);
return ret;
}
int qry(node* t, int r){
if(t->tl == t->tr) return t->cnt;
if(r <= t->l->tr) return qry(t->l, r);
else return t->l->cnt + qry(t->r, r);
}
int qry(node* t, int l, int r){
return qry(t, r) - qry(t, l - 1);
}
node* T[N];
// MT merge sort tree
namespace MTree{
vector<int> t[N * 4];
vector<int> a, b;
void build(int v, int l, int r){
if(l == r){
t[v].push_back(C[l + 1]);
return;
}
int m = (l + r) / 2;
build(v * 2 + 1, l, m);
build(v * 2 + 2, m + 1, r);
merge(t[v * 2 + 1].begin(), t[v * 2 + 1].end(), t[v * 2 + 2].begin(), t[v * 2 + 2].end(), back_inserter(t[v]));
}
int last(int v, int l, int r, int vl, int vr){
if(l == r) return l;
if(lower_bound(t[v * 2 + 2].begin(), t[v * 2 + 2].end(), vl) != upper_bound(t[v * 2 + 2].begin(), t[v * 2 + 2].end(), vr)){
return last(v * 2 + 2, (l + r) / 2 + 1, r, vl, vr);
}else{
return last(v * 2 + 1, l, (l + r) / 2, vl, vr);
}
}
};
int32_t main(){
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> A[i] >> B[i];
x1[i] = min(A[i], B[i]);
x2[i] = max(A[i], B[i]);
pa[i] = x1[i] != A[i];
}
for(int i = 1; i <= k; i++){
cin >> C[i];
}
{
vector<int> v; for(int i = 1; i <= k; i++) v.push_back(C[i]);
sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin());
int idx = 0; for(auto i : v) M[i] = ++idx; M[1 << 30] = ++idx; m = idx;
for(int i = 1; i <= k; i++) C[i] = M[C[i]];
}
MTree::build(0, 0, k - 1);
build(T[0], 0, m);
for(int i = 1; i <= k; i++){
T[i] = upd(T[i - 1], 0, m, C[i], 1);
}
long long ans = 0;
for(int i = 0; i < n; i++){
int y1 = M.lower_bound(x1[i])->second;
int y2 = M.lower_bound(x2[i])->second;
int l = MTree::last(0, 0, k - 1, y1, y2 - 1);
if(l != 0) pa[i] = true;
int cnt = qry(T[k], y2, m) - qry(T[l], y2, m);
pa[i] ^= cnt % 2;
ans += pa[i] ? x2[i] : x1[i];
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19800 KB |
Output is correct |
2 |
Correct |
13 ms |
19920 KB |
Output is correct |
3 |
Correct |
13 ms |
19924 KB |
Output is correct |
4 |
Correct |
15 ms |
19924 KB |
Output is correct |
5 |
Correct |
13 ms |
19884 KB |
Output is correct |
6 |
Correct |
14 ms |
19924 KB |
Output is correct |
7 |
Correct |
14 ms |
19876 KB |
Output is correct |
8 |
Correct |
13 ms |
19852 KB |
Output is correct |
9 |
Correct |
12 ms |
19924 KB |
Output is correct |
10 |
Correct |
13 ms |
19796 KB |
Output is correct |
11 |
Correct |
13 ms |
19924 KB |
Output is correct |
12 |
Correct |
15 ms |
19924 KB |
Output is correct |
13 |
Correct |
15 ms |
19924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19800 KB |
Output is correct |
2 |
Correct |
13 ms |
19920 KB |
Output is correct |
3 |
Correct |
13 ms |
19924 KB |
Output is correct |
4 |
Correct |
15 ms |
19924 KB |
Output is correct |
5 |
Correct |
13 ms |
19884 KB |
Output is correct |
6 |
Correct |
14 ms |
19924 KB |
Output is correct |
7 |
Correct |
14 ms |
19876 KB |
Output is correct |
8 |
Correct |
13 ms |
19852 KB |
Output is correct |
9 |
Correct |
12 ms |
19924 KB |
Output is correct |
10 |
Correct |
13 ms |
19796 KB |
Output is correct |
11 |
Correct |
13 ms |
19924 KB |
Output is correct |
12 |
Correct |
15 ms |
19924 KB |
Output is correct |
13 |
Correct |
15 ms |
19924 KB |
Output is correct |
14 |
Correct |
55 ms |
28992 KB |
Output is correct |
15 |
Correct |
107 ms |
39792 KB |
Output is correct |
16 |
Correct |
174 ms |
50248 KB |
Output is correct |
17 |
Correct |
246 ms |
62508 KB |
Output is correct |
18 |
Correct |
233 ms |
62520 KB |
Output is correct |
19 |
Correct |
225 ms |
62536 KB |
Output is correct |
20 |
Correct |
239 ms |
62496 KB |
Output is correct |
21 |
Correct |
212 ms |
62444 KB |
Output is correct |
22 |
Correct |
146 ms |
60492 KB |
Output is correct |
23 |
Correct |
144 ms |
60032 KB |
Output is correct |
24 |
Correct |
152 ms |
59296 KB |
Output is correct |
25 |
Correct |
140 ms |
61088 KB |
Output is correct |
26 |
Correct |
182 ms |
62352 KB |
Output is correct |
27 |
Correct |
237 ms |
62360 KB |
Output is correct |
28 |
Correct |
185 ms |
62368 KB |
Output is correct |
29 |
Correct |
277 ms |
62556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19800 KB |
Output is correct |
2 |
Correct |
13 ms |
19920 KB |
Output is correct |
3 |
Correct |
13 ms |
19924 KB |
Output is correct |
4 |
Correct |
15 ms |
19924 KB |
Output is correct |
5 |
Correct |
13 ms |
19884 KB |
Output is correct |
6 |
Correct |
14 ms |
19924 KB |
Output is correct |
7 |
Correct |
14 ms |
19876 KB |
Output is correct |
8 |
Correct |
13 ms |
19852 KB |
Output is correct |
9 |
Correct |
12 ms |
19924 KB |
Output is correct |
10 |
Correct |
13 ms |
19796 KB |
Output is correct |
11 |
Correct |
13 ms |
19924 KB |
Output is correct |
12 |
Correct |
15 ms |
19924 KB |
Output is correct |
13 |
Correct |
15 ms |
19924 KB |
Output is correct |
14 |
Correct |
55 ms |
28992 KB |
Output is correct |
15 |
Correct |
107 ms |
39792 KB |
Output is correct |
16 |
Correct |
174 ms |
50248 KB |
Output is correct |
17 |
Correct |
246 ms |
62508 KB |
Output is correct |
18 |
Correct |
233 ms |
62520 KB |
Output is correct |
19 |
Correct |
225 ms |
62536 KB |
Output is correct |
20 |
Correct |
239 ms |
62496 KB |
Output is correct |
21 |
Correct |
212 ms |
62444 KB |
Output is correct |
22 |
Correct |
146 ms |
60492 KB |
Output is correct |
23 |
Correct |
144 ms |
60032 KB |
Output is correct |
24 |
Correct |
152 ms |
59296 KB |
Output is correct |
25 |
Correct |
140 ms |
61088 KB |
Output is correct |
26 |
Correct |
182 ms |
62352 KB |
Output is correct |
27 |
Correct |
237 ms |
62360 KB |
Output is correct |
28 |
Correct |
185 ms |
62368 KB |
Output is correct |
29 |
Correct |
277 ms |
62556 KB |
Output is correct |
30 |
Correct |
619 ms |
252112 KB |
Output is correct |
31 |
Correct |
822 ms |
252832 KB |
Output is correct |
32 |
Correct |
1077 ms |
253988 KB |
Output is correct |
33 |
Correct |
1604 ms |
255740 KB |
Output is correct |
34 |
Correct |
554 ms |
253684 KB |
Output is correct |
35 |
Correct |
1611 ms |
261548 KB |
Output is correct |
36 |
Correct |
1605 ms |
261596 KB |
Output is correct |
37 |
Correct |
1618 ms |
261620 KB |
Output is correct |
38 |
Correct |
1567 ms |
261576 KB |
Output is correct |
39 |
Correct |
1643 ms |
261700 KB |
Output is correct |
40 |
Correct |
1228 ms |
261364 KB |
Output is correct |
41 |
Correct |
1568 ms |
261652 KB |
Output is correct |
42 |
Correct |
1620 ms |
261620 KB |
Output is correct |
43 |
Correct |
851 ms |
261188 KB |
Output is correct |
44 |
Correct |
850 ms |
260904 KB |
Output is correct |
45 |
Correct |
835 ms |
261004 KB |
Output is correct |
46 |
Correct |
821 ms |
247420 KB |
Output is correct |
47 |
Correct |
835 ms |
242372 KB |
Output is correct |
48 |
Correct |
1349 ms |
261712 KB |
Output is correct |
49 |
Correct |
1251 ms |
261584 KB |
Output is correct |