#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000;
const int MAX_M = 200000;
int a[MAX_N + 5], b[MAX_N + 5];
int q[MAX_N + 5];
int srt[MAX_N + 5], srtq[MAX_M + 5];
int nor[2 * MAX_N + MAX_M + 5];
int aib[2 * MAX_N + MAX_M + 5];
int last[MAX_N + 5];
map<int, int>mp;
bool cmp1(int x, int y) {
return min(a[x], b[x]) > min(a[y], b[y]);
}
bool cmp2(int x, int y) {
return q[x] > q[y];
}
bool cmp3(int x, int y) {
return last[x] > last[y];
}
void update(int poz, int val, int n) {
assert(poz != 0);
for (; poz <= n; poz += (poz & -poz))
aib[poz] = max(aib[poz], val);
}
int query(int poz) {
int ans = 0;
for (; poz; poz -= (poz & -poz))
ans = max(ans, aib[poz]);
return ans;
}
void update1(int poz, int val, int n) {
assert(poz != 0);
for (; poz <= n; poz += (poz & -poz))
aib[poz]++;
}
int query1(int poz) {
int ans = 0;
for (; poz; poz -= (poz & -poz))
ans += aib[poz];
return ans;
}
int main() {
int n, m, k = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &a[i], &b[i]);
nor[++k] = a[i];
nor[++k] = b[i];
srt[i] = i;
}
for (int i = 1; i <= m; ++i) {
scanf("%d", &q[i]);
nor[++k] = q[i];
srtq[i] = i;
}
sort(nor + 1, nor + k + 1);
int x = 0;
for (int i = 1; i <= k;) {
x++;
int aux = nor[i];
while (i <= k && nor[i] == aux)
++i;
mp[aux] = x;
}
sort(srt + 1, srt + n + 1, cmp1);
sort(srtq + 1, srtq + m + 1, cmp2);
for (int i = 1, j = 1; i <= n && j <= m;) {
while (j <= m && q[srtq[j]] >= min(a[srt[i]], b[srt[i]])) {
update(mp[q[srtq[j]]], srtq[j], k);
j++;
}
while (i <= n && min(a[srt[i]], b[srt[i]]) > q[srtq[j]]) {
last[srt[i]] = query(mp[max(a[srt[i]], b[srt[i]])] - 1);
i++;
}
}
//for (int i = 1; i <= n; ++i)
//printf("%d %d\n", i, last[i]);
//return 0;
sort(srt + 1, srt + n + 1, cmp3);
memset(aib, 0, sizeof(aib));
long long ans = 0;
int i = 1;
while (i <= n && last[srt[i]] == m)
ans += max(a[srt[i]], b[srt[i]]), i++;
for (int j = m; i <= n && j >= 1; --j) {
update1(mp[q[j]], 1, k);
while (i <= n && last[srt[i]] + 1 == j) {
int aux = query1(k) - query1(mp[max(a[srt[i]], b[srt[i]])] - 1);
if (last[srt[i]] == 0) {
if (aux % 2 == 1)
ans += b[srt[i]];
else
ans += a[srt[i]];
} else {
if (aux % 2 == 1) {
ans += min(a[srt[i]], b[srt[i]]);
}
else {
ans += max(a[srt[i]], b[srt[i]]);
}
}
i++;
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:58:8: 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:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i], &b[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
3072 KB |
Output is correct |
3 |
Correct |
6 ms |
3340 KB |
Output is correct |
4 |
Correct |
6 ms |
3340 KB |
Output is correct |
5 |
Correct |
6 ms |
3340 KB |
Output is correct |
6 |
Correct |
6 ms |
3376 KB |
Output is correct |
7 |
Correct |
6 ms |
3568 KB |
Output is correct |
8 |
Correct |
6 ms |
3568 KB |
Output is correct |
9 |
Correct |
5 ms |
3568 KB |
Output is correct |
10 |
Correct |
5 ms |
3568 KB |
Output is correct |
11 |
Correct |
6 ms |
3568 KB |
Output is correct |
12 |
Correct |
5 ms |
3632 KB |
Output is correct |
13 |
Correct |
5 ms |
3632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
3072 KB |
Output is correct |
3 |
Correct |
6 ms |
3340 KB |
Output is correct |
4 |
Correct |
6 ms |
3340 KB |
Output is correct |
5 |
Correct |
6 ms |
3340 KB |
Output is correct |
6 |
Correct |
6 ms |
3376 KB |
Output is correct |
7 |
Correct |
6 ms |
3568 KB |
Output is correct |
8 |
Correct |
6 ms |
3568 KB |
Output is correct |
9 |
Correct |
5 ms |
3568 KB |
Output is correct |
10 |
Correct |
5 ms |
3568 KB |
Output is correct |
11 |
Correct |
6 ms |
3568 KB |
Output is correct |
12 |
Correct |
5 ms |
3632 KB |
Output is correct |
13 |
Correct |
5 ms |
3632 KB |
Output is correct |
14 |
Correct |
33 ms |
5512 KB |
Output is correct |
15 |
Correct |
67 ms |
7848 KB |
Output is correct |
16 |
Correct |
118 ms |
10528 KB |
Output is correct |
17 |
Correct |
167 ms |
13596 KB |
Output is correct |
18 |
Correct |
186 ms |
14672 KB |
Output is correct |
19 |
Correct |
134 ms |
15792 KB |
Output is correct |
20 |
Correct |
158 ms |
16968 KB |
Output is correct |
21 |
Correct |
125 ms |
18044 KB |
Output is correct |
22 |
Correct |
82 ms |
18044 KB |
Output is correct |
23 |
Correct |
74 ms |
18044 KB |
Output is correct |
24 |
Correct |
65 ms |
18044 KB |
Output is correct |
25 |
Correct |
80 ms |
19460 KB |
Output is correct |
26 |
Correct |
104 ms |
19536 KB |
Output is correct |
27 |
Correct |
115 ms |
21332 KB |
Output is correct |
28 |
Correct |
119 ms |
22384 KB |
Output is correct |
29 |
Correct |
129 ms |
24452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
3072 KB |
Output is correct |
3 |
Correct |
6 ms |
3340 KB |
Output is correct |
4 |
Correct |
6 ms |
3340 KB |
Output is correct |
5 |
Correct |
6 ms |
3340 KB |
Output is correct |
6 |
Correct |
6 ms |
3376 KB |
Output is correct |
7 |
Correct |
6 ms |
3568 KB |
Output is correct |
8 |
Correct |
6 ms |
3568 KB |
Output is correct |
9 |
Correct |
5 ms |
3568 KB |
Output is correct |
10 |
Correct |
5 ms |
3568 KB |
Output is correct |
11 |
Correct |
6 ms |
3568 KB |
Output is correct |
12 |
Correct |
5 ms |
3632 KB |
Output is correct |
13 |
Correct |
5 ms |
3632 KB |
Output is correct |
14 |
Correct |
33 ms |
5512 KB |
Output is correct |
15 |
Correct |
67 ms |
7848 KB |
Output is correct |
16 |
Correct |
118 ms |
10528 KB |
Output is correct |
17 |
Correct |
167 ms |
13596 KB |
Output is correct |
18 |
Correct |
186 ms |
14672 KB |
Output is correct |
19 |
Correct |
134 ms |
15792 KB |
Output is correct |
20 |
Correct |
158 ms |
16968 KB |
Output is correct |
21 |
Correct |
125 ms |
18044 KB |
Output is correct |
22 |
Correct |
82 ms |
18044 KB |
Output is correct |
23 |
Correct |
74 ms |
18044 KB |
Output is correct |
24 |
Correct |
65 ms |
18044 KB |
Output is correct |
25 |
Correct |
80 ms |
19460 KB |
Output is correct |
26 |
Correct |
104 ms |
19536 KB |
Output is correct |
27 |
Correct |
115 ms |
21332 KB |
Output is correct |
28 |
Correct |
119 ms |
22384 KB |
Output is correct |
29 |
Correct |
129 ms |
24452 KB |
Output is correct |
30 |
Correct |
241 ms |
33504 KB |
Output is correct |
31 |
Correct |
524 ms |
40928 KB |
Output is correct |
32 |
Correct |
695 ms |
50768 KB |
Output is correct |
33 |
Correct |
1068 ms |
68440 KB |
Output is correct |
34 |
Correct |
203 ms |
68440 KB |
Output is correct |
35 |
Correct |
1192 ms |
76216 KB |
Output is correct |
36 |
Correct |
1060 ms |
82132 KB |
Output is correct |
37 |
Correct |
1124 ms |
87832 KB |
Output is correct |
38 |
Correct |
919 ms |
93484 KB |
Output is correct |
39 |
Correct |
1046 ms |
99308 KB |
Output is correct |
40 |
Correct |
735 ms |
105004 KB |
Output is correct |
41 |
Correct |
717 ms |
110740 KB |
Output is correct |
42 |
Correct |
780 ms |
116736 KB |
Output is correct |
43 |
Correct |
578 ms |
121652 KB |
Output is correct |
44 |
Correct |
532 ms |
126860 KB |
Output is correct |
45 |
Correct |
527 ms |
131968 KB |
Output is correct |
46 |
Correct |
413 ms |
131968 KB |
Output is correct |
47 |
Correct |
395 ms |
131968 KB |
Output is correct |
48 |
Correct |
761 ms |
136072 KB |
Output is correct |
49 |
Correct |
858 ms |
142020 KB |
Output is correct |