#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll S = (1 << 21);
struct MaxSegTree{
ll T[2 * S] = {};
void update(ll k, ll v) { update(1, 1, S, k, v); }
ll query(ll l, ll r) { return query(1, 1, S, l, r); }
void update(ll node, ll s, ll e, ll k, ll v){
if (s == e){
T[node] = max(T[node], v);
return;
}
ll m = (s + e) / 2;
ll lch = 2 * node, rch = 2 * node + 1;
if (k <= m) update(lch, s, m, k, v);
else update(rch, m + 1, e, k, v);
ll x = T[lch], y = T[rch];
T[node] = max(x, y);
}
ll query(ll node, ll s, ll e, ll l, ll r){
if (e < l || s > r) return 0;
if (l <= s && e <= r) return T[node];
ll m = (s + e) / 2;
ll lch = 2 * node, rch = 2 * node + 1;
ll x = query(lch, s, m, l, r), y = query(rch, m + 1, e, l, r);
return max(x, y);
}
};
struct MergeSortTree{
vector<ll> T[2 * S];
ll query(ll l, ll r, ll k) {return query(1, 1, S / 2, l, r, k);}
void init(){
for(int i = S / 2 - 1; i >= 1; i--){
vector<ll> &L = T[i * 2], &R = T[i * 2 + 1];
T[i].resize(L.size() + R.size());
merge(L.begin(), L.end(), R.begin(), R.end(), T[i].begin());
}
}
void push(ll k, ll v){
T[k + S / 2 - 1].push_back(v);
}
ll query(ll node, ll s, ll e, ll l, ll r, ll v){
if(e < l || s > r) return 0;
if(l <= s && e <= r) return T[node].end() - lower_bound(T[node].begin(), T[node].end(), v);
ll m = (s + e) / 2, lch = node * 2, rch = node * 2 + 1;
ll x = query(lch, s, m, l, r, v);
ll y = query(rch, m + 1, e, l, r, v);
return x + y;
}
};
vector<ll> V;
map<ll, ll> mp;
MaxSegTree MT;
MergeSortTree SRT;
ll Index(ll n){
return lower_bound(V.begin(), V.end(), n) - V.begin() + 1;
}
ll N, K;
ll A[400005];
ll B[400005];
ll Q[400005];
int main(){
cin.tie(0) -> sync_with_stdio(false);
cin >> N >> K;
for(int i = 1; i <= N; i++){
cin >> A[i] >> B[i];
V.push_back(A[i]); V.push_back(B[i]);
}
for(int i = 1; i <= K; i++){
cin >> Q[i];
V.push_back(Q[i]);
}
sort(V.begin(), V.end());
V.erase(unique(V.begin(), V.end()), V.end());
for(int i = 1; i <= N; i++){
mp[Index(A[i])] = A[i];
A[i] = Index(A[i]);
mp[Index(B[i])] = B[i];
B[i] = Index(B[i]);
}
for(int i = 1; i <= K; i++){
mp[Index(Q[i])] = Q[i];
Q[i] = Index(Q[i]);
MT.update(Q[i], i);
SRT.push(i, Q[i]);
}
SRT.init();
ll ans = 0;
for(int i = 1; i <= N; i++){
ll C = min(A[i], B[i]), D = max(A[i], B[i]);
ll Last = MT.query(C, D - 1);
ll R = SRT.query(Last + 1, K, D);
if(Last == 0) C = B[i], D = A[i];
if(R % 2 == 0){
ans += mp[D];
} else {
ans += mp[C];
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
99216 KB |
Output is correct |
2 |
Correct |
56 ms |
99276 KB |
Output is correct |
3 |
Correct |
59 ms |
99468 KB |
Output is correct |
4 |
Correct |
56 ms |
99396 KB |
Output is correct |
5 |
Correct |
58 ms |
99384 KB |
Output is correct |
6 |
Correct |
56 ms |
99396 KB |
Output is correct |
7 |
Correct |
57 ms |
99400 KB |
Output is correct |
8 |
Correct |
59 ms |
99356 KB |
Output is correct |
9 |
Correct |
62 ms |
99344 KB |
Output is correct |
10 |
Correct |
57 ms |
99164 KB |
Output is correct |
11 |
Correct |
64 ms |
99412 KB |
Output is correct |
12 |
Correct |
56 ms |
99312 KB |
Output is correct |
13 |
Correct |
58 ms |
99268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
99216 KB |
Output is correct |
2 |
Correct |
56 ms |
99276 KB |
Output is correct |
3 |
Correct |
59 ms |
99468 KB |
Output is correct |
4 |
Correct |
56 ms |
99396 KB |
Output is correct |
5 |
Correct |
58 ms |
99384 KB |
Output is correct |
6 |
Correct |
56 ms |
99396 KB |
Output is correct |
7 |
Correct |
57 ms |
99400 KB |
Output is correct |
8 |
Correct |
59 ms |
99356 KB |
Output is correct |
9 |
Correct |
62 ms |
99344 KB |
Output is correct |
10 |
Correct |
57 ms |
99164 KB |
Output is correct |
11 |
Correct |
64 ms |
99412 KB |
Output is correct |
12 |
Correct |
56 ms |
99312 KB |
Output is correct |
13 |
Correct |
58 ms |
99268 KB |
Output is correct |
14 |
Correct |
85 ms |
104000 KB |
Output is correct |
15 |
Correct |
127 ms |
108736 KB |
Output is correct |
16 |
Correct |
194 ms |
113684 KB |
Output is correct |
17 |
Correct |
206 ms |
118464 KB |
Output is correct |
18 |
Correct |
202 ms |
118540 KB |
Output is correct |
19 |
Correct |
261 ms |
118552 KB |
Output is correct |
20 |
Correct |
208 ms |
118432 KB |
Output is correct |
21 |
Correct |
196 ms |
118548 KB |
Output is correct |
22 |
Correct |
151 ms |
115428 KB |
Output is correct |
23 |
Correct |
140 ms |
113856 KB |
Output is correct |
24 |
Correct |
144 ms |
112784 KB |
Output is correct |
25 |
Correct |
162 ms |
116208 KB |
Output is correct |
26 |
Correct |
152 ms |
114648 KB |
Output is correct |
27 |
Correct |
214 ms |
115472 KB |
Output is correct |
28 |
Correct |
156 ms |
115320 KB |
Output is correct |
29 |
Correct |
224 ms |
116908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
99216 KB |
Output is correct |
2 |
Correct |
56 ms |
99276 KB |
Output is correct |
3 |
Correct |
59 ms |
99468 KB |
Output is correct |
4 |
Correct |
56 ms |
99396 KB |
Output is correct |
5 |
Correct |
58 ms |
99384 KB |
Output is correct |
6 |
Correct |
56 ms |
99396 KB |
Output is correct |
7 |
Correct |
57 ms |
99400 KB |
Output is correct |
8 |
Correct |
59 ms |
99356 KB |
Output is correct |
9 |
Correct |
62 ms |
99344 KB |
Output is correct |
10 |
Correct |
57 ms |
99164 KB |
Output is correct |
11 |
Correct |
64 ms |
99412 KB |
Output is correct |
12 |
Correct |
56 ms |
99312 KB |
Output is correct |
13 |
Correct |
58 ms |
99268 KB |
Output is correct |
14 |
Correct |
85 ms |
104000 KB |
Output is correct |
15 |
Correct |
127 ms |
108736 KB |
Output is correct |
16 |
Correct |
194 ms |
113684 KB |
Output is correct |
17 |
Correct |
206 ms |
118464 KB |
Output is correct |
18 |
Correct |
202 ms |
118540 KB |
Output is correct |
19 |
Correct |
261 ms |
118552 KB |
Output is correct |
20 |
Correct |
208 ms |
118432 KB |
Output is correct |
21 |
Correct |
196 ms |
118548 KB |
Output is correct |
22 |
Correct |
151 ms |
115428 KB |
Output is correct |
23 |
Correct |
140 ms |
113856 KB |
Output is correct |
24 |
Correct |
144 ms |
112784 KB |
Output is correct |
25 |
Correct |
162 ms |
116208 KB |
Output is correct |
26 |
Correct |
152 ms |
114648 KB |
Output is correct |
27 |
Correct |
214 ms |
115472 KB |
Output is correct |
28 |
Correct |
156 ms |
115320 KB |
Output is correct |
29 |
Correct |
224 ms |
116908 KB |
Output is correct |
30 |
Correct |
431 ms |
160400 KB |
Output is correct |
31 |
Correct |
611 ms |
167964 KB |
Output is correct |
32 |
Correct |
813 ms |
177412 KB |
Output is correct |
33 |
Correct |
1349 ms |
196188 KB |
Output is correct |
34 |
Correct |
391 ms |
160300 KB |
Output is correct |
35 |
Correct |
1359 ms |
201604 KB |
Output is correct |
36 |
Correct |
1314 ms |
201704 KB |
Output is correct |
37 |
Correct |
1343 ms |
201804 KB |
Output is correct |
38 |
Correct |
1320 ms |
201840 KB |
Output is correct |
39 |
Correct |
1302 ms |
201768 KB |
Output is correct |
40 |
Correct |
1171 ms |
201476 KB |
Output is correct |
41 |
Correct |
1337 ms |
201752 KB |
Output is correct |
42 |
Correct |
1291 ms |
201716 KB |
Output is correct |
43 |
Correct |
730 ms |
196508 KB |
Output is correct |
44 |
Correct |
742 ms |
196952 KB |
Output is correct |
45 |
Correct |
715 ms |
198536 KB |
Output is correct |
46 |
Correct |
684 ms |
176392 KB |
Output is correct |
47 |
Correct |
635 ms |
168432 KB |
Output is correct |
48 |
Correct |
917 ms |
186024 KB |
Output is correct |
49 |
Correct |
909 ms |
186032 KB |
Output is correct |