#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[200005];
void Add(int id, int value)
{
for(id; id <= k; 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;
for(int i = 1; i <= n; i++)
{
std::cin >> A[i] >> B[i];
l[i] = 0;
r[i] = k;
}
for(int i = 1; i <= k; i++)
{
std::cin >> q[i];
}
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);
}
std::set <int> S;
for(int i = k; i > 0; i--)
{
S.insert(q[i]);
for(auto x : Query[i])
{
auto value = S.lower_bound(std::min(A[x], B[x]));
if(value != S.end() and *value < std::max(A[x], B[x]))
{
l[x] = i;
}
else
{
r[x] = i - 1;
}
}
}
}
long long ans = 0;
std::vector <std::vector <int> > Query(k + 1, std::vector <int> ());
std::vector <int> Q;
for(int i = 1; i <= k; i++)
{
Q.push_back(q[i]);
}
Q.push_back(0);
std::sort(Q.begin(), Q.end());
Q.erase(unique(Q.begin(), Q.end()), Q.end());
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(lower_bound(Q.begin(), Q.end(), std::max(A[x], B[x])) - Q.begin() - 1);
turn ^= (cnt & 1);
if(turn)
{
ans += B[x];
}
else
{
ans += A[x];
}
}
if(i)
{
Add(lower_bound(Q.begin(), Q.end(), q[i]) - Q.begin(), 1);
}
}
std::cout << ans << '\n';
}
Compilation message
fortune_telling2.cpp: In function 'void Add(int, int)':
fortune_telling2.cpp:25:6: warning: statement has no effect [-Wunused-value]
25 | for(id; id <= k; id += id & -id)
| ^~
fortune_telling2.cpp: In function 'int Get(int)':
fortune_telling2.cpp:34:6: warning: statement has no effect [-Wunused-value]
34 | for(id; id > 0; id -= id & -id)
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
492 KB |
Output is correct |
2 |
Correct |
5 ms |
492 KB |
Output is correct |
3 |
Correct |
5 ms |
492 KB |
Output is correct |
4 |
Correct |
5 ms |
492 KB |
Output is correct |
5 |
Correct |
5 ms |
492 KB |
Output is correct |
6 |
Correct |
5 ms |
492 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
492 KB |
Output is correct |
9 |
Correct |
5 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
492 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
5 ms |
492 KB |
Output is correct |
13 |
Correct |
6 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
492 KB |
Output is correct |
2 |
Correct |
5 ms |
492 KB |
Output is correct |
3 |
Correct |
5 ms |
492 KB |
Output is correct |
4 |
Correct |
5 ms |
492 KB |
Output is correct |
5 |
Correct |
5 ms |
492 KB |
Output is correct |
6 |
Correct |
5 ms |
492 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
492 KB |
Output is correct |
9 |
Correct |
5 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
492 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
5 ms |
492 KB |
Output is correct |
13 |
Correct |
6 ms |
492 KB |
Output is correct |
14 |
Correct |
60 ms |
1664 KB |
Output is correct |
15 |
Correct |
136 ms |
3028 KB |
Output is correct |
16 |
Correct |
222 ms |
4316 KB |
Output is correct |
17 |
Correct |
304 ms |
5668 KB |
Output is correct |
18 |
Correct |
308 ms |
5540 KB |
Output is correct |
19 |
Correct |
304 ms |
5576 KB |
Output is correct |
20 |
Correct |
309 ms |
5700 KB |
Output is correct |
21 |
Correct |
294 ms |
5540 KB |
Output is correct |
22 |
Correct |
247 ms |
4644 KB |
Output is correct |
23 |
Correct |
239 ms |
4516 KB |
Output is correct |
24 |
Correct |
247 ms |
4580 KB |
Output is correct |
25 |
Correct |
255 ms |
4900 KB |
Output is correct |
26 |
Correct |
276 ms |
5348 KB |
Output is correct |
27 |
Correct |
324 ms |
5796 KB |
Output is correct |
28 |
Correct |
270 ms |
5668 KB |
Output is correct |
29 |
Correct |
391 ms |
5668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
492 KB |
Output is correct |
2 |
Correct |
5 ms |
492 KB |
Output is correct |
3 |
Correct |
5 ms |
492 KB |
Output is correct |
4 |
Correct |
5 ms |
492 KB |
Output is correct |
5 |
Correct |
5 ms |
492 KB |
Output is correct |
6 |
Correct |
5 ms |
492 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
492 KB |
Output is correct |
9 |
Correct |
5 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
492 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
5 ms |
492 KB |
Output is correct |
13 |
Correct |
6 ms |
492 KB |
Output is correct |
14 |
Correct |
60 ms |
1664 KB |
Output is correct |
15 |
Correct |
136 ms |
3028 KB |
Output is correct |
16 |
Correct |
222 ms |
4316 KB |
Output is correct |
17 |
Correct |
304 ms |
5668 KB |
Output is correct |
18 |
Correct |
308 ms |
5540 KB |
Output is correct |
19 |
Correct |
304 ms |
5576 KB |
Output is correct |
20 |
Correct |
309 ms |
5700 KB |
Output is correct |
21 |
Correct |
294 ms |
5540 KB |
Output is correct |
22 |
Correct |
247 ms |
4644 KB |
Output is correct |
23 |
Correct |
239 ms |
4516 KB |
Output is correct |
24 |
Correct |
247 ms |
4580 KB |
Output is correct |
25 |
Correct |
255 ms |
4900 KB |
Output is correct |
26 |
Correct |
276 ms |
5348 KB |
Output is correct |
27 |
Correct |
324 ms |
5796 KB |
Output is correct |
28 |
Correct |
270 ms |
5668 KB |
Output is correct |
29 |
Correct |
391 ms |
5668 KB |
Output is correct |
30 |
Correct |
2793 ms |
18540 KB |
Output is correct |
31 |
Correct |
2664 ms |
20016 KB |
Output is correct |
32 |
Correct |
2844 ms |
22192 KB |
Output is correct |
33 |
Correct |
2987 ms |
26156 KB |
Output is correct |
34 |
Correct |
2741 ms |
17996 KB |
Output is correct |
35 |
Execution timed out |
3070 ms |
26272 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |