#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define PF push_front
#define MP make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn = 2e5 + 4;
const int maxl = 23;
int seg[maxl][4 * maxn], t[maxn], a[maxn], b[maxn];
int get2 (int id, int L, int R, int l, int r, int lb, int h = 0) {
if (L == l and R == r) {
int fi = lower_bound (seg[h] + L, seg[h] + R, lb) - seg[h];
return R - fi;
}
int mid = (L + R) >> 1, ret = 0;
if (mid > l)
ret += get2 (2 * id + 0, L, mid, l, min (mid, r), lb, h + 1);
if (mid < r)
ret += get2 (2 * id + 1, mid, R, max (l, mid), r, lb, h + 1);
return ret;
}
int get (int id, int L, int R, int lb, int ub, int h = 0) {
int idx = lower_bound (seg[h] + L, seg[h] + R, lb) - seg[h];
if (idx == R or seg[h][idx] >= ub)
return -1;
if (L + 1 == R)
return L;
int mid = (L + R) >> 1;
int ret = get (2 * id + 1, mid, R, lb, ub, h + 1);
if (ret != -1)
return ret;
return get (2 * id + 0, L, mid, lb, ub, h + 1);
}
void build (int id, int L, int R, int h = 0) {
if (L + 1 == R) {
seg[h][L] = t[L];
return;
}
int mid = (L + R) >> 1;
build (2 * id + 0, L, mid, h + 1);
build (2 * id + 1, mid, R, h + 1);
for (int i = L; i < R; i++)
seg[h][i] = seg[h + 1][i];
sort (seg[h] + L, seg[h] + R);
}
int main () {
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
for (int i = 0; i < k; i++)
cin >> t[i];
build (1, 0, k);
ll ans = 0;
for (int i = 0; i < n; i++) {
int fi = a[i], se = b[i];
if (fi == se) {
ans += fi;
continue;
}
if (fi > se)
swap (fi, se);
int idx = get (1, 0, k, fi, se);
if (idx == -1) {
int x = get2 (1, 0, k, 0, k, se);
if (x % 2 == 0)
ans += a[i];
else
ans += b[i];
}
else {
int x = get2 (1, 0, k, idx, k, se);
if (x % 2 == 0)
ans += se;
else
ans += fi;
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
4 ms |
504 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
4 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
508 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
508 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
4 ms |
504 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
4 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
508 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
508 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
508 KB |
Output is correct |
14 |
Correct |
19 ms |
1400 KB |
Output is correct |
15 |
Correct |
40 ms |
2632 KB |
Output is correct |
16 |
Correct |
63 ms |
3708 KB |
Output is correct |
17 |
Correct |
89 ms |
4856 KB |
Output is correct |
18 |
Correct |
89 ms |
4856 KB |
Output is correct |
19 |
Correct |
83 ms |
4828 KB |
Output is correct |
20 |
Correct |
92 ms |
4856 KB |
Output is correct |
21 |
Correct |
83 ms |
4732 KB |
Output is correct |
22 |
Correct |
57 ms |
4344 KB |
Output is correct |
23 |
Correct |
57 ms |
4344 KB |
Output is correct |
24 |
Correct |
58 ms |
4316 KB |
Output is correct |
25 |
Correct |
58 ms |
4288 KB |
Output is correct |
26 |
Correct |
58 ms |
4600 KB |
Output is correct |
27 |
Correct |
113 ms |
4856 KB |
Output is correct |
28 |
Correct |
58 ms |
4728 KB |
Output is correct |
29 |
Correct |
86 ms |
4772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
4 ms |
504 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
4 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
508 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
508 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
508 KB |
Output is correct |
14 |
Correct |
19 ms |
1400 KB |
Output is correct |
15 |
Correct |
40 ms |
2632 KB |
Output is correct |
16 |
Correct |
63 ms |
3708 KB |
Output is correct |
17 |
Correct |
89 ms |
4856 KB |
Output is correct |
18 |
Correct |
89 ms |
4856 KB |
Output is correct |
19 |
Correct |
83 ms |
4828 KB |
Output is correct |
20 |
Correct |
92 ms |
4856 KB |
Output is correct |
21 |
Correct |
83 ms |
4732 KB |
Output is correct |
22 |
Correct |
57 ms |
4344 KB |
Output is correct |
23 |
Correct |
57 ms |
4344 KB |
Output is correct |
24 |
Correct |
58 ms |
4316 KB |
Output is correct |
25 |
Correct |
58 ms |
4288 KB |
Output is correct |
26 |
Correct |
58 ms |
4600 KB |
Output is correct |
27 |
Correct |
113 ms |
4856 KB |
Output is correct |
28 |
Correct |
58 ms |
4728 KB |
Output is correct |
29 |
Correct |
86 ms |
4772 KB |
Output is correct |
30 |
Correct |
211 ms |
18528 KB |
Output is correct |
31 |
Correct |
289 ms |
19516 KB |
Output is correct |
32 |
Correct |
378 ms |
20736 KB |
Output is correct |
33 |
Correct |
567 ms |
23544 KB |
Output is correct |
34 |
Correct |
192 ms |
18168 KB |
Output is correct |
35 |
Correct |
571 ms |
23488 KB |
Output is correct |
36 |
Correct |
566 ms |
23580 KB |
Output is correct |
37 |
Correct |
571 ms |
23508 KB |
Output is correct |
38 |
Correct |
545 ms |
23516 KB |
Output is correct |
39 |
Correct |
601 ms |
23616 KB |
Output is correct |
40 |
Correct |
442 ms |
23388 KB |
Output is correct |
41 |
Correct |
572 ms |
23564 KB |
Output is correct |
42 |
Correct |
592 ms |
23604 KB |
Output is correct |
43 |
Correct |
319 ms |
22780 KB |
Output is correct |
44 |
Correct |
317 ms |
22984 KB |
Output is correct |
45 |
Correct |
318 ms |
22776 KB |
Output is correct |
46 |
Correct |
346 ms |
21624 KB |
Output is correct |
47 |
Correct |
381 ms |
21624 KB |
Output is correct |
48 |
Correct |
378 ms |
23616 KB |
Output is correct |
49 |
Correct |
339 ms |
23524 KB |
Output is correct |