#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int a[200005];
int b[200005];
int sp[25][200005];
int g[200005];
int treba[200005];
int lg[200005];
int bit[600005];
pair <int, int> t[200005];
int k;
int nadji(int x){
/// poslednji manji jednak x
int l = 1, r = k, res = 0;
while(l <= r){
int mid = (l+r)/2;
if(t[mid].first <= x){
l = mid+1;
res = mid;
}
else r = mid-1;
}
return res;
}
int getmax(int l, int r){
int j = lg[r-l+1];
return max(sp[j][l], sp[j][r-(1<<j)+1]);
}
int bitget(int x){
int res = 0;
while(x){
res += bit[x];
x -= x & -x;
}
return res;
}
void bitadd(int x){
while(x <= 600000){
bit[x]++;
x += x & -x;
}
}
map <int, int> u;
vector <int> vec[200005];
int kolko[200005];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n >> k;
for(int i=2; i<=k; i++){
lg[i] = 1 + lg[i/2];
}
for(int i=1; i<=n; i++){
cin >> a[i] >> b[i];
}
for(int i=1; i<=k; i++){
cin >> t[i].first;
t[i].second = i;
g[i] = t[i].first;
}
sort(t+1, t+1+k);
for(int i=1; i<=k; i++){
sp[0][i] = t[i].second;
}
for(int j=1; (1<<j)<=k; j++){
for(int i=1; i+(1<<j)-1<=k; i++){
sp[j][i] = max(sp[j-1][i], sp[j-1][i+(1<<(j-1))]);
}
}
for(int i=1; i<=n; i++){
int p = min(a[i], b[i]);
int d = max(a[i], b[i]);
int x = nadji(p-1);
int y = nadji(d-1);
if(x >= y) continue;
treba[i] = getmax(x+1, y);
}
vector <int> comp;
for(int i=1; i<=n; i++){
vec[treba[i]+1].push_back(i);
}
for(int i=1; i<=n; i++){
comp.push_back(a[i]);
comp.push_back(b[i]);
}
for(int i=1; i<=k; i++){
comp.push_back(g[i]);
}
sort(comp.begin(), comp.end());
int cmpr = 0;
for(auto c : comp){
if(!u[c]) u[c] = ++cmpr;
}
for(int i=k; i>=1; i--){
bitadd(u[g[i]]);
for(auto c : vec[i]){
kolko[c] = k-i+1 - bitget(u[min(a[c], b[c])]);
}
}
ll res = 0;
for(int i=1; i<=n; i++){
//cout << i << " " << treba[i] << " " << kolko[i] << endl;
kolko[i] %= 2;
if(a[i] >= b[i] || treba[i] == 0){
if(kolko[i]) res += b[i];
else res += a[i];
}
else{
if(kolko[i]) res += a[i];
else res += b[i];
}
}
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5248 KB |
Output is correct |
2 |
Correct |
8 ms |
5376 KB |
Output is correct |
3 |
Correct |
9 ms |
5376 KB |
Output is correct |
4 |
Correct |
9 ms |
5376 KB |
Output is correct |
5 |
Correct |
9 ms |
5376 KB |
Output is correct |
6 |
Correct |
9 ms |
5376 KB |
Output is correct |
7 |
Correct |
9 ms |
5376 KB |
Output is correct |
8 |
Correct |
9 ms |
5376 KB |
Output is correct |
9 |
Correct |
9 ms |
5376 KB |
Output is correct |
10 |
Correct |
8 ms |
5376 KB |
Output is correct |
11 |
Correct |
9 ms |
5376 KB |
Output is correct |
12 |
Correct |
9 ms |
5376 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5248 KB |
Output is correct |
2 |
Correct |
8 ms |
5376 KB |
Output is correct |
3 |
Correct |
9 ms |
5376 KB |
Output is correct |
4 |
Correct |
9 ms |
5376 KB |
Output is correct |
5 |
Correct |
9 ms |
5376 KB |
Output is correct |
6 |
Correct |
9 ms |
5376 KB |
Output is correct |
7 |
Correct |
9 ms |
5376 KB |
Output is correct |
8 |
Correct |
9 ms |
5376 KB |
Output is correct |
9 |
Correct |
9 ms |
5376 KB |
Output is correct |
10 |
Correct |
8 ms |
5376 KB |
Output is correct |
11 |
Correct |
9 ms |
5376 KB |
Output is correct |
12 |
Correct |
9 ms |
5376 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
14 |
Correct |
29 ms |
7672 KB |
Output is correct |
15 |
Correct |
56 ms |
10248 KB |
Output is correct |
16 |
Correct |
83 ms |
12784 KB |
Output is correct |
17 |
Correct |
114 ms |
15468 KB |
Output is correct |
18 |
Correct |
114 ms |
15472 KB |
Output is correct |
19 |
Correct |
112 ms |
15468 KB |
Output is correct |
20 |
Correct |
118 ms |
15492 KB |
Output is correct |
21 |
Correct |
110 ms |
15408 KB |
Output is correct |
22 |
Correct |
87 ms |
13424 KB |
Output is correct |
23 |
Correct |
78 ms |
12400 KB |
Output is correct |
24 |
Correct |
75 ms |
11644 KB |
Output is correct |
25 |
Correct |
88 ms |
13936 KB |
Output is correct |
26 |
Correct |
95 ms |
13044 KB |
Output is correct |
27 |
Correct |
92 ms |
13556 KB |
Output is correct |
28 |
Correct |
84 ms |
13420 KB |
Output is correct |
29 |
Correct |
113 ms |
14580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5248 KB |
Output is correct |
2 |
Correct |
8 ms |
5376 KB |
Output is correct |
3 |
Correct |
9 ms |
5376 KB |
Output is correct |
4 |
Correct |
9 ms |
5376 KB |
Output is correct |
5 |
Correct |
9 ms |
5376 KB |
Output is correct |
6 |
Correct |
9 ms |
5376 KB |
Output is correct |
7 |
Correct |
9 ms |
5376 KB |
Output is correct |
8 |
Correct |
9 ms |
5376 KB |
Output is correct |
9 |
Correct |
9 ms |
5376 KB |
Output is correct |
10 |
Correct |
8 ms |
5376 KB |
Output is correct |
11 |
Correct |
9 ms |
5376 KB |
Output is correct |
12 |
Correct |
9 ms |
5376 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
14 |
Correct |
29 ms |
7672 KB |
Output is correct |
15 |
Correct |
56 ms |
10248 KB |
Output is correct |
16 |
Correct |
83 ms |
12784 KB |
Output is correct |
17 |
Correct |
114 ms |
15468 KB |
Output is correct |
18 |
Correct |
114 ms |
15472 KB |
Output is correct |
19 |
Correct |
112 ms |
15468 KB |
Output is correct |
20 |
Correct |
118 ms |
15492 KB |
Output is correct |
21 |
Correct |
110 ms |
15408 KB |
Output is correct |
22 |
Correct |
87 ms |
13424 KB |
Output is correct |
23 |
Correct |
78 ms |
12400 KB |
Output is correct |
24 |
Correct |
75 ms |
11644 KB |
Output is correct |
25 |
Correct |
88 ms |
13936 KB |
Output is correct |
26 |
Correct |
95 ms |
13044 KB |
Output is correct |
27 |
Correct |
92 ms |
13556 KB |
Output is correct |
28 |
Correct |
84 ms |
13420 KB |
Output is correct |
29 |
Correct |
113 ms |
14580 KB |
Output is correct |
30 |
Correct |
306 ms |
33644 KB |
Output is correct |
31 |
Correct |
402 ms |
38764 KB |
Output is correct |
32 |
Correct |
518 ms |
45204 KB |
Output is correct |
33 |
Correct |
795 ms |
64000 KB |
Output is correct |
34 |
Correct |
275 ms |
34160 KB |
Output is correct |
35 |
Correct |
831 ms |
64072 KB |
Output is correct |
36 |
Correct |
794 ms |
64036 KB |
Output is correct |
37 |
Correct |
846 ms |
63960 KB |
Output is correct |
38 |
Correct |
851 ms |
63964 KB |
Output is correct |
39 |
Correct |
803 ms |
64288 KB |
Output is correct |
40 |
Correct |
630 ms |
63644 KB |
Output is correct |
41 |
Correct |
771 ms |
63968 KB |
Output is correct |
42 |
Correct |
774 ms |
64000 KB |
Output is correct |
43 |
Correct |
558 ms |
62440 KB |
Output is correct |
44 |
Correct |
543 ms |
62688 KB |
Output is correct |
45 |
Correct |
523 ms |
62812 KB |
Output is correct |
46 |
Correct |
499 ms |
46812 KB |
Output is correct |
47 |
Correct |
469 ms |
41824 KB |
Output is correct |
48 |
Correct |
527 ms |
53924 KB |
Output is correct |
49 |
Correct |
500 ms |
53936 KB |
Output is correct |