#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<long, long>;
using vi = vector<int>;
using vl = vector<long long>;
#define pb push_back
#define F first
#define S second
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sum(a) ( accumulate((a).begin(), (a).end(), 0LL))
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
void fastIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
const ll mod = 1e9 + 7;
struct WaveletTree {
vi b;
int lo, hi;
WaveletTree *l, *r;
WaveletTree (int *i, int *j, int x, int y) {
lo = x, hi = y;
if (lo == hi) return;
int mid = (lo + hi) / 2;
b.pb(0);
for (auto it = i; it != j; it++) {
b.pb(b.back() + (*it <= mid));
}
auto k = stable_partition(i, j, [mid] (int x) {return x <= mid;});
if (i < k) l = new WaveletTree(i, k, lo, mid);
if (k < j) r = new WaveletTree(k, j, mid + 1, hi);
}
int query(int l, int r, int k) {
if (l > r || k < lo) return 0;
if (hi <= k) return r - l + 1;
int lb = b[l - 1], rb = b[r];
return this->l->query(lb + 1, rb, k) + this->r->query(l - lb, r - rb, k);
}
int query(int l, int r, int x, int y) {
l++; r++;
if (y < x) return 0;
return query(l, r, y) - query(l, r, x - 1);
}
};
void solve() {
int n, k;
cin >> n >> k;
vi a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
int t[k];
for (int &u: t) cin >> u;
vi c;
for (int u: a) c.pb(u);
for (int u: b) c.pb(u);
for (int u: t) c.pb(u);
sort(all(c));
c.resize(unique(all(c)) - c.begin());
for (int &u: a) {
u = lowb(c, u);
}
for (int &u: b) {
u = lowb(c, u);
}
for (int &u: t) {
u = lowb(c, u);
}
WaveletTree T(t, t + k, 0, 6e5);
ll ans = 0;
for (int i = 0; i < n; i++) {
int x = T.query(0, k - 1, min(a[i], b[i]), max(a[i], b[i]) - 1);
if (x == 0) {
if (T.query(0, k - 1, max(a[i], b[i]), 1e9) % 2) {
ans += c[b[i]];
} else {
ans += c[a[i]];
}
} else {
int l = -1, r = k - 1;
while (l + 1 < r) {
int m = (l + r) / 2;
if (T.query(0, m, min(a[i], b[i]), max(a[i], b[i]) - 1) == x) {
r = m;
} else {
l = m;
}
}
if (T.query(r + 1, k - 1, max(a[i], b[i]), 1e9) % 2) {
ans += c[min(a[i], b[i])];
} else {
ans += c[max(a[i], b[i])];
}
}
}
cout << ans;
}
int main() {
fastIO();
cout << setprecision(15) << fixed;
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
5 ms |
588 KB |
Output is correct |
3 |
Correct |
8 ms |
716 KB |
Output is correct |
4 |
Correct |
7 ms |
716 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
6 ms |
712 KB |
Output is correct |
7 |
Correct |
6 ms |
716 KB |
Output is correct |
8 |
Correct |
6 ms |
584 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
7 ms |
460 KB |
Output is correct |
11 |
Correct |
6 ms |
588 KB |
Output is correct |
12 |
Correct |
6 ms |
588 KB |
Output is correct |
13 |
Correct |
6 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
5 ms |
588 KB |
Output is correct |
3 |
Correct |
8 ms |
716 KB |
Output is correct |
4 |
Correct |
7 ms |
716 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
6 ms |
712 KB |
Output is correct |
7 |
Correct |
6 ms |
716 KB |
Output is correct |
8 |
Correct |
6 ms |
584 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
7 ms |
460 KB |
Output is correct |
11 |
Correct |
6 ms |
588 KB |
Output is correct |
12 |
Correct |
6 ms |
588 KB |
Output is correct |
13 |
Correct |
6 ms |
588 KB |
Output is correct |
14 |
Correct |
78 ms |
4304 KB |
Output is correct |
15 |
Correct |
170 ms |
8272 KB |
Output is correct |
16 |
Correct |
268 ms |
12128 KB |
Output is correct |
17 |
Correct |
409 ms |
16116 KB |
Output is correct |
18 |
Correct |
374 ms |
16112 KB |
Output is correct |
19 |
Correct |
387 ms |
16120 KB |
Output is correct |
20 |
Correct |
386 ms |
16248 KB |
Output is correct |
21 |
Correct |
341 ms |
14812 KB |
Output is correct |
22 |
Correct |
299 ms |
13476 KB |
Output is correct |
23 |
Correct |
297 ms |
11780 KB |
Output is correct |
24 |
Correct |
291 ms |
11104 KB |
Output is correct |
25 |
Correct |
303 ms |
13380 KB |
Output is correct |
26 |
Correct |
265 ms |
13852 KB |
Output is correct |
27 |
Correct |
330 ms |
14532 KB |
Output is correct |
28 |
Correct |
253 ms |
14332 KB |
Output is correct |
29 |
Correct |
213 ms |
15148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
5 ms |
588 KB |
Output is correct |
3 |
Correct |
8 ms |
716 KB |
Output is correct |
4 |
Correct |
7 ms |
716 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
6 ms |
712 KB |
Output is correct |
7 |
Correct |
6 ms |
716 KB |
Output is correct |
8 |
Correct |
6 ms |
584 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
7 ms |
460 KB |
Output is correct |
11 |
Correct |
6 ms |
588 KB |
Output is correct |
12 |
Correct |
6 ms |
588 KB |
Output is correct |
13 |
Correct |
6 ms |
588 KB |
Output is correct |
14 |
Correct |
78 ms |
4304 KB |
Output is correct |
15 |
Correct |
170 ms |
8272 KB |
Output is correct |
16 |
Correct |
268 ms |
12128 KB |
Output is correct |
17 |
Correct |
409 ms |
16116 KB |
Output is correct |
18 |
Correct |
374 ms |
16112 KB |
Output is correct |
19 |
Correct |
387 ms |
16120 KB |
Output is correct |
20 |
Correct |
386 ms |
16248 KB |
Output is correct |
21 |
Correct |
341 ms |
14812 KB |
Output is correct |
22 |
Correct |
299 ms |
13476 KB |
Output is correct |
23 |
Correct |
297 ms |
11780 KB |
Output is correct |
24 |
Correct |
291 ms |
11104 KB |
Output is correct |
25 |
Correct |
303 ms |
13380 KB |
Output is correct |
26 |
Correct |
265 ms |
13852 KB |
Output is correct |
27 |
Correct |
330 ms |
14532 KB |
Output is correct |
28 |
Correct |
253 ms |
14332 KB |
Output is correct |
29 |
Correct |
213 ms |
15148 KB |
Output is correct |
30 |
Correct |
327 ms |
57228 KB |
Output is correct |
31 |
Correct |
777 ms |
60528 KB |
Output is correct |
32 |
Correct |
1376 ms |
69600 KB |
Output is correct |
33 |
Correct |
2585 ms |
79668 KB |
Output is correct |
34 |
Correct |
203 ms |
54376 KB |
Output is correct |
35 |
Correct |
2563 ms |
79668 KB |
Output is correct |
36 |
Correct |
2596 ms |
79544 KB |
Output is correct |
37 |
Correct |
2636 ms |
79716 KB |
Output is correct |
38 |
Correct |
2558 ms |
79420 KB |
Output is correct |
39 |
Correct |
2519 ms |
79932 KB |
Output is correct |
40 |
Correct |
1915 ms |
69264 KB |
Output is correct |
41 |
Correct |
2530 ms |
80424 KB |
Output is correct |
42 |
Correct |
2515 ms |
79916 KB |
Output is correct |
43 |
Correct |
873 ms |
60720 KB |
Output is correct |
44 |
Correct |
913 ms |
60768 KB |
Output is correct |
45 |
Correct |
1022 ms |
60968 KB |
Output is correct |
46 |
Correct |
1735 ms |
58556 KB |
Output is correct |
47 |
Correct |
1617 ms |
53300 KB |
Output is correct |
48 |
Correct |
1959 ms |
70572 KB |
Output is correct |
49 |
Correct |
1624 ms |
70588 KB |
Output is correct |