#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n, k, t[N], ans, tindx[N * 2], indx, cnt;
priority_queue<pair<long long, long long>> pq;
struct Card{
long long a, b, aa, bb;
bool swapped;
bool operator < (Card &scnd) const{
return a > scnd.a;
}
};
Card cards[N];
struct SegTree{
long long tree[(N * 2) * 4];
void build(long long v, long long l, long long r){
if(l == r) tree[v] = tindx[l];
else{
long long m = l + (r - l) / 2;
build(v * 2, l, m);
build(v * 2 + 1, m + 1, r);
tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
}
}
long long query(long long v, long long tl, long long tr, long long l, long long r){
if(l > r) return 0;
else if(tl == l && tr == r) return tree[v];
else{
long long tm = tl + (tr - tl) / 2;
return max(query(v * 2, tl, tm, l, min(tm, r)), query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
}
};
SegTree segtree;
struct Fenwick{
long long tree[N * 2];
long long getsum(long long r){
long long sum = 0;
for(long long i = r; i >= 1; i -= i & (-i)){
sum += tree[i];
}
return sum;
}
long long query(long long l, long long r){
return getsum(r) - getsum(l - 1);
}
void update(long long pos, long long val){
for(long long i = pos; i < N * 2; i += i & (-i)){
tree[i] += val;
}
}
};
Fenwick fw;
void compress(){
vector<long long> nums;
for(long long i = 1; i <= n; i++) nums.push_back(cards[i].a), nums.push_back(cards[i].b);
for(long long i = 1; i <= k; i++) nums.push_back(t[i]);
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
for(long long i = 1; i <= n; i++){
cards[i].aa = cards[i].a;
cards[i].a = lower_bound(nums.begin(), nums.end(), cards[i].a) - nums.begin() + 1;
cards[i].bb = cards[i].b;
cards[i].b = lower_bound(nums.begin(), nums.end(), cards[i].b) - nums.begin() + 1;
}
for(long long i = 1; i <= k; i++) t[i] = lower_bound(nums.begin(), nums.end(), t[i]) - nums.begin() + 1;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> k;
for(long long i = 1; i <= n; i++){
cin >> cards[i].a >> cards[i].b;
if(cards[i].b > cards[i].a) swap(cards[i].a, cards[i].b), cards[i].swapped = true;
}
for(long long i = 1; i <= k; i++) cin >> t[i];
compress();
sort(cards + 1, cards + n + 1);
for(long long i = 1; i <= k; i++){
pq.push({t[i], i});
tindx[t[i]] = max(tindx[t[i]], i);
}
segtree.build(1, 1, N * 2 - 5);
for(long long i = 1; i <= n; i++){
while(!pq.empty() && pq.top().first >= cards[i].a){
fw.update(pq.top().second, 1);
pq.pop();
}
indx = segtree.query(1, 1, N * 2 - 1, cards[i].b, cards[i].a - 1);
cnt = fw.query(indx + 1, N * 2 - 1);
if(indx == 0){
if(cards[i].swapped) ans += (cnt % 2 == 0 ? cards[i].bb : cards[i].aa);
else ans += (cnt % 2 == 0 ? cards[i].aa : cards[i].bb);
}
else{
ans += (cnt % 2 == 0 ? cards[i].aa : cards[i].bb);
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8652 KB |
Output is correct |
2 |
Correct |
8 ms |
8628 KB |
Output is correct |
3 |
Correct |
8 ms |
8652 KB |
Output is correct |
4 |
Correct |
10 ms |
8652 KB |
Output is correct |
5 |
Correct |
8 ms |
8772 KB |
Output is correct |
6 |
Correct |
8 ms |
8652 KB |
Output is correct |
7 |
Correct |
8 ms |
8652 KB |
Output is correct |
8 |
Correct |
8 ms |
8724 KB |
Output is correct |
9 |
Correct |
8 ms |
8608 KB |
Output is correct |
10 |
Correct |
7 ms |
8652 KB |
Output is correct |
11 |
Correct |
8 ms |
8652 KB |
Output is correct |
12 |
Correct |
8 ms |
8748 KB |
Output is correct |
13 |
Correct |
9 ms |
8652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8652 KB |
Output is correct |
2 |
Correct |
8 ms |
8628 KB |
Output is correct |
3 |
Correct |
8 ms |
8652 KB |
Output is correct |
4 |
Correct |
10 ms |
8652 KB |
Output is correct |
5 |
Correct |
8 ms |
8772 KB |
Output is correct |
6 |
Correct |
8 ms |
8652 KB |
Output is correct |
7 |
Correct |
8 ms |
8652 KB |
Output is correct |
8 |
Correct |
8 ms |
8724 KB |
Output is correct |
9 |
Correct |
8 ms |
8608 KB |
Output is correct |
10 |
Correct |
7 ms |
8652 KB |
Output is correct |
11 |
Correct |
8 ms |
8652 KB |
Output is correct |
12 |
Correct |
8 ms |
8748 KB |
Output is correct |
13 |
Correct |
9 ms |
8652 KB |
Output is correct |
14 |
Incorrect |
22 ms |
9968 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8652 KB |
Output is correct |
2 |
Correct |
8 ms |
8628 KB |
Output is correct |
3 |
Correct |
8 ms |
8652 KB |
Output is correct |
4 |
Correct |
10 ms |
8652 KB |
Output is correct |
5 |
Correct |
8 ms |
8772 KB |
Output is correct |
6 |
Correct |
8 ms |
8652 KB |
Output is correct |
7 |
Correct |
8 ms |
8652 KB |
Output is correct |
8 |
Correct |
8 ms |
8724 KB |
Output is correct |
9 |
Correct |
8 ms |
8608 KB |
Output is correct |
10 |
Correct |
7 ms |
8652 KB |
Output is correct |
11 |
Correct |
8 ms |
8652 KB |
Output is correct |
12 |
Correct |
8 ms |
8748 KB |
Output is correct |
13 |
Correct |
9 ms |
8652 KB |
Output is correct |
14 |
Incorrect |
22 ms |
9968 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |