#include<bits/stdc++.h>
#define STIZE(x) fprintf(stderr, "STIZE%d\n", x);
#define PRINT(x) fprintf(stderr, "%s = %d\n", #x, x);
#define NL(x) printf("%c", " \n"[(x)]);
#define lld long long
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 200010
lld N, K, a[MAXN], b[MAXN], t[MAXN];
lld seg[4*MAXN];
vector<pii> T, B;
void init(int node, int l, int r) {
if(l == r) seg[node] = T[l].se;
else {
init(2*node, l, mid); init(2*node+1, mid+1, r);
seg[node] = max(seg[2*node], seg[2*node+1]);
}
}
lld query(int node, int l, int r, int L, int R) {
if(r < l || R < L || r < L || R < l) return 0;
if(L <= l && r <= R) return seg[node];
return max(query(2*node, l, mid, L, R), query(2*node+1, mid+1, r, L, R));
}
lld bit[MAXN];
void update(int idx, lld val) {
for( ; idx < MAXN; idx += idx&-idx) bit[idx] += val;
}
lld query(int idx) {
lld rez = 0;
for( ; idx ; idx -= idx&-idx) rez += bit[idx];
return rez;
}
int bsL(int key) {
auto l = lower_bound(all(T), make_pair(key, -INF));
return l-T.begin();
}
int bsR(int key) {
auto r = lower_bound(all(T), make_pair(key, INF)); r--;
return r-T.begin();
}
int sred[MAXN];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) cin >> a[i] >> b[i];
for(int i = 1; i <= K; i++) cin >> t[i];
T.pb({-INF, -INF}); B.pb({-INF, -INF});
for(int i = 1; i <= N; i++) B.pb({max(a[i], b[i]), i});
for(int i = 1; i <= K; i++) T.pb({t[i], i});
sort(all(T)); sort(all(B));
init(1, 1, K);
lld rez = 0;
for(int i = 1; i <= N; i++) {
int L = bsL(min(a[i], b[i])), R = bsR(max(a[i], b[i])-1);
sred[i] = query(1, 1, K, L, R);
}
int cb = N, ct = K;
while(cb > 0) {
if(B[cb].fi <= T[ct].fi) {
update(T[ct].se, +1);
ct--;
} else {
lld vece = (query(MAXN-1)-query(sred[B[cb].se]))%2;
int idx = B[cb].se;
if(sred[idx] == 0) {
if(vece == 1) rez += b[idx];
else rez += a[idx];
} else {
if(vece == 1) rez += min(a[idx], b[idx]);
else rez += max(a[idx], b[idx]);
}
cb--;
}
}
cout << rez << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
15 ms |
1400 KB |
Output is correct |
15 |
Correct |
28 ms |
2164 KB |
Output is correct |
16 |
Correct |
44 ms |
2720 KB |
Output is correct |
17 |
Correct |
62 ms |
3700 KB |
Output is correct |
18 |
Correct |
61 ms |
3696 KB |
Output is correct |
19 |
Correct |
65 ms |
3776 KB |
Output is correct |
20 |
Correct |
58 ms |
3700 KB |
Output is correct |
21 |
Correct |
54 ms |
3700 KB |
Output is correct |
22 |
Correct |
39 ms |
3696 KB |
Output is correct |
23 |
Correct |
38 ms |
3696 KB |
Output is correct |
24 |
Correct |
39 ms |
3672 KB |
Output is correct |
25 |
Correct |
37 ms |
3672 KB |
Output is correct |
26 |
Correct |
45 ms |
3572 KB |
Output is correct |
27 |
Correct |
54 ms |
3700 KB |
Output is correct |
28 |
Correct |
48 ms |
3696 KB |
Output is correct |
29 |
Correct |
45 ms |
3700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
15 ms |
1400 KB |
Output is correct |
15 |
Correct |
28 ms |
2164 KB |
Output is correct |
16 |
Correct |
44 ms |
2720 KB |
Output is correct |
17 |
Correct |
62 ms |
3700 KB |
Output is correct |
18 |
Correct |
61 ms |
3696 KB |
Output is correct |
19 |
Correct |
65 ms |
3776 KB |
Output is correct |
20 |
Correct |
58 ms |
3700 KB |
Output is correct |
21 |
Correct |
54 ms |
3700 KB |
Output is correct |
22 |
Correct |
39 ms |
3696 KB |
Output is correct |
23 |
Correct |
38 ms |
3696 KB |
Output is correct |
24 |
Correct |
39 ms |
3672 KB |
Output is correct |
25 |
Correct |
37 ms |
3672 KB |
Output is correct |
26 |
Correct |
45 ms |
3572 KB |
Output is correct |
27 |
Correct |
54 ms |
3700 KB |
Output is correct |
28 |
Correct |
48 ms |
3696 KB |
Output is correct |
29 |
Correct |
45 ms |
3700 KB |
Output is correct |
30 |
Correct |
82 ms |
12004 KB |
Output is correct |
31 |
Correct |
136 ms |
13548 KB |
Output is correct |
32 |
Correct |
210 ms |
15960 KB |
Output is correct |
33 |
Correct |
341 ms |
20564 KB |
Output is correct |
34 |
Correct |
61 ms |
11240 KB |
Output is correct |
35 |
Correct |
350 ms |
20568 KB |
Output is correct |
36 |
Correct |
350 ms |
20708 KB |
Output is correct |
37 |
Correct |
352 ms |
20552 KB |
Output is correct |
38 |
Correct |
371 ms |
20644 KB |
Output is correct |
39 |
Correct |
344 ms |
20580 KB |
Output is correct |
40 |
Correct |
291 ms |
20408 KB |
Output is correct |
41 |
Correct |
348 ms |
20592 KB |
Output is correct |
42 |
Correct |
372 ms |
20612 KB |
Output is correct |
43 |
Correct |
183 ms |
18904 KB |
Output is correct |
44 |
Correct |
186 ms |
19016 KB |
Output is correct |
45 |
Correct |
195 ms |
19516 KB |
Output is correct |
46 |
Correct |
224 ms |
18632 KB |
Output is correct |
47 |
Correct |
218 ms |
18548 KB |
Output is correct |
48 |
Correct |
288 ms |
20712 KB |
Output is correct |
49 |
Correct |
291 ms |
20580 KB |
Output is correct |