#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define FOR(i,a,b) for (int i=(a); i<=(b); ++i)
#define FORD(i,a,b) for (int i=(a); i>=(b); --i)
#define pb push_back
#define pf push_front
#define mp make_pair
#define TWO(x) (1<<(x))
#define BIT(s,i) (((s)&TWO(i))>0)
#define ON(s,i) (s |= TWO(i))
#define OFF(s,i) (s &= ~TWO(i))
#define FLIP(s,i) (s ^= TWO(i))
#define vvec vector<vector<int>>
struct ngocon{
int val,lazy;
};
struct point{
int x,y;
};
typedef pair<int,int> pii;
typedef pair<pii,int> piii;
typedef pair<int,pii> ipii;
typedef pair<pii,pii> pipi;
const int h4[4] = {1,0,-1,0}; /// 4 huong
const int c4[4] = {0,1,0,-1}; /// 4 huong
const int h8[8] = {-1,-1,-1,0,1,1,1,0}; /// 8 huong
const int c8[8] = {-1,0,1,1,1,0,-1,-1}; /// 8 huong
const int cv1[8] = {1,2,2,1,-1,-2,-2,-1}; /// 8 huong quan ma
const int cv2[8] = {-2,-1,1,2,2,1,-1,-2}; /// 8 huong quan ma
const int mod = 1e9 + 7; /// chia du
const int base = 26; /// ma hoa
const int oo = 1e16;
const int N = 2e5 + 2;
int n,k,result;
int a[N],b[N],v[N],f[24*N],d[N],bit[6*N],ans[N];
bool dd[N];
vector <int> nor,vec[N];
unordered_map <int,int> Map;
void compress(){
FOR(i,1,n){
nor.pb(a[i]);
nor.pb(b[i]);
}
FOR(i,1,k) nor.pb(v[i]);
sort(nor.begin(),nor.end());
//cout << "size: " << nor.size() << ' ' << 2*n + k << '\n';
int pos;
FOR(i,1,n){
pos = lower_bound(nor.begin(),nor.end(),a[i]) - nor.begin() + 1;
Map[pos] = a[i]; a[i] = pos;
pos = lower_bound(nor.begin(),nor.end(),b[i]) - nor.begin() + 1;
Map[pos] = b[i]; b[i] = pos;
}
FOR(i,1,k) v[i] = lower_bound(nor.begin(),nor.end(),v[i]) - nor.begin() + 1;
}
void update(int i, int l, int r, int u, int v){
if (r < u || u < l) return;
if (l == r){
f[i] = max(f[i],v);
return;
}
int mid = (l + r) >> 1;
update(i*2,l,mid,u,v);
update(i*2+1,mid+1,r,u,v);
f[i] = max(f[i*2],f[i*2+1]);
}
int query(int i, int l, int r, int u, int v){
if (r < u || v < l || u > v) return 0;
if (u <= l && r <= v) return f[i];
int mid = (l + r) >> 1;
return max(query(i*2,l,mid,u,v),query(i*2+1,mid+1,r,u,v));
}
void update_bit(int i, int v){
while (i <= 2*n + k){
bit[i] += v;
i += (i & (-i));
}
}
int query_bit(int i){
int res = 0;
while (i > 0){
res += bit[i];
i -= (i & (-i));
}
return res;
}
//int a1[N],b1[N],ans1[N],ans2[N],result1;
//
//void sub1(){
// FOR(i,1,n) a1[i] = a[i], b1[i] = b[i];
//
// FOR(i,1,k){
// FOR(j,1,n) if (a[j] <= v[i]) swap(a[j],b[j]);
// }
//
// FOR(i,1,n){
// ans1[i] = a[i];
// result1 += a[i];
// }
//}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
cin >> n >> k;
FOR(i,1,n) cin >> a[i] >> b[i];
FOR(i,1,k) cin >> v[i];
// sub1();
compress();
// FOR(i,1,n) cout << a[i] << ' ' << b[i] << '\n';
// FOR(i,1,k) cout << v[i] << ' '; cout << '\n';
FOR(i,1,k){
update(1,1,2*n+k,v[i],i);
update_bit(v[i],1);
}
FOR(i,1,n){
// cout << i << ": " << min(a[i],b[i]) << ' ' << max(a[i],b[i]) - 1 << '\n';
d[i] = query(1,1,2*n + k,min(a[i],b[i]),max(a[i],b[i]) - 1);
if (d[i] != 0) dd[i] = 1;
vec[d[i]+1].pb(i);
}
// cout << "d: "; FOR(i,1,n) cout << d[i] << ' '; cout << '\n';
FOR(i,1,k){
for (auto j : vec[i]) ans[j] = k - i + 1 - query_bit(max(b[j],a[j]) - 1);
update_bit(v[i],-1);
}
// cout << "ans: "; FOR(i,1,n) cout << ans[i] << ' '; cout << '\n';
FOR(i,1,n){
a[i] = Map[a[i]];
b[i] = Map[b[i]];
}
FOR(i,1,n){
if (!dd[i]){
if (ans[i]%2 == 0) result += a[i];
else result += b[i];
}
else{
if (a[i] >= b[i]){
if (ans[i]%2 == 0) result += a[i];
else result += b[i];
}
else{
if (ans[i]%2 == 0) result += b[i];
else result += a[i];
}
}
}
// FOR(i,1,n){
// if (ans1[i] != ans2[i]) cout << i << ": " << ans1[i] << ' ' << ans2[i] << '\n';
// }
cout << result;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
17244 KB |
Output is correct |
4 |
Correct |
4 ms |
17244 KB |
Output is correct |
5 |
Correct |
3 ms |
17244 KB |
Output is correct |
6 |
Correct |
4 ms |
17244 KB |
Output is correct |
7 |
Correct |
4 ms |
17040 KB |
Output is correct |
8 |
Correct |
4 ms |
17244 KB |
Output is correct |
9 |
Correct |
3 ms |
17244 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
17040 KB |
Output is correct |
13 |
Correct |
4 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
17244 KB |
Output is correct |
4 |
Correct |
4 ms |
17244 KB |
Output is correct |
5 |
Correct |
3 ms |
17244 KB |
Output is correct |
6 |
Correct |
4 ms |
17244 KB |
Output is correct |
7 |
Correct |
4 ms |
17040 KB |
Output is correct |
8 |
Correct |
4 ms |
17244 KB |
Output is correct |
9 |
Correct |
3 ms |
17244 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
17040 KB |
Output is correct |
13 |
Correct |
4 ms |
17244 KB |
Output is correct |
14 |
Correct |
16 ms |
18912 KB |
Output is correct |
15 |
Correct |
31 ms |
20816 KB |
Output is correct |
16 |
Correct |
47 ms |
23528 KB |
Output is correct |
17 |
Correct |
68 ms |
25796 KB |
Output is correct |
18 |
Correct |
64 ms |
25548 KB |
Output is correct |
19 |
Correct |
63 ms |
25552 KB |
Output is correct |
20 |
Correct |
63 ms |
25548 KB |
Output is correct |
21 |
Correct |
63 ms |
25768 KB |
Output is correct |
22 |
Correct |
48 ms |
24268 KB |
Output is correct |
23 |
Correct |
47 ms |
24548 KB |
Output is correct |
24 |
Correct |
47 ms |
24020 KB |
Output is correct |
25 |
Correct |
53 ms |
24700 KB |
Output is correct |
26 |
Correct |
48 ms |
22136 KB |
Output is correct |
27 |
Correct |
57 ms |
24016 KB |
Output is correct |
28 |
Correct |
48 ms |
23848 KB |
Output is correct |
29 |
Correct |
58 ms |
25024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
17244 KB |
Output is correct |
4 |
Correct |
4 ms |
17244 KB |
Output is correct |
5 |
Correct |
3 ms |
17244 KB |
Output is correct |
6 |
Correct |
4 ms |
17244 KB |
Output is correct |
7 |
Correct |
4 ms |
17040 KB |
Output is correct |
8 |
Correct |
4 ms |
17244 KB |
Output is correct |
9 |
Correct |
3 ms |
17244 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
17040 KB |
Output is correct |
13 |
Correct |
4 ms |
17244 KB |
Output is correct |
14 |
Correct |
16 ms |
18912 KB |
Output is correct |
15 |
Correct |
31 ms |
20816 KB |
Output is correct |
16 |
Correct |
47 ms |
23528 KB |
Output is correct |
17 |
Correct |
68 ms |
25796 KB |
Output is correct |
18 |
Correct |
64 ms |
25548 KB |
Output is correct |
19 |
Correct |
63 ms |
25552 KB |
Output is correct |
20 |
Correct |
63 ms |
25548 KB |
Output is correct |
21 |
Correct |
63 ms |
25768 KB |
Output is correct |
22 |
Correct |
48 ms |
24268 KB |
Output is correct |
23 |
Correct |
47 ms |
24548 KB |
Output is correct |
24 |
Correct |
47 ms |
24020 KB |
Output is correct |
25 |
Correct |
53 ms |
24700 KB |
Output is correct |
26 |
Correct |
48 ms |
22136 KB |
Output is correct |
27 |
Correct |
57 ms |
24016 KB |
Output is correct |
28 |
Correct |
48 ms |
23848 KB |
Output is correct |
29 |
Correct |
58 ms |
25024 KB |
Output is correct |
30 |
Correct |
113 ms |
26616 KB |
Output is correct |
31 |
Correct |
170 ms |
39396 KB |
Output is correct |
32 |
Correct |
284 ms |
46100 KB |
Output is correct |
33 |
Correct |
444 ms |
70760 KB |
Output is correct |
34 |
Correct |
101 ms |
25612 KB |
Output is correct |
35 |
Correct |
461 ms |
70324 KB |
Output is correct |
36 |
Correct |
464 ms |
71264 KB |
Output is correct |
37 |
Correct |
446 ms |
69768 KB |
Output is correct |
38 |
Correct |
402 ms |
71600 KB |
Output is correct |
39 |
Correct |
419 ms |
70224 KB |
Output is correct |
40 |
Correct |
439 ms |
72940 KB |
Output is correct |
41 |
Correct |
409 ms |
70332 KB |
Output is correct |
42 |
Correct |
457 ms |
70584 KB |
Output is correct |
43 |
Correct |
245 ms |
65208 KB |
Output is correct |
44 |
Correct |
248 ms |
66664 KB |
Output is correct |
45 |
Correct |
249 ms |
64980 KB |
Output is correct |
46 |
Correct |
267 ms |
61712 KB |
Output is correct |
47 |
Correct |
277 ms |
60024 KB |
Output is correct |
48 |
Correct |
306 ms |
58556 KB |
Output is correct |
49 |
Correct |
289 ms |
59088 KB |
Output is correct |