#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 2e5+10, inf = 0x3f3f3f3f;
struct PersistentSegmentTree {
struct Node {
Node *l, *r;
int val;
Node() : l(this), r(this), val(0) {}
};
int t = 0;
Node *root[maxn]; // we have a root for each version of the seg
PersistentSegmentTree() { root[0] = new Node(); }
void upd(int pos) {
++t;
root[t] = upd(root[t-1], 1, inf, pos);
}
Node* upd(Node *node, int i, int j, int pos) { // add v to interval l, r
Node *aq = new Node();
*aq = *node;
aq->val++;
if(i == j) return aq;
int m = (i+j) >> 1;
if(pos <= m)
aq->l = upd(aq->l, i, m, pos);
else
aq->r = upd(aq->r, m+1, j, pos);
return aq;
}
int query(Node *node, int i, int j, int l, int r) { // query the interval [l, r] on this version of the seg
if(i > r || j < l) return 0;
if(i >= l && j <= r) return node->val;
int m = (i+j) >> 1;
return query(node->l, i, m, l, r) + query(node->r, m+1, j, l, r);
}
} segTime;
struct SegmentTree {
int tree[4*maxn], a[maxn]; // we have a root for each version of the seg
void build(int node, int i, int j) {
if(i == j) return (void)(tree[node] = a[i]);
int m = (i+j) >> 1;
build(node<<1, i, m);
build(node<<1|1, m+1, j);
tree[node] = max(tree[node<<1], tree[node<<1|1]);
}
int query(int node, int i, int j, int l, int r) { // query the interval [l, r]
if(i > r || j < l) return 0;
if(i >= l && j <= r) return tree[node];
int m = (i+j) >> 1;
return max(query(node<<1, i, m, l, r), query(node<<1|1, m+1, j, l, r));
}
} segX;
int A[maxn], B[maxn];
pair<int,int> X[maxn];
int main() {
int n, k; scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%d %d", A+i, B+i);
for(int i = 1; i <= k; i++) {
int x; scanf("%d", &x); X[i] = {x, i};
segTime.upd(x);
}
sort(X+1, X+k+1);
for(int i = 1; i <= k; i++)
segX.a[i] = X[i].second;
segX.build(1, 1, k);
long long ans = 0;
for(int i = 1; i <= n; i++) {
int a = A[i], b = B[i];
if(a > b) swap(a, b);
// quero achar o evento com o maior tempo tal que o valor dele está contido em [a, b[
int last = 0;
if(a != b) {
int itL = (lower_bound(X+1, X+1+k, pair<int,int>(a, 0)) - X);
int itR = (lower_bound(X+1, X+1+k, pair<int,int>(b, 0)) - X) - 1;
// printf("%d %d -> %d %d\n", a, b, itL, itR);
last = segX.query(1, 1, k, itL, itR);
} else last = 0;
// printf("%d %d -> %d\n", a, b, last);
if(last != 0)
ans += (segTime.query(segTime.root[segTime.t], 1, inf, b, inf) -
segTime.query(segTime.root[last], 1, inf, b, inf)) % 2 == 0 ? b : a;
else
ans += segTime.query(segTime.root[segTime.t], 1, inf, b, inf) % 2 == 0 ? A[i] : B[i];
}
printf("%lld\n", ans);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:70:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | int n, k; scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d %d", A+i, B+i);
| ~~~~~^~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:76:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | int x; scanf("%d", &x); X[i] = {x, i};
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1340 KB |
Output is correct |
2 |
Correct |
4 ms |
1356 KB |
Output is correct |
3 |
Correct |
3 ms |
1356 KB |
Output is correct |
4 |
Correct |
5 ms |
1356 KB |
Output is correct |
5 |
Correct |
3 ms |
1356 KB |
Output is correct |
6 |
Correct |
3 ms |
1356 KB |
Output is correct |
7 |
Correct |
3 ms |
1356 KB |
Output is correct |
8 |
Correct |
3 ms |
1348 KB |
Output is correct |
9 |
Correct |
2 ms |
1348 KB |
Output is correct |
10 |
Correct |
4 ms |
1228 KB |
Output is correct |
11 |
Correct |
5 ms |
1356 KB |
Output is correct |
12 |
Correct |
3 ms |
1356 KB |
Output is correct |
13 |
Correct |
5 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1340 KB |
Output is correct |
2 |
Correct |
4 ms |
1356 KB |
Output is correct |
3 |
Correct |
3 ms |
1356 KB |
Output is correct |
4 |
Correct |
5 ms |
1356 KB |
Output is correct |
5 |
Correct |
3 ms |
1356 KB |
Output is correct |
6 |
Correct |
3 ms |
1356 KB |
Output is correct |
7 |
Correct |
3 ms |
1356 KB |
Output is correct |
8 |
Correct |
3 ms |
1348 KB |
Output is correct |
9 |
Correct |
2 ms |
1348 KB |
Output is correct |
10 |
Correct |
4 ms |
1228 KB |
Output is correct |
11 |
Correct |
5 ms |
1356 KB |
Output is correct |
12 |
Correct |
3 ms |
1356 KB |
Output is correct |
13 |
Correct |
5 ms |
1356 KB |
Output is correct |
14 |
Correct |
27 ms |
10700 KB |
Output is correct |
15 |
Correct |
52 ms |
21076 KB |
Output is correct |
16 |
Correct |
83 ms |
31304 KB |
Output is correct |
17 |
Correct |
150 ms |
41892 KB |
Output is correct |
18 |
Correct |
118 ms |
41980 KB |
Output is correct |
19 |
Correct |
119 ms |
41896 KB |
Output is correct |
20 |
Correct |
115 ms |
41900 KB |
Output is correct |
21 |
Correct |
108 ms |
41756 KB |
Output is correct |
22 |
Correct |
118 ms |
41420 KB |
Output is correct |
23 |
Correct |
95 ms |
41424 KB |
Output is correct |
24 |
Correct |
88 ms |
41368 KB |
Output is correct |
25 |
Correct |
88 ms |
41388 KB |
Output is correct |
26 |
Correct |
110 ms |
41648 KB |
Output is correct |
27 |
Correct |
105 ms |
41900 KB |
Output is correct |
28 |
Correct |
100 ms |
41816 KB |
Output is correct |
29 |
Correct |
107 ms |
41892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1340 KB |
Output is correct |
2 |
Correct |
4 ms |
1356 KB |
Output is correct |
3 |
Correct |
3 ms |
1356 KB |
Output is correct |
4 |
Correct |
5 ms |
1356 KB |
Output is correct |
5 |
Correct |
3 ms |
1356 KB |
Output is correct |
6 |
Correct |
3 ms |
1356 KB |
Output is correct |
7 |
Correct |
3 ms |
1356 KB |
Output is correct |
8 |
Correct |
3 ms |
1348 KB |
Output is correct |
9 |
Correct |
2 ms |
1348 KB |
Output is correct |
10 |
Correct |
4 ms |
1228 KB |
Output is correct |
11 |
Correct |
5 ms |
1356 KB |
Output is correct |
12 |
Correct |
3 ms |
1356 KB |
Output is correct |
13 |
Correct |
5 ms |
1356 KB |
Output is correct |
14 |
Correct |
27 ms |
10700 KB |
Output is correct |
15 |
Correct |
52 ms |
21076 KB |
Output is correct |
16 |
Correct |
83 ms |
31304 KB |
Output is correct |
17 |
Correct |
150 ms |
41892 KB |
Output is correct |
18 |
Correct |
118 ms |
41980 KB |
Output is correct |
19 |
Correct |
119 ms |
41896 KB |
Output is correct |
20 |
Correct |
115 ms |
41900 KB |
Output is correct |
21 |
Correct |
108 ms |
41756 KB |
Output is correct |
22 |
Correct |
118 ms |
41420 KB |
Output is correct |
23 |
Correct |
95 ms |
41424 KB |
Output is correct |
24 |
Correct |
88 ms |
41368 KB |
Output is correct |
25 |
Correct |
88 ms |
41388 KB |
Output is correct |
26 |
Correct |
110 ms |
41648 KB |
Output is correct |
27 |
Correct |
105 ms |
41900 KB |
Output is correct |
28 |
Correct |
100 ms |
41816 KB |
Output is correct |
29 |
Correct |
107 ms |
41892 KB |
Output is correct |
30 |
Correct |
353 ms |
202448 KB |
Output is correct |
31 |
Correct |
467 ms |
203604 KB |
Output is correct |
32 |
Correct |
514 ms |
204844 KB |
Output is correct |
33 |
Correct |
704 ms |
207756 KB |
Output is correct |
34 |
Correct |
340 ms |
202288 KB |
Output is correct |
35 |
Correct |
713 ms |
207636 KB |
Output is correct |
36 |
Correct |
681 ms |
207652 KB |
Output is correct |
37 |
Correct |
671 ms |
207780 KB |
Output is correct |
38 |
Correct |
709 ms |
207776 KB |
Output is correct |
39 |
Correct |
763 ms |
207696 KB |
Output is correct |
40 |
Correct |
585 ms |
207364 KB |
Output is correct |
41 |
Correct |
702 ms |
207804 KB |
Output is correct |
42 |
Correct |
771 ms |
207720 KB |
Output is correct |
43 |
Correct |
499 ms |
207120 KB |
Output is correct |
44 |
Correct |
490 ms |
206992 KB |
Output is correct |
45 |
Correct |
473 ms |
207016 KB |
Output is correct |
46 |
Correct |
494 ms |
205892 KB |
Output is correct |
47 |
Correct |
483 ms |
205652 KB |
Output is correct |
48 |
Correct |
591 ms |
207732 KB |
Output is correct |
49 |
Correct |
634 ms |
207844 KB |
Output is correct |