#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
const int INF = 1e18;
struct FenwickTree{
vector<bool> a; int n, s;
FenwickTree(int N){ a.resize((n=N)+1); }
void add(int i){
for(++i; i<=n; i+=i&-i) a[i] = a[i] ^ 1;
}
int get(int i){
for(s=0; i>=1; i-=i&-i) s ^= a[i];
return s;
}
int get(int i, int j){ return get(j+1) ^ get(i); }
};
struct SegmentTree{
vector<int> a; int sz = 1;
SegmentTree(vector<int> &v){
while(sz < (int)v.size()) sz += sz;
a.assign(2*sz, -1);
build(v, 0, 0, sz);
}
void build(vector<int> &v, int x, int lx, int rx){
if(rx-lx==1){
if(lx < (int)v.size()) a[x] = v[lx];
return;
}
int mx = (lx + rx) / 2;
build(v, 2*x+1, lx, mx);
build(v, 2*x+2, mx, rx);
a[x] = max(a[2*x+1], a[2*x+2]);
}
void update(int i, int v, int x, int lx, int rx){
if(rx-lx==1) return void(a[x] = v);
int mx = (lx + rx) / 2;
if(i < mx) update(i, v, 2*x+1, lx, mx);
else update(i, v, 2*x+2, mx, rx);
a[x] = max(a[2*x+1], a[2*x+2]);
}
void update(int i, int v){ update(i, v, 0, 0, sz); }
int lastGreater(int v, int x, int lx, int rx){
if(rx-lx==1) return (a[x] >= v ? lx : -1);
int mx = (lx + rx) / 2;
if(a[2*x+2] >= v) return lastGreater(v, 2*x+2, mx, rx);
else return lastGreater(v, 2*x+1, lx, mx);
}
int lastGreater(int v){ return lastGreater(v, 0, 0, sz); }
};
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
array<int, 2> a[n], s[k];
for(auto &i : a) cin >> i[0] >> i[1];
vector<int> b(k);
for(int i=0; i<k; ++i) cin >> b[i], s[i] = {b[i], i};
sort(s, s+k);
int p = k, res = 0;
sort(a, a+n, [&](array<int, 2> &x, array<int, 2> &y){
return max(x[0], x[1]) > max(y[0], y[1]);
});
SegmentTree st(b);
FenwickTree f(k);
for(auto &i : a){
int x = i[0], y = i[1];
if(x > y) swap(x, y);
while(p && s[p-1][0] >= y){
--p;
st.update(s[p][1], -1);
f.add(s[p][1]);
}
int j = st.lastGreater(x);
if(j < 0) res += i[f.get(j+1, k-1)];
else{
if(i[0] > i[1]) swap(i[0], i[1]);
res += i[f.get(j+1, k-1) ^ 1];
}
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
14 |
Correct |
9 ms |
1228 KB |
Output is correct |
15 |
Correct |
17 ms |
2124 KB |
Output is correct |
16 |
Correct |
26 ms |
2892 KB |
Output is correct |
17 |
Correct |
35 ms |
3948 KB |
Output is correct |
18 |
Correct |
35 ms |
3968 KB |
Output is correct |
19 |
Correct |
33 ms |
4072 KB |
Output is correct |
20 |
Correct |
35 ms |
4076 KB |
Output is correct |
21 |
Correct |
29 ms |
4024 KB |
Output is correct |
22 |
Correct |
28 ms |
3476 KB |
Output is correct |
23 |
Correct |
26 ms |
3532 KB |
Output is correct |
24 |
Correct |
28 ms |
3600 KB |
Output is correct |
25 |
Correct |
25 ms |
3532 KB |
Output is correct |
26 |
Correct |
32 ms |
3860 KB |
Output is correct |
27 |
Correct |
35 ms |
3948 KB |
Output is correct |
28 |
Correct |
34 ms |
3964 KB |
Output is correct |
29 |
Correct |
36 ms |
4068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
14 |
Correct |
9 ms |
1228 KB |
Output is correct |
15 |
Correct |
17 ms |
2124 KB |
Output is correct |
16 |
Correct |
26 ms |
2892 KB |
Output is correct |
17 |
Correct |
35 ms |
3948 KB |
Output is correct |
18 |
Correct |
35 ms |
3968 KB |
Output is correct |
19 |
Correct |
33 ms |
4072 KB |
Output is correct |
20 |
Correct |
35 ms |
4076 KB |
Output is correct |
21 |
Correct |
29 ms |
4024 KB |
Output is correct |
22 |
Correct |
28 ms |
3476 KB |
Output is correct |
23 |
Correct |
26 ms |
3532 KB |
Output is correct |
24 |
Correct |
28 ms |
3600 KB |
Output is correct |
25 |
Correct |
25 ms |
3532 KB |
Output is correct |
26 |
Correct |
32 ms |
3860 KB |
Output is correct |
27 |
Correct |
35 ms |
3948 KB |
Output is correct |
28 |
Correct |
34 ms |
3964 KB |
Output is correct |
29 |
Correct |
36 ms |
4068 KB |
Output is correct |
30 |
Correct |
99 ms |
11388 KB |
Output is correct |
31 |
Correct |
117 ms |
12820 KB |
Output is correct |
32 |
Correct |
140 ms |
14672 KB |
Output is correct |
33 |
Correct |
180 ms |
17940 KB |
Output is correct |
34 |
Correct |
61 ms |
10948 KB |
Output is correct |
35 |
Correct |
182 ms |
17980 KB |
Output is correct |
36 |
Correct |
185 ms |
18124 KB |
Output is correct |
37 |
Correct |
190 ms |
18020 KB |
Output is correct |
38 |
Correct |
172 ms |
18064 KB |
Output is correct |
39 |
Correct |
190 ms |
18068 KB |
Output is correct |
40 |
Correct |
144 ms |
17848 KB |
Output is correct |
41 |
Correct |
186 ms |
18180 KB |
Output is correct |
42 |
Correct |
186 ms |
18056 KB |
Output is correct |
43 |
Correct |
120 ms |
17328 KB |
Output is correct |
44 |
Correct |
117 ms |
17300 KB |
Output is correct |
45 |
Correct |
120 ms |
17220 KB |
Output is correct |
46 |
Correct |
141 ms |
16132 KB |
Output is correct |
47 |
Correct |
163 ms |
16144 KB |
Output is correct |
48 |
Correct |
184 ms |
18116 KB |
Output is correct |
49 |
Correct |
179 ms |
18052 KB |
Output is correct |