#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define wall cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2e5 + 10;
int n , k , ar[maxn];
pair<int , int> all[maxn];
bool last[maxn];
struct nod{
vector <int> all;
} tree[(maxn << 2)];
void Build(int node = 1 , int nl = 0 , int nr = k)
{
if(nr - nl == 1)
{
tree[node].all.pb(ar[nl]);
return;
}
int lc = node << 1 , rc = lc | 1 , mid = (nl + nr) >> 1;
Build(lc , nl , mid); Build(rc , mid , nr);
tree[node].all = tree[lc].all;
for(auto u : tree[rc].all) tree[node].all.pb(u);
sort(tree[node].all.begin() , tree[node].all.end());
}
bool check(int node , int l , int r)
{
if(tree[node].all.back() < l) return false;
int ps = lower_bound(tree[node].all.begin() , tree[node].all.end() , l) - tree[node].all.begin();
return tree[node].all[ps] < r;
}
int Find_ps(int l , int r , int node = 1 , int nl = 0 , int nr = k)
{
if(nr - nl == 1) return nl;
int lc = node << 1 , rc = lc | 1 , mid = (nl + nr) >> 1;
if(check(rc , l , r)) return Find_ps(l , r , rc , mid , nr);
else return Find_ps(l , r , lc , nl , mid);
}
int Get(int ind , int val , int node = 1 , int nl = 0 , int nr = k)
{
if(nr <= ind) return 0;
if(nl >= ind)
{
int ps = lower_bound(tree[node].all.begin() , tree[node].all.end() , val) - tree[node].all.begin();
return SZ(tree[node].all) - ps;
}
int lc = node << 1 , rc = lc | 1 , mid = (nl + nr) >> 1;
return Get(ind , val , lc , nl , mid) + Get(ind , val , rc , mid , nr);
}
int32_t main()
{
fast;
cin >> n >> k;
for(int i = 0 ; i < n ; i++) cin >> all[i].F >> all[i].S;
for(int i = 0 ; i < k ; i++) cin >> ar[i];
Build();
ll sum = 0;
for(int i = 0 ; i < n ; i++)
{
int l;
if(!check(1 , min(all[i].F , all[i].S) , max(all[i].F , all[i].S))) l = -1;
else l = Find_ps(min(all[i].F , all[i].S) , max(all[i].F , all[i].S));
int cnt = Get(l + 1 , all[i].S);
if(l != -1 && all[i].F < all[i].S) swap(all[i].F , all[i].S);
if(cnt & 1) swap(all[i].F , all[i].S);
sum += all[i].F;
}
cout << sum << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19156 KB |
Output is correct |
2 |
Correct |
11 ms |
19220 KB |
Output is correct |
3 |
Correct |
12 ms |
19284 KB |
Output is correct |
4 |
Correct |
11 ms |
19208 KB |
Output is correct |
5 |
Correct |
11 ms |
19272 KB |
Output is correct |
6 |
Correct |
11 ms |
19200 KB |
Output is correct |
7 |
Correct |
11 ms |
19264 KB |
Output is correct |
8 |
Correct |
10 ms |
19188 KB |
Output is correct |
9 |
Correct |
11 ms |
19180 KB |
Output is correct |
10 |
Correct |
12 ms |
19156 KB |
Output is correct |
11 |
Correct |
12 ms |
19284 KB |
Output is correct |
12 |
Correct |
12 ms |
19232 KB |
Output is correct |
13 |
Correct |
13 ms |
19284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19156 KB |
Output is correct |
2 |
Correct |
11 ms |
19220 KB |
Output is correct |
3 |
Correct |
12 ms |
19284 KB |
Output is correct |
4 |
Correct |
11 ms |
19208 KB |
Output is correct |
5 |
Correct |
11 ms |
19272 KB |
Output is correct |
6 |
Correct |
11 ms |
19200 KB |
Output is correct |
7 |
Correct |
11 ms |
19264 KB |
Output is correct |
8 |
Correct |
10 ms |
19188 KB |
Output is correct |
9 |
Correct |
11 ms |
19180 KB |
Output is correct |
10 |
Correct |
12 ms |
19156 KB |
Output is correct |
11 |
Correct |
12 ms |
19284 KB |
Output is correct |
12 |
Correct |
12 ms |
19232 KB |
Output is correct |
13 |
Correct |
13 ms |
19284 KB |
Output is correct |
14 |
Correct |
25 ms |
20692 KB |
Output is correct |
15 |
Correct |
49 ms |
22348 KB |
Output is correct |
16 |
Correct |
63 ms |
24248 KB |
Output is correct |
17 |
Correct |
92 ms |
26060 KB |
Output is correct |
18 |
Correct |
93 ms |
25940 KB |
Output is correct |
19 |
Correct |
87 ms |
25952 KB |
Output is correct |
20 |
Correct |
92 ms |
25892 KB |
Output is correct |
21 |
Correct |
72 ms |
25808 KB |
Output is correct |
22 |
Correct |
61 ms |
25404 KB |
Output is correct |
23 |
Correct |
59 ms |
25400 KB |
Output is correct |
24 |
Correct |
64 ms |
25420 KB |
Output is correct |
25 |
Correct |
59 ms |
25420 KB |
Output is correct |
26 |
Correct |
61 ms |
25672 KB |
Output is correct |
27 |
Correct |
87 ms |
26048 KB |
Output is correct |
28 |
Correct |
58 ms |
25904 KB |
Output is correct |
29 |
Correct |
79 ms |
25940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19156 KB |
Output is correct |
2 |
Correct |
11 ms |
19220 KB |
Output is correct |
3 |
Correct |
12 ms |
19284 KB |
Output is correct |
4 |
Correct |
11 ms |
19208 KB |
Output is correct |
5 |
Correct |
11 ms |
19272 KB |
Output is correct |
6 |
Correct |
11 ms |
19200 KB |
Output is correct |
7 |
Correct |
11 ms |
19264 KB |
Output is correct |
8 |
Correct |
10 ms |
19188 KB |
Output is correct |
9 |
Correct |
11 ms |
19180 KB |
Output is correct |
10 |
Correct |
12 ms |
19156 KB |
Output is correct |
11 |
Correct |
12 ms |
19284 KB |
Output is correct |
12 |
Correct |
12 ms |
19232 KB |
Output is correct |
13 |
Correct |
13 ms |
19284 KB |
Output is correct |
14 |
Correct |
25 ms |
20692 KB |
Output is correct |
15 |
Correct |
49 ms |
22348 KB |
Output is correct |
16 |
Correct |
63 ms |
24248 KB |
Output is correct |
17 |
Correct |
92 ms |
26060 KB |
Output is correct |
18 |
Correct |
93 ms |
25940 KB |
Output is correct |
19 |
Correct |
87 ms |
25952 KB |
Output is correct |
20 |
Correct |
92 ms |
25892 KB |
Output is correct |
21 |
Correct |
72 ms |
25808 KB |
Output is correct |
22 |
Correct |
61 ms |
25404 KB |
Output is correct |
23 |
Correct |
59 ms |
25400 KB |
Output is correct |
24 |
Correct |
64 ms |
25420 KB |
Output is correct |
25 |
Correct |
59 ms |
25420 KB |
Output is correct |
26 |
Correct |
61 ms |
25672 KB |
Output is correct |
27 |
Correct |
87 ms |
26048 KB |
Output is correct |
28 |
Correct |
58 ms |
25904 KB |
Output is correct |
29 |
Correct |
79 ms |
25940 KB |
Output is correct |
30 |
Correct |
229 ms |
49728 KB |
Output is correct |
31 |
Correct |
286 ms |
50808 KB |
Output is correct |
32 |
Correct |
355 ms |
52304 KB |
Output is correct |
33 |
Correct |
491 ms |
55096 KB |
Output is correct |
34 |
Correct |
211 ms |
49472 KB |
Output is correct |
35 |
Correct |
495 ms |
55024 KB |
Output is correct |
36 |
Correct |
489 ms |
54892 KB |
Output is correct |
37 |
Correct |
507 ms |
55024 KB |
Output is correct |
38 |
Correct |
490 ms |
54920 KB |
Output is correct |
39 |
Correct |
517 ms |
55000 KB |
Output is correct |
40 |
Correct |
409 ms |
54780 KB |
Output is correct |
41 |
Correct |
501 ms |
54916 KB |
Output is correct |
42 |
Correct |
527 ms |
54992 KB |
Output is correct |
43 |
Correct |
293 ms |
54280 KB |
Output is correct |
44 |
Correct |
294 ms |
54556 KB |
Output is correct |
45 |
Correct |
295 ms |
54148 KB |
Output is correct |
46 |
Correct |
337 ms |
53112 KB |
Output is correct |
47 |
Correct |
358 ms |
52964 KB |
Output is correct |
48 |
Correct |
352 ms |
55040 KB |
Output is correct |
49 |
Correct |
313 ms |
55056 KB |
Output is correct |