#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct sparse_table{
vector<int> lg;
vector<vector<int>> v;
sparse_table(vector<int> _v){
int n = _v.size();
lg.resize(n+1);
for(int i = 0; i <= n; i++) lg[i] = log2(i);
int k = 0, p = 1;
while(p < n) p*=2, k++;
k++;
v = vector<vector<int>>(n, vector<int>(k, 0));
for(int i = 0; i < n; i++) v[i][0] = _v[i];
for(int p = 1; p < k; p++){
for(int i = 0; i < n; i++){
v[i][p] = max(v[i][p], v[i][p-1]);
if(i + (1<<(p-1)) < n) v[i][p] = max(v[i][p], v[i + (1<<(p-1))][p-1]);
}
}
}
int query(int a, int b){
int p = lg[b-a+1];
return max(v[a][p], v[b-(1<<p)+1][p]);
}
};
struct BIT{
int n;
vector<int> sm;
BIT(int _n){
n = _n;
sm.resize(n);
}
void add(int in, int x){
in++;
while(in <= n) sm[in-1]+=x, in+=in&-in;
}
int sum(int in){
in++;
int s = 0;
while(in >= 1) s+=sm[in-1], in-=in&-in;
return s;
}
int sum(int l, int r){
return sum(r)-(l == 0? 0:sum(l-1));
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
vector<ll> A(n), B(n);
vector<pair<ll, int>> T(k);
for(int i = 0; i < n; i++) cin >> A[i] >> B[i];
for(int i = 0; i < k; i++) cin >> T[i].f, T[i].sc = i;
sort(T.begin(), T.end());
vector<int> _T;
for(int i = 0; i < k; i++) _T.pb(T[i].sc);
sparse_table sp(_T);
ll ans = 0;
vector<vector<pair<int, int>>> sth(k);
for(int i = 0; i < n; i++){
auto it1 = lower_bound(T.begin(), T.end(), make_pair(min(A[i], B[i]), -1));
auto it2 = lower_bound(T.begin(), T.end(), make_pair(max(A[i], B[i]), -1));
if(it1 == it2){
ans+=(T.end()-it2)%2?B[i]:A[i];
continue;
}
if(it2 == T.end()){
ans+=max(A[i], B[i]);
continue;
}
sth[it2-T.begin()].pb({i, sp.query(it1-T.begin(), it2-T.begin()-1)});
}
BIT bit(k);
for(int i = k-1; i >= 0; i--){
bit.add(T[i].sc, 1);
for(auto [in, x]: sth[i]) ans+=bit.sum(x, k-1)%2?min(A[in], B[in]):max(A[in], B[in]);
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
520 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
520 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
10 ms |
2260 KB |
Output is correct |
15 |
Correct |
20 ms |
4128 KB |
Output is correct |
16 |
Correct |
30 ms |
6684 KB |
Output is correct |
17 |
Correct |
40 ms |
8920 KB |
Output is correct |
18 |
Correct |
38 ms |
8908 KB |
Output is correct |
19 |
Correct |
40 ms |
8796 KB |
Output is correct |
20 |
Correct |
44 ms |
8960 KB |
Output is correct |
21 |
Correct |
35 ms |
8624 KB |
Output is correct |
22 |
Correct |
27 ms |
8376 KB |
Output is correct |
23 |
Correct |
27 ms |
8448 KB |
Output is correct |
24 |
Correct |
29 ms |
8512 KB |
Output is correct |
25 |
Correct |
26 ms |
8332 KB |
Output is correct |
26 |
Correct |
37 ms |
8480 KB |
Output is correct |
27 |
Correct |
35 ms |
8796 KB |
Output is correct |
28 |
Correct |
34 ms |
8860 KB |
Output is correct |
29 |
Correct |
33 ms |
8472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
520 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
10 ms |
2260 KB |
Output is correct |
15 |
Correct |
20 ms |
4128 KB |
Output is correct |
16 |
Correct |
30 ms |
6684 KB |
Output is correct |
17 |
Correct |
40 ms |
8920 KB |
Output is correct |
18 |
Correct |
38 ms |
8908 KB |
Output is correct |
19 |
Correct |
40 ms |
8796 KB |
Output is correct |
20 |
Correct |
44 ms |
8960 KB |
Output is correct |
21 |
Correct |
35 ms |
8624 KB |
Output is correct |
22 |
Correct |
27 ms |
8376 KB |
Output is correct |
23 |
Correct |
27 ms |
8448 KB |
Output is correct |
24 |
Correct |
29 ms |
8512 KB |
Output is correct |
25 |
Correct |
26 ms |
8332 KB |
Output is correct |
26 |
Correct |
37 ms |
8480 KB |
Output is correct |
27 |
Correct |
35 ms |
8796 KB |
Output is correct |
28 |
Correct |
34 ms |
8860 KB |
Output is correct |
29 |
Correct |
33 ms |
8472 KB |
Output is correct |
30 |
Correct |
114 ms |
36656 KB |
Output is correct |
31 |
Correct |
166 ms |
38864 KB |
Output is correct |
32 |
Correct |
195 ms |
41472 KB |
Output is correct |
33 |
Correct |
330 ms |
46496 KB |
Output is correct |
34 |
Correct |
104 ms |
35800 KB |
Output is correct |
35 |
Correct |
305 ms |
46576 KB |
Output is correct |
36 |
Correct |
328 ms |
46408 KB |
Output is correct |
37 |
Correct |
301 ms |
46596 KB |
Output is correct |
38 |
Correct |
299 ms |
46256 KB |
Output is correct |
39 |
Correct |
295 ms |
46648 KB |
Output is correct |
40 |
Correct |
223 ms |
45212 KB |
Output is correct |
41 |
Correct |
272 ms |
46660 KB |
Output is correct |
42 |
Correct |
307 ms |
46668 KB |
Output is correct |
43 |
Correct |
185 ms |
43820 KB |
Output is correct |
44 |
Correct |
192 ms |
44300 KB |
Output is correct |
45 |
Correct |
157 ms |
44096 KB |
Output is correct |
46 |
Correct |
177 ms |
44728 KB |
Output is correct |
47 |
Correct |
200 ms |
45084 KB |
Output is correct |
48 |
Correct |
256 ms |
46156 KB |
Output is correct |
49 |
Correct |
286 ms |
46208 KB |
Output is correct |