#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
typedef pair<int, int> pii;
const int NMAX = 2e5;
pii card[NMAX + 5];
int N, M, K;
int pos[NMAX + 5], v[NMAX + 5];
int lg2[3 * NMAX + 5], rmq[20][3 * NMAX + 5];
int ordC[NMAX + 5], ordV[NMAX + 5];
class Normalizer
{
public:
vector<int> ord;
map<int, int> mp;
void add(int x) { if(mp.count(x)) return; mp[x]; ord.push_back(x); }
void pre()
{
sort(begin(ord), end(ord));
for(int i = 0; i < ord.size(); i++)
mp[ ord[i] ] = i + 1;
}
int get(int x) { return mp[x]; }
}nrm;
class BinaryIndexedTree
{
public:
int aib[NMAX + 5];
void U(int pos)
{
for(; pos <= M; pos += (pos & (-pos)))
aib[pos]++;
}
int Q(int pos)
{
int ans = 0;
for(; pos > 0; pos -= (pos & (-pos)))
ans += aib[pos];
return ans;
}
}aib;
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
//freopen("1.out", "w", stdout);
#endif
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; i++)
{
int x, y;
scanf("%d%d", &x, &y);
card[i] = {x, y};
nrm.add(x); nrm.add(y);
}
for(int i = 1; i <= M; i++)
{
scanf("%d", &v[i]);
nrm.add(v[i]);
}
nrm.pre();
K = nrm.ord.size();
for(int i = 1; i <= N; i++)
{
card[i].first = nrm.get(card[i].first);
card[i].second = nrm.get(card[i].second);
}
for(int i = 1; i <= M; i++) v[i] = nrm.get(v[i]);
for(int i = 1; i <= M; i++)
rmq[0][ v[i] ] = i;
for(int i = 2; i <= K; i++) lg2[i] = lg2[i >> 1] + 1;
for(int i = 1; (1 << i) <= K; i++)
for(int j = 1; j + (1 << i) - 1 <= K; j++)
rmq[i][j] = max(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
for(int i = 1; i <= N; i++)
{
int st = min(card[i].first, card[i].second), dr = max(card[i].first, card[i].second) - 1;
if(st > dr) { pos[i] = 0; continue; }
int lg = lg2[dr - st + 1];
pos[i] = max(rmq[lg][st], rmq[lg][dr - (1 << lg) + 1]);
}
for(int i = 1; i <= N; i++) ordC[i] = i;
sort(ordC + 1, ordC + N + 1,
[](int a, int b) { return max(card[a].first, card[a].second) > max(card[b].first, card[b].second); });
for(int i = 1; i <= M; i++) ordV[i] = i;
sort(ordV + 1, ordV + M + 1,
[](int a, int b) { return v[a] > v[b]; });
int p = 1;
long long ans = 0LL;
for(int i = 1; i <= N; i++)
{
int id = ordC[i];
int val = max(card[id].first, card[id].second);
while(p <= M && val <= v[ ordV[p] ])
{
aib.U(ordV[p]);
p++;
}
int cnt = aib.Q(M) - aib.Q(pos[id]);
int face = 0;
if(pos[id] == 0)
{
if(cnt % 2 == 1) face = card[id].second;
else face = card[id].first;
}
else
{
if(cnt % 2 == 1) face = min(card[id].first, card[id].second);
else face = max(card[id].first, card[id].second);
}
face = nrm.ord[face - 1];
ans += face;
}
cout << ans << '\n';
return 0;
}
Compilation message
fortune_telling2.cpp: In member function 'void Normalizer::pre()':
fortune_telling2.cpp:27:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ord.size(); i++)
~~^~~~~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:58: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:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &v[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
888 KB |
Output is correct |
4 |
Correct |
5 ms |
888 KB |
Output is correct |
5 |
Correct |
4 ms |
760 KB |
Output is correct |
6 |
Correct |
4 ms |
760 KB |
Output is correct |
7 |
Correct |
4 ms |
760 KB |
Output is correct |
8 |
Correct |
4 ms |
888 KB |
Output is correct |
9 |
Correct |
4 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
888 KB |
Output is correct |
4 |
Correct |
5 ms |
888 KB |
Output is correct |
5 |
Correct |
4 ms |
760 KB |
Output is correct |
6 |
Correct |
4 ms |
760 KB |
Output is correct |
7 |
Correct |
4 ms |
760 KB |
Output is correct |
8 |
Correct |
4 ms |
888 KB |
Output is correct |
9 |
Correct |
4 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
33 ms |
4344 KB |
Output is correct |
15 |
Correct |
81 ms |
8348 KB |
Output is correct |
16 |
Correct |
123 ms |
12580 KB |
Output is correct |
17 |
Correct |
220 ms |
16752 KB |
Output is correct |
18 |
Correct |
208 ms |
16880 KB |
Output is correct |
19 |
Correct |
165 ms |
16840 KB |
Output is correct |
20 |
Correct |
157 ms |
16752 KB |
Output is correct |
21 |
Correct |
184 ms |
16820 KB |
Output is correct |
22 |
Correct |
108 ms |
11568 KB |
Output is correct |
23 |
Correct |
82 ms |
9124 KB |
Output is correct |
24 |
Correct |
72 ms |
7284 KB |
Output is correct |
25 |
Correct |
113 ms |
12912 KB |
Output is correct |
26 |
Correct |
104 ms |
10988 KB |
Output is correct |
27 |
Correct |
117 ms |
11888 KB |
Output is correct |
28 |
Correct |
118 ms |
12040 KB |
Output is correct |
29 |
Correct |
160 ms |
14316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
888 KB |
Output is correct |
4 |
Correct |
5 ms |
888 KB |
Output is correct |
5 |
Correct |
4 ms |
760 KB |
Output is correct |
6 |
Correct |
4 ms |
760 KB |
Output is correct |
7 |
Correct |
4 ms |
760 KB |
Output is correct |
8 |
Correct |
4 ms |
888 KB |
Output is correct |
9 |
Correct |
4 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
33 ms |
4344 KB |
Output is correct |
15 |
Correct |
81 ms |
8348 KB |
Output is correct |
16 |
Correct |
123 ms |
12580 KB |
Output is correct |
17 |
Correct |
220 ms |
16752 KB |
Output is correct |
18 |
Correct |
208 ms |
16880 KB |
Output is correct |
19 |
Correct |
165 ms |
16840 KB |
Output is correct |
20 |
Correct |
157 ms |
16752 KB |
Output is correct |
21 |
Correct |
184 ms |
16820 KB |
Output is correct |
22 |
Correct |
108 ms |
11568 KB |
Output is correct |
23 |
Correct |
82 ms |
9124 KB |
Output is correct |
24 |
Correct |
72 ms |
7284 KB |
Output is correct |
25 |
Correct |
113 ms |
12912 KB |
Output is correct |
26 |
Correct |
104 ms |
10988 KB |
Output is correct |
27 |
Correct |
117 ms |
11888 KB |
Output is correct |
28 |
Correct |
118 ms |
12040 KB |
Output is correct |
29 |
Correct |
160 ms |
14316 KB |
Output is correct |
30 |
Correct |
490 ms |
31620 KB |
Output is correct |
31 |
Correct |
617 ms |
43240 KB |
Output is correct |
32 |
Correct |
1075 ms |
57888 KB |
Output is correct |
33 |
Correct |
1496 ms |
87332 KB |
Output is correct |
34 |
Correct |
348 ms |
28776 KB |
Output is correct |
35 |
Correct |
1747 ms |
87588 KB |
Output is correct |
36 |
Correct |
1673 ms |
87368 KB |
Output is correct |
37 |
Correct |
1689 ms |
87316 KB |
Output is correct |
38 |
Correct |
1674 ms |
87524 KB |
Output is correct |
39 |
Correct |
1754 ms |
87524 KB |
Output is correct |
40 |
Correct |
1485 ms |
87080 KB |
Output is correct |
41 |
Correct |
1641 ms |
87580 KB |
Output is correct |
42 |
Correct |
1738 ms |
87516 KB |
Output is correct |
43 |
Correct |
985 ms |
85212 KB |
Output is correct |
44 |
Correct |
986 ms |
85344 KB |
Output is correct |
45 |
Correct |
1004 ms |
85980 KB |
Output is correct |
46 |
Correct |
739 ms |
46532 KB |
Output is correct |
47 |
Correct |
585 ms |
33812 KB |
Output is correct |
48 |
Correct |
972 ms |
61328 KB |
Output is correct |
49 |
Correct |
971 ms |
61608 KB |
Output is correct |