#include<bits/stdc++.h>
using namespace std;
const int MX = 200005;
const int uMX = 600006;
int n, k;
int a[MX], b[MX];
int ops[MX];
map<int,int>HASH, iHASH;
vector<int>mst[MX*4], imst(uMX*4);
//mst ->index to value
//imst ->value to index
//HASH ->HASH value of some bigger value
//iHASH ->index of some HASH value
vector<int>merge(vector<int>A, vector<int>B) {
vector<int>ret;
int p = 0, q = 0;
int N = A.size(), M = B.size();
while(p<N && q<M) {
if(A[p]<B[q]) {
ret.push_back(A[p++]);
} else {
ret.push_back(B[q++]);
}
}
while(p<N) ret.push_back(A[p++]);
while(q<M) ret.push_back(B[q++]);
return ret;
}
void Build(int l, int r, int v) { //mst
if(l==r) {
mst[v].push_back(HASH[ops[l]]);
return;
}
int m = (l+r)/2;
Build(l, m, v*2);
Build(m+1, r, v*2+1);
mst[v] = merge(mst[v*2], mst[v*2+1]);
}
void Build2(int l, int r, int v) { //imst
if(l==r) {
if(iHASH.count(l)) {
imst[v] = iHASH[l];
} else {
imst[v] = -1;
}
return;
}
int m = (l+r)/2;
Build2(l, m, v*2);
Build2(m+1, r, v*2+1);
imst[v] = max(imst[v*2], imst[v*2+1]);
}
int q2(int l, int r, int L, int R, int v) {
if(l>=L && r<=R) {
return imst[v];
} else if(l>R || r<L) return -1;
int m = (l+r)/2;
return max(q2(l, m, L, R, v*2), q2(m+1, r, L, R, v*2+1));
}
int q(int l, int r, int L, int R, int lb, int v) {
if(l>=L && r<=R) {
return mst[v].end()-lower_bound(mst[v].begin(), mst[v].end(), lb);
}
if(l>R || r<L) return 0;
int m = (l+r)/2;
return q(l, m, L, R, lb, v*2) + q(m+1, r, L, R, lb, v*2+1);
}
void PlayGround() {
cin >> n >> k;
for(int i=1; i<=n; ++i) {
cin >> a[i] >> b[i];
HASH[a[i]] = HASH[b[i]] = 0;
}
for(int i=1; i<=k; ++i) {
cin >> ops[i];
HASH[ops[i]] = i;
}
int tag = 1;
for(auto &p : HASH) {
if(p.second) iHASH[tag] = p.second;
p.second = tag++;
} //end of HASH stuffs
Build(1, k, 1);
Build2(1, uMX-1, 1);
//final processing
long long ans = 0;
for(int i=1; i<=n; ++i) {
if(a[i]==b[i]) {
ans += a[i];
continue;
}
int mx = a[i], mn = b[i];
if(mx<mn) swap(mx, mn);
int yellow = q2(1, uMX-1, HASH[mn], HASH[mx]-1, 1);
int l;
if(yellow==-1) l = 1;
else l = yellow+1;
int cnt = q(1, k, l, k, HASH[mx], 1);
if(yellow==-1) {
ans += (cnt&1)?b[i]:a[i];
} else {
ans += (cnt&1)?mn:mx;
}
}
cout << ans << endl;
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
28692 KB |
Output is correct |
2 |
Correct |
31 ms |
28784 KB |
Output is correct |
3 |
Correct |
34 ms |
28844 KB |
Output is correct |
4 |
Correct |
33 ms |
28748 KB |
Output is correct |
5 |
Correct |
33 ms |
28844 KB |
Output is correct |
6 |
Correct |
32 ms |
28736 KB |
Output is correct |
7 |
Correct |
32 ms |
28852 KB |
Output is correct |
8 |
Correct |
32 ms |
28872 KB |
Output is correct |
9 |
Correct |
33 ms |
28748 KB |
Output is correct |
10 |
Correct |
32 ms |
28624 KB |
Output is correct |
11 |
Correct |
38 ms |
28884 KB |
Output is correct |
12 |
Correct |
32 ms |
28748 KB |
Output is correct |
13 |
Correct |
32 ms |
28788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
28692 KB |
Output is correct |
2 |
Correct |
31 ms |
28784 KB |
Output is correct |
3 |
Correct |
34 ms |
28844 KB |
Output is correct |
4 |
Correct |
33 ms |
28748 KB |
Output is correct |
5 |
Correct |
33 ms |
28844 KB |
Output is correct |
6 |
Correct |
32 ms |
28736 KB |
Output is correct |
7 |
Correct |
32 ms |
28852 KB |
Output is correct |
8 |
Correct |
32 ms |
28872 KB |
Output is correct |
9 |
Correct |
33 ms |
28748 KB |
Output is correct |
10 |
Correct |
32 ms |
28624 KB |
Output is correct |
11 |
Correct |
38 ms |
28884 KB |
Output is correct |
12 |
Correct |
32 ms |
28748 KB |
Output is correct |
13 |
Correct |
32 ms |
28788 KB |
Output is correct |
14 |
Correct |
83 ms |
32092 KB |
Output is correct |
15 |
Correct |
145 ms |
35940 KB |
Output is correct |
16 |
Correct |
159 ms |
39060 KB |
Output is correct |
17 |
Correct |
206 ms |
43504 KB |
Output is correct |
18 |
Correct |
198 ms |
43484 KB |
Output is correct |
19 |
Correct |
221 ms |
43480 KB |
Output is correct |
20 |
Correct |
240 ms |
43500 KB |
Output is correct |
21 |
Correct |
204 ms |
43416 KB |
Output is correct |
22 |
Correct |
157 ms |
40672 KB |
Output is correct |
23 |
Correct |
152 ms |
39716 KB |
Output is correct |
24 |
Correct |
145 ms |
38884 KB |
Output is correct |
25 |
Correct |
158 ms |
41408 KB |
Output is correct |
26 |
Correct |
167 ms |
41076 KB |
Output is correct |
27 |
Correct |
180 ms |
41608 KB |
Output is correct |
28 |
Correct |
162 ms |
41596 KB |
Output is correct |
29 |
Correct |
188 ms |
42588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
28692 KB |
Output is correct |
2 |
Correct |
31 ms |
28784 KB |
Output is correct |
3 |
Correct |
34 ms |
28844 KB |
Output is correct |
4 |
Correct |
33 ms |
28748 KB |
Output is correct |
5 |
Correct |
33 ms |
28844 KB |
Output is correct |
6 |
Correct |
32 ms |
28736 KB |
Output is correct |
7 |
Correct |
32 ms |
28852 KB |
Output is correct |
8 |
Correct |
32 ms |
28872 KB |
Output is correct |
9 |
Correct |
33 ms |
28748 KB |
Output is correct |
10 |
Correct |
32 ms |
28624 KB |
Output is correct |
11 |
Correct |
38 ms |
28884 KB |
Output is correct |
12 |
Correct |
32 ms |
28748 KB |
Output is correct |
13 |
Correct |
32 ms |
28788 KB |
Output is correct |
14 |
Correct |
83 ms |
32092 KB |
Output is correct |
15 |
Correct |
145 ms |
35940 KB |
Output is correct |
16 |
Correct |
159 ms |
39060 KB |
Output is correct |
17 |
Correct |
206 ms |
43504 KB |
Output is correct |
18 |
Correct |
198 ms |
43484 KB |
Output is correct |
19 |
Correct |
221 ms |
43480 KB |
Output is correct |
20 |
Correct |
240 ms |
43500 KB |
Output is correct |
21 |
Correct |
204 ms |
43416 KB |
Output is correct |
22 |
Correct |
157 ms |
40672 KB |
Output is correct |
23 |
Correct |
152 ms |
39716 KB |
Output is correct |
24 |
Correct |
145 ms |
38884 KB |
Output is correct |
25 |
Correct |
158 ms |
41408 KB |
Output is correct |
26 |
Correct |
167 ms |
41076 KB |
Output is correct |
27 |
Correct |
180 ms |
41608 KB |
Output is correct |
28 |
Correct |
162 ms |
41596 KB |
Output is correct |
29 |
Correct |
188 ms |
42588 KB |
Output is correct |
30 |
Correct |
453 ms |
79680 KB |
Output is correct |
31 |
Correct |
672 ms |
84572 KB |
Output is correct |
32 |
Correct |
880 ms |
90548 KB |
Output is correct |
33 |
Correct |
1299 ms |
102764 KB |
Output is correct |
34 |
Correct |
427 ms |
78556 KB |
Output is correct |
35 |
Correct |
1305 ms |
102720 KB |
Output is correct |
36 |
Correct |
1390 ms |
102756 KB |
Output is correct |
37 |
Correct |
1315 ms |
102728 KB |
Output is correct |
38 |
Correct |
1300 ms |
102748 KB |
Output is correct |
39 |
Correct |
1340 ms |
102816 KB |
Output is correct |
40 |
Correct |
1286 ms |
102464 KB |
Output is correct |
41 |
Correct |
1314 ms |
102756 KB |
Output is correct |
42 |
Correct |
1334 ms |
102676 KB |
Output is correct |
43 |
Correct |
793 ms |
102040 KB |
Output is correct |
44 |
Correct |
762 ms |
102048 KB |
Output is correct |
45 |
Correct |
689 ms |
101896 KB |
Output is correct |
46 |
Correct |
758 ms |
84216 KB |
Output is correct |
47 |
Correct |
611 ms |
78472 KB |
Output is correct |
48 |
Correct |
856 ms |
93400 KB |
Output is correct |
49 |
Correct |
813 ms |
93504 KB |
Output is correct |