#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MOD = (int)1e9 + 7;
const int NS = (int)6e5 + 4;
int N, k;
int a[NS], b[NS];
int t[NS];
vector<int> srt;
int last[NS];
struct Data{
int pos, mn, num;
bool operator<(const Data&r)const{
return pos > r.pos;
}
}que[NS];
int seg[NS * 4];
void push(int num, int s, int e, int pos, int val){
if(pos < s || pos > e) return;
if(s == e){
seg[num] = val;
return;
}
push(num * 2, s, (s + e) / 2, pos, val), push(num * 2 + 1, (s + e) / 2 + 1, e, pos, val);
seg[num] = max(seg[num * 2], seg[num * 2 + 1]);
}
int get(int num, int s, int e, int fs, int fe){
if(fe < s || fs > e || fs > fe) return 0;
if(s >= fs && e <= fe){
return seg[num];
}
return max(get(num * 2, s, (s + e) / 2, fs, fe), get(num * 2 + 1, (s + e) / 2 + 1, e, fs, fe));
}
struct fenwick{
vector < int > fenw;
int N;
fenwick(){}
fenwick(int n):N(n){
fenw.resize(N);
}
void push(int x, int val){
for(int i = x; i < N; i += (i & -i)){
fenw[i] += val;
}
}
int get(int x){
int rv = 0;
for(int i = x; i; i -= (i & -i)){
rv += fenw[i];
}
return rv;
}
}fen(NS);
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> k;
for(int i = 1; i <= N; ++i){
cin >> a[i] >> b[i];
srt.push_back(a[i]), srt.push_back(b[i]);
}
for(int i = 1; i <= k; ++i){
cin >> t[i];
srt.push_back(t[i]);
}
sort(srt.begin(), srt.end());
srt.erase(unique(srt.begin(), srt.end()), srt.end());
for(int i = 1; i <= N; ++i){
a[i] = lower_bound(srt.begin(), srt.end(), a[i]) - srt.begin() + 1;
b[i] = lower_bound(srt.begin(), srt.end(), b[i]) - srt.begin() + 1;
}
for(int i = 1; i <= k; ++i){
t[i] = lower_bound(srt.begin(), srt.end(), t[i]) - srt.begin() + 1;
last[t[i]] = i;
}
for(int i = 1; i <= (int)srt.size(); ++i){
push(1, 1, (int)srt.size(), i, last[i]);
}
for(int i = 1; i <= N; ++i){
int mn = min(a[i], b[i]), mx = max(a[i], b[i]);
int pos = get(1, 1, (int)srt.size(), mn, mx - 1);
que[i].pos = pos + 1, que[i].mn = mx, que[i].num = i;
}
sort(que + 1, que + N + 1);
reverse(que + 1, que + N + 1);
int j = N;
LL ans = 0;
for(int i = k; i >= 1; --i){
while(j && que[j].pos > i){
// cout << que[j].pos << ' ' << que[j].mn << ' ' << que[j].num << endl;
int flip = fen.get((int)srt.size()) - fen.get(que[j].mn - 1);
if(a[que[j].num] >= b[que[j].num]){
if(flip % 2){
ans += srt[b[que[j].num] - 1];
}
else{
ans += srt[a[que[j].num] - 1];
}
}
else{
if(flip % 2){
ans += srt[a[que[j].num] - 1];
}
else{
ans += srt[b[que[j].num] - 1];
}
}
--j;
// cout << ans << endl;
}
fen.push(t[i], 1);
}
while(j){
// cout << que[j].pos << ' ' << que[j].mn << ' ' << que[j].num << endl;
int flip = fen.get((int)srt.size()) - fen.get(que[j].mn - 1);
if(a[que[j].num] >= b[que[j].num]){
if(flip % 2){
ans += srt[b[que[j].num] - 1];
}
else{
ans += srt[a[que[j].num] - 1];
}
}
else{
if((flip % 2 && que[j].pos > 1) || (flip % 2 == 0 && que[j].pos == 1)){
ans += srt[a[que[j].num] - 1];
}
else{
ans += srt[b[que[j].num] - 1];
}
}
--j;
// cout << ans << endl;
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2796 KB |
Output is correct |
2 |
Correct |
3 ms |
2796 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
4 ms |
2924 KB |
Output is correct |
5 |
Correct |
4 ms |
2924 KB |
Output is correct |
6 |
Correct |
4 ms |
2924 KB |
Output is correct |
7 |
Correct |
4 ms |
2924 KB |
Output is correct |
8 |
Correct |
4 ms |
2924 KB |
Output is correct |
9 |
Correct |
4 ms |
2796 KB |
Output is correct |
10 |
Correct |
3 ms |
2796 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
3 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2796 KB |
Output is correct |
2 |
Correct |
3 ms |
2796 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
4 ms |
2924 KB |
Output is correct |
5 |
Correct |
4 ms |
2924 KB |
Output is correct |
6 |
Correct |
4 ms |
2924 KB |
Output is correct |
7 |
Correct |
4 ms |
2924 KB |
Output is correct |
8 |
Correct |
4 ms |
2924 KB |
Output is correct |
9 |
Correct |
4 ms |
2796 KB |
Output is correct |
10 |
Correct |
3 ms |
2796 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
3 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2924 KB |
Output is correct |
14 |
Correct |
23 ms |
3948 KB |
Output is correct |
15 |
Correct |
45 ms |
4916 KB |
Output is correct |
16 |
Correct |
69 ms |
6240 KB |
Output is correct |
17 |
Correct |
93 ms |
7012 KB |
Output is correct |
18 |
Correct |
95 ms |
7012 KB |
Output is correct |
19 |
Correct |
92 ms |
7012 KB |
Output is correct |
20 |
Correct |
92 ms |
7012 KB |
Output is correct |
21 |
Correct |
93 ms |
6948 KB |
Output is correct |
22 |
Correct |
65 ms |
6368 KB |
Output is correct |
23 |
Correct |
59 ms |
5732 KB |
Output is correct |
24 |
Correct |
55 ms |
5604 KB |
Output is correct |
25 |
Correct |
73 ms |
6372 KB |
Output is correct |
26 |
Correct |
65 ms |
6500 KB |
Output is correct |
27 |
Correct |
76 ms |
6816 KB |
Output is correct |
28 |
Correct |
70 ms |
6756 KB |
Output is correct |
29 |
Correct |
80 ms |
6884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2796 KB |
Output is correct |
2 |
Correct |
3 ms |
2796 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
4 ms |
2924 KB |
Output is correct |
5 |
Correct |
4 ms |
2924 KB |
Output is correct |
6 |
Correct |
4 ms |
2924 KB |
Output is correct |
7 |
Correct |
4 ms |
2924 KB |
Output is correct |
8 |
Correct |
4 ms |
2924 KB |
Output is correct |
9 |
Correct |
4 ms |
2796 KB |
Output is correct |
10 |
Correct |
3 ms |
2796 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
3 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2924 KB |
Output is correct |
14 |
Correct |
23 ms |
3948 KB |
Output is correct |
15 |
Correct |
45 ms |
4916 KB |
Output is correct |
16 |
Correct |
69 ms |
6240 KB |
Output is correct |
17 |
Correct |
93 ms |
7012 KB |
Output is correct |
18 |
Correct |
95 ms |
7012 KB |
Output is correct |
19 |
Correct |
92 ms |
7012 KB |
Output is correct |
20 |
Correct |
92 ms |
7012 KB |
Output is correct |
21 |
Correct |
93 ms |
6948 KB |
Output is correct |
22 |
Correct |
65 ms |
6368 KB |
Output is correct |
23 |
Correct |
59 ms |
5732 KB |
Output is correct |
24 |
Correct |
55 ms |
5604 KB |
Output is correct |
25 |
Correct |
73 ms |
6372 KB |
Output is correct |
26 |
Correct |
65 ms |
6500 KB |
Output is correct |
27 |
Correct |
76 ms |
6816 KB |
Output is correct |
28 |
Correct |
70 ms |
6756 KB |
Output is correct |
29 |
Correct |
80 ms |
6884 KB |
Output is correct |
30 |
Correct |
155 ms |
9824 KB |
Output is correct |
31 |
Correct |
243 ms |
14044 KB |
Output is correct |
32 |
Correct |
327 ms |
16860 KB |
Output is correct |
33 |
Correct |
551 ms |
26328 KB |
Output is correct |
34 |
Correct |
130 ms |
9184 KB |
Output is correct |
35 |
Correct |
554 ms |
26324 KB |
Output is correct |
36 |
Correct |
556 ms |
26324 KB |
Output is correct |
37 |
Correct |
558 ms |
26324 KB |
Output is correct |
38 |
Correct |
549 ms |
26328 KB |
Output is correct |
39 |
Correct |
547 ms |
26324 KB |
Output is correct |
40 |
Correct |
536 ms |
26196 KB |
Output is correct |
41 |
Correct |
555 ms |
26304 KB |
Output is correct |
42 |
Correct |
543 ms |
26372 KB |
Output is correct |
43 |
Correct |
428 ms |
24404 KB |
Output is correct |
44 |
Correct |
417 ms |
24660 KB |
Output is correct |
45 |
Correct |
428 ms |
25116 KB |
Output is correct |
46 |
Correct |
331 ms |
19156 KB |
Output is correct |
47 |
Correct |
274 ms |
16616 KB |
Output is correct |
48 |
Correct |
409 ms |
21464 KB |
Output is correct |
49 |
Correct |
388 ms |
21588 KB |
Output is correct |