#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
typedef pair<int, int> ii;
typedef long long ll;
const int len = 2e5+5, mx = 1e9;
ii arr[len];
int num[len], n, m;
struct node{
int sum, rig;
node *left, *right;
node(int s = 0, int p = 0, node *l = NULL, node *r = NULL){
sum = s;
rig = p;
left = l;
right = r;
}
};
typedef node* pnode;
pnode suf[len], null = new node();
pnode add(pnode t, int l, int r, int i, int p){
if (l == r)
return new node(t->sum+1, max(t->rig, p), null, null);
int mid = (l+r)/2;
if (i <= mid)
return new node(t->sum+1, max(t->rig, p), add(t->left, l, mid, i, p), t->right);
return new node(t->sum+1, max(t->rig, p), t->left, add(t->right, mid+1, r, i, p));
}
node join(node a, node b){
node c;
c.rig = max(a.rig, b.rig);
c.sum = a.sum+b.sum;
return c;
}
node ask(pnode t, int l, int r, int i, int j){
if (r < i || j < l)
return node();
if (i <= l && r <= j)
return *t;
int mid = (l+r)/2;
return join(ask(t->left, l, mid, i, j), ask(t->right, mid+1, r, i, j));
}
int main(){
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d %d", &arr[i].fi, &arr[i].se);
for (int i = 1; i <= m; i++)
scanf("%d", &num[i]);
suf[m+1] = null->left = null->right = null;
for (int i = m; i >= 1; i--)
suf[i] = add(suf[i+1], 1, mx, num[i], i);
ll ans = 0;
for (int i = 1; i <= n; i++){
int l = arr[i].fi, r = arr[i].se;
int pos = 0;
if (min(l, r) < max(l, r))
pos = ask(suf[1], 1, mx, min(l, r), max(l, r)-1).rig;
int cnt = ask(suf[pos+1], 1, mx, max(l, r), mx).sum;
if (pos == 0){
if (cnt%2 == 0)
ans += l;
else
ans += r;
}
else{
if (cnt%2 == 0)
ans += max(l, r);
else
ans += min(l, r);
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &arr[i].fi, &arr[i].se);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &num[i]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1408 KB |
Output is correct |
2 |
Correct |
4 ms |
1408 KB |
Output is correct |
3 |
Correct |
6 ms |
1408 KB |
Output is correct |
4 |
Correct |
6 ms |
1408 KB |
Output is correct |
5 |
Correct |
5 ms |
1280 KB |
Output is correct |
6 |
Correct |
6 ms |
1408 KB |
Output is correct |
7 |
Correct |
5 ms |
1280 KB |
Output is correct |
8 |
Correct |
5 ms |
1284 KB |
Output is correct |
9 |
Correct |
5 ms |
1408 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
5 ms |
1280 KB |
Output is correct |
13 |
Correct |
5 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1408 KB |
Output is correct |
2 |
Correct |
4 ms |
1408 KB |
Output is correct |
3 |
Correct |
6 ms |
1408 KB |
Output is correct |
4 |
Correct |
6 ms |
1408 KB |
Output is correct |
5 |
Correct |
5 ms |
1280 KB |
Output is correct |
6 |
Correct |
6 ms |
1408 KB |
Output is correct |
7 |
Correct |
5 ms |
1280 KB |
Output is correct |
8 |
Correct |
5 ms |
1284 KB |
Output is correct |
9 |
Correct |
5 ms |
1408 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
5 ms |
1280 KB |
Output is correct |
13 |
Correct |
5 ms |
1408 KB |
Output is correct |
14 |
Correct |
41 ms |
10572 KB |
Output is correct |
15 |
Correct |
178 ms |
20572 KB |
Output is correct |
16 |
Correct |
132 ms |
30516 KB |
Output is correct |
17 |
Correct |
191 ms |
40460 KB |
Output is correct |
18 |
Correct |
189 ms |
40316 KB |
Output is correct |
19 |
Correct |
188 ms |
40312 KB |
Output is correct |
20 |
Correct |
187 ms |
40316 KB |
Output is correct |
21 |
Correct |
164 ms |
40316 KB |
Output is correct |
22 |
Correct |
122 ms |
40440 KB |
Output is correct |
23 |
Correct |
123 ms |
40416 KB |
Output is correct |
24 |
Correct |
123 ms |
40384 KB |
Output is correct |
25 |
Correct |
121 ms |
40312 KB |
Output is correct |
26 |
Correct |
148 ms |
40284 KB |
Output is correct |
27 |
Correct |
204 ms |
40412 KB |
Output is correct |
28 |
Correct |
147 ms |
40312 KB |
Output is correct |
29 |
Correct |
188 ms |
40440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1408 KB |
Output is correct |
2 |
Correct |
4 ms |
1408 KB |
Output is correct |
3 |
Correct |
6 ms |
1408 KB |
Output is correct |
4 |
Correct |
6 ms |
1408 KB |
Output is correct |
5 |
Correct |
5 ms |
1280 KB |
Output is correct |
6 |
Correct |
6 ms |
1408 KB |
Output is correct |
7 |
Correct |
5 ms |
1280 KB |
Output is correct |
8 |
Correct |
5 ms |
1284 KB |
Output is correct |
9 |
Correct |
5 ms |
1408 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
5 ms |
1280 KB |
Output is correct |
13 |
Correct |
5 ms |
1408 KB |
Output is correct |
14 |
Correct |
41 ms |
10572 KB |
Output is correct |
15 |
Correct |
178 ms |
20572 KB |
Output is correct |
16 |
Correct |
132 ms |
30516 KB |
Output is correct |
17 |
Correct |
191 ms |
40460 KB |
Output is correct |
18 |
Correct |
189 ms |
40316 KB |
Output is correct |
19 |
Correct |
188 ms |
40312 KB |
Output is correct |
20 |
Correct |
187 ms |
40316 KB |
Output is correct |
21 |
Correct |
164 ms |
40316 KB |
Output is correct |
22 |
Correct |
122 ms |
40440 KB |
Output is correct |
23 |
Correct |
123 ms |
40416 KB |
Output is correct |
24 |
Correct |
123 ms |
40384 KB |
Output is correct |
25 |
Correct |
121 ms |
40312 KB |
Output is correct |
26 |
Correct |
148 ms |
40284 KB |
Output is correct |
27 |
Correct |
204 ms |
40412 KB |
Output is correct |
28 |
Correct |
147 ms |
40312 KB |
Output is correct |
29 |
Correct |
188 ms |
40440 KB |
Output is correct |
30 |
Correct |
505 ms |
196868 KB |
Output is correct |
31 |
Correct |
635 ms |
197232 KB |
Output is correct |
32 |
Correct |
892 ms |
197600 KB |
Output is correct |
33 |
Correct |
1126 ms |
198492 KB |
Output is correct |
34 |
Correct |
481 ms |
196856 KB |
Output is correct |
35 |
Correct |
1177 ms |
198476 KB |
Output is correct |
36 |
Correct |
1189 ms |
198612 KB |
Output is correct |
37 |
Correct |
1156 ms |
203824 KB |
Output is correct |
38 |
Correct |
1156 ms |
203896 KB |
Output is correct |
39 |
Correct |
1132 ms |
203872 KB |
Output is correct |
40 |
Correct |
946 ms |
203600 KB |
Output is correct |
41 |
Correct |
1153 ms |
203868 KB |
Output is correct |
42 |
Correct |
1127 ms |
203900 KB |
Output is correct |
43 |
Correct |
705 ms |
203084 KB |
Output is correct |
44 |
Correct |
691 ms |
203116 KB |
Output is correct |
45 |
Correct |
741 ms |
203000 KB |
Output is correct |
46 |
Correct |
738 ms |
202036 KB |
Output is correct |
47 |
Correct |
728 ms |
201688 KB |
Output is correct |
48 |
Correct |
1042 ms |
204016 KB |
Output is correct |
49 |
Correct |
934 ms |
203996 KB |
Output is correct |