#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <chrono>
#include <random>
#include <bitset>
#include <utility>
int n, k;
int A[200005], B[200005];
int l[200005], r[200005];
int q[200005];
int BIT[600005];
std::vector <int> Q;
void Add(int id, int value)
{
for(id; id < (int)Q.size(); id += id & -id)
{
BIT[id] += value;
}
}
int Get(int id)
{
int ans = 0;
for(id; id > 0; id -= id & -id)
{
ans += BIT[id];
}
return ans;
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin >> n >> k;
Q.push_back(0);
for(int i = 1; i <= n; i++)
{
std::cin >> A[i] >> B[i];
Q.push_back(A[i]);
Q.push_back(B[i]);
l[i] = 0;
r[i] = k;
}
for(int i = 1; i <= k; i++)
{
std::cin >> q[i];
Q.push_back(q[i]);
}
std::sort(Q.begin(), Q.end());
Q.erase(unique(Q.begin(), Q.end()), Q.end());
for(int i = 1; i <= n; i++)
{
A[i] = lower_bound(Q.begin(), Q.end(), A[i]) - Q.begin();
B[i] = lower_bound(Q.begin(), Q.end(), B[i]) - Q.begin();
}
for(int i = 1; i <= k; i++)
{
q[i] = lower_bound(Q.begin(), Q.end(), q[i]) - Q.begin();
}
for(int turn = 1; turn <= 20; turn++)
{
std::vector <std::vector <int> > Query(k + 1, std::vector <int> ());
for(int i = 1; i <= n; i++)
{
int mid = (l[i] + r[i] + 1) >> 1;
if(l[i] != r[i])
Query[mid].push_back(i);
}
for(int i = 1; i <= Q.size(); i++)
{
BIT[i] = 0;
}
for(int i = k; i > 0; i--)
{
Add(q[i], 1);
for(auto x : Query[i])
{
if(Get(std::min(A[x], B[x]) - 1) != Get(std::max(A[x], B[x]) - 1))
{
l[x] = i;
}
else
{
r[x] = i - 1;
}
}
}
}
for(int i = 1; i <= Q.size(); i++)
{
BIT[i] = 0;
}
long long ans = 0;
std::vector <std::vector <int> > Query(k + 1, std::vector <int> ());
for(int i = 1; i <= n; i++)
{
Query[l[i]].push_back(i);
}
for(int i = k; i >= 0; i--)
{
for(auto x : Query[i])
{
bool turn = (i == 0) ? 0 : A[x] < B[x];
int cnt = k - i - Get(std::min(A[x], B[x]) - 1);
turn ^= (cnt & 1);
if(turn)
{
ans += Q[B[x]];
}
else
{
ans += Q[A[x]];
}
}
if(i)
{
Add(q[i], 1);
}
}
std::cout << ans << '\n';
}
Compilation message
fortune_telling2.cpp: In function 'void Add(int, int)':
fortune_telling2.cpp:26:6: warning: statement has no effect [-Wunused-value]
26 | for(id; id < (int)Q.size(); id += id & -id)
| ^~
fortune_telling2.cpp: In function 'int Get(int)':
fortune_telling2.cpp:35:6: warning: statement has no effect [-Wunused-value]
35 | for(id; id > 0; id -= id & -id)
| ^~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = 1; i <= Q.size(); i++)
| ~~^~~~~~~~~~~
fortune_telling2.cpp:102:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 1; i <= Q.size(); i++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
3 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
3 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
23 ms |
1152 KB |
Output is correct |
15 |
Correct |
47 ms |
1904 KB |
Output is correct |
16 |
Correct |
85 ms |
2668 KB |
Output is correct |
17 |
Correct |
107 ms |
3512 KB |
Output is correct |
18 |
Correct |
102 ms |
3532 KB |
Output is correct |
19 |
Correct |
104 ms |
3492 KB |
Output is correct |
20 |
Correct |
103 ms |
3492 KB |
Output is correct |
21 |
Correct |
99 ms |
3564 KB |
Output is correct |
22 |
Correct |
93 ms |
3308 KB |
Output is correct |
23 |
Correct |
87 ms |
3236 KB |
Output is correct |
24 |
Correct |
79 ms |
3180 KB |
Output is correct |
25 |
Correct |
86 ms |
3512 KB |
Output is correct |
26 |
Correct |
99 ms |
3132 KB |
Output is correct |
27 |
Correct |
108 ms |
3308 KB |
Output is correct |
28 |
Correct |
86 ms |
3308 KB |
Output is correct |
29 |
Correct |
104 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
3 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
23 ms |
1152 KB |
Output is correct |
15 |
Correct |
47 ms |
1904 KB |
Output is correct |
16 |
Correct |
85 ms |
2668 KB |
Output is correct |
17 |
Correct |
107 ms |
3512 KB |
Output is correct |
18 |
Correct |
102 ms |
3532 KB |
Output is correct |
19 |
Correct |
104 ms |
3492 KB |
Output is correct |
20 |
Correct |
103 ms |
3492 KB |
Output is correct |
21 |
Correct |
99 ms |
3564 KB |
Output is correct |
22 |
Correct |
93 ms |
3308 KB |
Output is correct |
23 |
Correct |
87 ms |
3236 KB |
Output is correct |
24 |
Correct |
79 ms |
3180 KB |
Output is correct |
25 |
Correct |
86 ms |
3512 KB |
Output is correct |
26 |
Correct |
99 ms |
3132 KB |
Output is correct |
27 |
Correct |
108 ms |
3308 KB |
Output is correct |
28 |
Correct |
86 ms |
3308 KB |
Output is correct |
29 |
Correct |
104 ms |
3436 KB |
Output is correct |
30 |
Correct |
239 ms |
7912 KB |
Output is correct |
31 |
Correct |
314 ms |
9612 KB |
Output is correct |
32 |
Correct |
408 ms |
11508 KB |
Output is correct |
33 |
Correct |
626 ms |
15708 KB |
Output is correct |
34 |
Correct |
217 ms |
7528 KB |
Output is correct |
35 |
Correct |
620 ms |
15620 KB |
Output is correct |
36 |
Correct |
628 ms |
21368 KB |
Output is correct |
37 |
Correct |
615 ms |
21380 KB |
Output is correct |
38 |
Correct |
610 ms |
21340 KB |
Output is correct |
39 |
Correct |
626 ms |
21508 KB |
Output is correct |
40 |
Correct |
599 ms |
21204 KB |
Output is correct |
41 |
Correct |
609 ms |
21260 KB |
Output is correct |
42 |
Correct |
608 ms |
21260 KB |
Output is correct |
43 |
Correct |
499 ms |
20876 KB |
Output is correct |
44 |
Correct |
499 ms |
20712 KB |
Output is correct |
45 |
Correct |
509 ms |
20572 KB |
Output is correct |
46 |
Correct |
478 ms |
18308 KB |
Output is correct |
47 |
Correct |
464 ms |
17676 KB |
Output is correct |
48 |
Correct |
528 ms |
20572 KB |
Output is correct |
49 |
Correct |
487 ms |
20612 KB |
Output is correct |