#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 200000 + 7;
const int INF = (int) 1e9 + 7;
int n;
int q;
int a[N];
int b[N];
int t[N];
struct Node {
int l;
int r;
int mx;
Node* lft;
Node* rgh;
Node(int l, int r) : l(l), r(r), mx(-INF), lft(nullptr), rgh(nullptr) {
}
};
Node* root = new Node(1, (int) 1e9);
void upd(Node* node, int index) {
assert(node->l <= t[index] && t[index] <= node->r);
if (node->l == node->r) {
node->mx = max(node->mx, index);
} else {
int mid = (node->l + node->r) / 2;
if (t[index] <= mid) {
if (!node->lft) {
node->lft = new Node(node->l, mid);
}
upd(node->lft, index);
} else {
if (!node->rgh) {
node->rgh = new Node(mid + 1, node->r);
}
upd(node->rgh, index);
}
node->mx = -INF;
if (node->lft) {
node->mx = max(node->mx, node->lft->mx);
}
if (node->rgh) {
node->mx = max(node->mx, node->rgh->mx);
}
}
}
int query(Node* node, int l, int r) {
assert(!(node->r < l || r < node->l));
if (l <= node->l && node->r <= r) {
return node->mx;
}
int mid = (node->l + node->r) / 2, sol = -INF;
if (node->lft && l <= mid) {
sol = max(sol, query(node->lft, l, r));
}
if (node->rgh && mid + 1 <= r) {
sol = max(sol, query(node->rgh, l, r));
}
return sol;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
/// freopen ("input", "r", stdin);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
for (int iq = 1; iq <= q; iq++) {
cin >> t[iq];
upd(root, iq);
}
ll sol = 0;
for (int i = 1; i <= n; i++) {
int mn = min(a[i], b[i]);
int mx = max(a[i], b[i]);
int start, current, v = query(root, mn, mx - 1);
if (v == -INF) {
start = 1;
current = a[i];
} else {
start = v + 1;
current = mx;
}
int cnt = 0;
for (int j = start; j <= q; j++) {
cnt += (mx <= t[j]);
}
if (cnt % 2 == 0) {
sol += current;
} else {
sol += a[i] + b[i] - current;
}
}
cout << sol << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1228 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
3 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
3 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1228 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
3 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
3 ms |
1228 KB |
Output is correct |
14 |
Correct |
19 ms |
8684 KB |
Output is correct |
15 |
Correct |
41 ms |
16212 KB |
Output is correct |
16 |
Correct |
62 ms |
23336 KB |
Output is correct |
17 |
Correct |
87 ms |
30260 KB |
Output is correct |
18 |
Correct |
87 ms |
31428 KB |
Output is correct |
19 |
Correct |
99 ms |
31544 KB |
Output is correct |
20 |
Correct |
94 ms |
31592 KB |
Output is correct |
21 |
Correct |
96 ms |
31480 KB |
Output is correct |
22 |
Correct |
40 ms |
5828 KB |
Output is correct |
23 |
Correct |
38 ms |
5272 KB |
Output is correct |
24 |
Correct |
37 ms |
4580 KB |
Output is correct |
25 |
Correct |
40 ms |
6788 KB |
Output is correct |
26 |
Correct |
518 ms |
31076 KB |
Output is correct |
27 |
Correct |
623 ms |
31496 KB |
Output is correct |
28 |
Correct |
616 ms |
30468 KB |
Output is correct |
29 |
Correct |
1054 ms |
31552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1228 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
3 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
3 ms |
1228 KB |
Output is correct |
14 |
Correct |
19 ms |
8684 KB |
Output is correct |
15 |
Correct |
41 ms |
16212 KB |
Output is correct |
16 |
Correct |
62 ms |
23336 KB |
Output is correct |
17 |
Correct |
87 ms |
30260 KB |
Output is correct |
18 |
Correct |
87 ms |
31428 KB |
Output is correct |
19 |
Correct |
99 ms |
31544 KB |
Output is correct |
20 |
Correct |
94 ms |
31592 KB |
Output is correct |
21 |
Correct |
96 ms |
31480 KB |
Output is correct |
22 |
Correct |
40 ms |
5828 KB |
Output is correct |
23 |
Correct |
38 ms |
5272 KB |
Output is correct |
24 |
Correct |
37 ms |
4580 KB |
Output is correct |
25 |
Correct |
40 ms |
6788 KB |
Output is correct |
26 |
Correct |
518 ms |
31076 KB |
Output is correct |
27 |
Correct |
623 ms |
31496 KB |
Output is correct |
28 |
Correct |
616 ms |
30468 KB |
Output is correct |
29 |
Correct |
1054 ms |
31552 KB |
Output is correct |
30 |
Correct |
282 ms |
129376 KB |
Output is correct |
31 |
Correct |
340 ms |
130620 KB |
Output is correct |
32 |
Correct |
428 ms |
132052 KB |
Output is correct |
33 |
Correct |
561 ms |
134632 KB |
Output is correct |
34 |
Correct |
268 ms |
129092 KB |
Output is correct |
35 |
Correct |
557 ms |
134620 KB |
Output is correct |
36 |
Correct |
563 ms |
134528 KB |
Output is correct |
37 |
Correct |
572 ms |
134544 KB |
Output is correct |
38 |
Correct |
552 ms |
134592 KB |
Output is correct |
39 |
Correct |
560 ms |
134584 KB |
Output is correct |
40 |
Correct |
480 ms |
134472 KB |
Output is correct |
41 |
Correct |
581 ms |
134564 KB |
Output is correct |
42 |
Correct |
550 ms |
134696 KB |
Output is correct |
43 |
Correct |
352 ms |
130836 KB |
Output is correct |
44 |
Correct |
337 ms |
124508 KB |
Output is correct |
45 |
Correct |
327 ms |
112108 KB |
Output is correct |
46 |
Correct |
242 ms |
25448 KB |
Output is correct |
47 |
Correct |
444 ms |
21188 KB |
Output is correct |
48 |
Execution timed out |
3057 ms |
133188 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |