#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 |
7 ms |
5196 KB |
Output is correct |
2 |
Correct |
5 ms |
5324 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
5 ms |
5424 KB |
Output is correct |
5 |
Correct |
5 ms |
5324 KB |
Output is correct |
6 |
Correct |
5 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
14 ms |
5424 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
8 ms |
5196 KB |
Output is correct |
11 |
Correct |
5 ms |
5324 KB |
Output is correct |
12 |
Correct |
5 ms |
5376 KB |
Output is correct |
13 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5196 KB |
Output is correct |
2 |
Correct |
5 ms |
5324 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
5 ms |
5424 KB |
Output is correct |
5 |
Correct |
5 ms |
5324 KB |
Output is correct |
6 |
Correct |
5 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
14 ms |
5424 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
8 ms |
5196 KB |
Output is correct |
11 |
Correct |
5 ms |
5324 KB |
Output is correct |
12 |
Correct |
5 ms |
5376 KB |
Output is correct |
13 |
Correct |
5 ms |
5324 KB |
Output is correct |
14 |
Correct |
26 ms |
7936 KB |
Output is correct |
15 |
Correct |
48 ms |
10708 KB |
Output is correct |
16 |
Correct |
74 ms |
13564 KB |
Output is correct |
17 |
Correct |
102 ms |
16476 KB |
Output is correct |
18 |
Correct |
102 ms |
16472 KB |
Output is correct |
19 |
Correct |
105 ms |
16512 KB |
Output is correct |
20 |
Correct |
149 ms |
16572 KB |
Output is correct |
21 |
Correct |
100 ms |
16536 KB |
Output is correct |
22 |
Correct |
115 ms |
14012 KB |
Output is correct |
23 |
Correct |
73 ms |
13020 KB |
Output is correct |
24 |
Correct |
69 ms |
12240 KB |
Output is correct |
25 |
Correct |
80 ms |
14636 KB |
Output is correct |
26 |
Correct |
75 ms |
13928 KB |
Output is correct |
27 |
Correct |
84 ms |
14572 KB |
Output is correct |
28 |
Correct |
77 ms |
14520 KB |
Output is correct |
29 |
Correct |
101 ms |
15656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5196 KB |
Output is correct |
2 |
Correct |
5 ms |
5324 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
5 ms |
5424 KB |
Output is correct |
5 |
Correct |
5 ms |
5324 KB |
Output is correct |
6 |
Correct |
5 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
14 ms |
5424 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
8 ms |
5196 KB |
Output is correct |
11 |
Correct |
5 ms |
5324 KB |
Output is correct |
12 |
Correct |
5 ms |
5376 KB |
Output is correct |
13 |
Correct |
5 ms |
5324 KB |
Output is correct |
14 |
Correct |
26 ms |
7936 KB |
Output is correct |
15 |
Correct |
48 ms |
10708 KB |
Output is correct |
16 |
Correct |
74 ms |
13564 KB |
Output is correct |
17 |
Correct |
102 ms |
16476 KB |
Output is correct |
18 |
Correct |
102 ms |
16472 KB |
Output is correct |
19 |
Correct |
105 ms |
16512 KB |
Output is correct |
20 |
Correct |
149 ms |
16572 KB |
Output is correct |
21 |
Correct |
100 ms |
16536 KB |
Output is correct |
22 |
Correct |
115 ms |
14012 KB |
Output is correct |
23 |
Correct |
73 ms |
13020 KB |
Output is correct |
24 |
Correct |
69 ms |
12240 KB |
Output is correct |
25 |
Correct |
80 ms |
14636 KB |
Output is correct |
26 |
Correct |
75 ms |
13928 KB |
Output is correct |
27 |
Correct |
84 ms |
14572 KB |
Output is correct |
28 |
Correct |
77 ms |
14520 KB |
Output is correct |
29 |
Correct |
101 ms |
15656 KB |
Output is correct |
30 |
Correct |
290 ms |
35620 KB |
Output is correct |
31 |
Correct |
401 ms |
41652 KB |
Output is correct |
32 |
Correct |
484 ms |
49012 KB |
Output is correct |
33 |
Correct |
740 ms |
63900 KB |
Output is correct |
34 |
Correct |
250 ms |
34264 KB |
Output is correct |
35 |
Correct |
792 ms |
63876 KB |
Output is correct |
36 |
Correct |
723 ms |
63820 KB |
Output is correct |
37 |
Correct |
777 ms |
63844 KB |
Output is correct |
38 |
Correct |
750 ms |
63880 KB |
Output is correct |
39 |
Correct |
767 ms |
64072 KB |
Output is correct |
40 |
Correct |
574 ms |
63676 KB |
Output is correct |
41 |
Correct |
715 ms |
64000 KB |
Output is correct |
42 |
Correct |
721 ms |
63924 KB |
Output is correct |
43 |
Correct |
480 ms |
62408 KB |
Output is correct |
44 |
Correct |
530 ms |
62616 KB |
Output is correct |
45 |
Correct |
488 ms |
62864 KB |
Output is correct |
46 |
Correct |
466 ms |
46772 KB |
Output is correct |
47 |
Correct |
405 ms |
41644 KB |
Output is correct |
48 |
Correct |
534 ms |
54064 KB |
Output is correct |
49 |
Correct |
463 ms |
53800 KB |
Output is correct |