#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair < int, int > PII;
const int NMAX = 2e5 + 5, KMAX = 2e5 + 5;
int N, K;
PII A[NMAX];
int T[KMAX];
int q;
PII V[KMAX], Q[KMAX];
vector < PII > Queries[NMAX];
/// Fenwick Tree:
int AIB[KMAX];
static inline int UB (int X)
{
return (X & (-X));
}
static inline void Update_1 (int pos, int val)
{
for(int i = pos; i <= q; i += UB(i))
AIB[i] += val;
return;
}
static inline int Query_1 (int pos)
{
int r = 0;
for(int i = pos; i >= 1; i -= UB(i))
r += AIB[i];
return r;
}
///
/// Segment Tree:
int AINT[4 * KMAX];
static inline void Update (int Node, int a, int b, int pos, int val)
{
if(a == b)
{
AINT[Node] = val;
return;
}
int Mid = (a + b) >> 1;
if(pos <= Mid)
Update(2 * Node, a, Mid, pos, val);
if(pos > Mid)
Update(2 * Node + 1, Mid + 1, b, pos, val);
AINT[Node] = max(AINT[2 * Node], AINT[2 * Node + 1]);
return;
}
static inline int Query (int Node, int a, int b, int qa, int qb)
{
if(qa <= a && b <= qb)
return AINT[Node];
int Mid = (a + b) >> 1;
int p1 = 0, p2 = 0;
if(qa <= Mid)
p1 = Query(2 * Node, a, Mid, qa, qb);
if(qb > Mid)
p2 = Query(2 * Node + 1, Mid + 1, b, qa, qb);
return max(p1, p2);
}
///
static inline void Read ()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
for(int i = 1; i <= N; ++i)
cin >> A[i].first >> A[i].second;
for(int i = 1; i <= K; ++i)
cin >> T[i], V[i] = {T[i], i};
return;
}
auto cmp = [] (PII A, PII B)
{
if(A.first < B.first)
return 1;
if(A.first > B.first)
return 0;
if(A.second > B.second)
return 1;
return 0;
};
static inline void Precalculation ()
{
sort(V + 1, V + K + 1, cmp);
Q[++q] = V[1];
for(int i = 2; i <= K; ++i)
if(V[i].first != V[i - 1].first)
Q[++q] = V[i];
for(int i = 1; i <= q; ++i)
Update(1, 1, q, i, Q[i].second);
return;
}
static inline int CB1 (int X)
{
int Left = 1, Right = q, ans = -1;
while(Left <= Right)
{
int Mid = (Left + Right) >> 1;
if(Q[Mid].first >= X)
{
ans = Mid;
Right = Mid - 1;
}
else
Left = Mid + 1;
}
return ans;
}
static inline int CB2 (int X)
{
int Left = 1, Right = q, ans = -1;
while(Left <= Right)
{
int Mid = (Left + Right) >> 1;
if(Q[Mid].first <= X)
{
ans = Mid;
Left = Mid + 1;
}
else
Right = Mid - 1;
}
return ans;
}
static inline void Solve ()
{
long long ans = 0;
for(int i = 1; i <= N; ++i)
{
int Last_Index = 0;
int Min = min(A[i].first, A[i].second);
int Max = max(A[i].first, A[i].second);
int p1 = CB1(Min);
int p2 = CB2(Max - 1);
if(p1 == -1 || (p1 > 0 && Q[p1].first >= Max))
Last_Index = 0;
else
Last_Index = Query(1, 1, q, p1, p2);
if(Last_Index && A[i].first < A[i].second)
swap(A[i].first, A[i].second);
if(Last_Index == K)
{
ans += 1LL * A[i].first;
continue;
}
Queries[Last_Index + 1].push_back({Max, i});
}
int Total = 0;
for(int i = K; i >= 1; --i)
{
Update_1(CB1(T[i]), 1);
++Total;
if(Queries[i].empty())
continue;
for(auto it : Queries[i])
{
int Val = it.first;
int j = it.second;
int counts = 0;
int B_E = 0;
if(Val <= Q[q].first)
{
int pos_Min = CB2(Val - 1);
int B_E = Query_1(pos_Min);
counts = Total - B_E;
}
if(counts & 1)
swap(A[j].first, A[j].second);
ans += 1LL * A[j].first;
}
}
cout << ans << '\n';
return;
}
int main()
{
Read();
Precalculation();
Solve();
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'void Solve()':
fortune_telling2.cpp:228:17: warning: unused variable 'B_E' [-Wunused-variable]
int B_E = 0;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
19 ms |
5632 KB |
Output is correct |
15 |
Correct |
33 ms |
6136 KB |
Output is correct |
16 |
Correct |
47 ms |
6396 KB |
Output is correct |
17 |
Correct |
61 ms |
7160 KB |
Output is correct |
18 |
Correct |
62 ms |
7032 KB |
Output is correct |
19 |
Correct |
60 ms |
7160 KB |
Output is correct |
20 |
Correct |
64 ms |
7420 KB |
Output is correct |
21 |
Correct |
58 ms |
6904 KB |
Output is correct |
22 |
Correct |
49 ms |
6648 KB |
Output is correct |
23 |
Correct |
52 ms |
6648 KB |
Output is correct |
24 |
Correct |
46 ms |
6908 KB |
Output is correct |
25 |
Correct |
44 ms |
6776 KB |
Output is correct |
26 |
Correct |
54 ms |
7416 KB |
Output is correct |
27 |
Correct |
57 ms |
7288 KB |
Output is correct |
28 |
Correct |
54 ms |
7416 KB |
Output is correct |
29 |
Correct |
55 ms |
7284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
19 ms |
5632 KB |
Output is correct |
15 |
Correct |
33 ms |
6136 KB |
Output is correct |
16 |
Correct |
47 ms |
6396 KB |
Output is correct |
17 |
Correct |
61 ms |
7160 KB |
Output is correct |
18 |
Correct |
62 ms |
7032 KB |
Output is correct |
19 |
Correct |
60 ms |
7160 KB |
Output is correct |
20 |
Correct |
64 ms |
7420 KB |
Output is correct |
21 |
Correct |
58 ms |
6904 KB |
Output is correct |
22 |
Correct |
49 ms |
6648 KB |
Output is correct |
23 |
Correct |
52 ms |
6648 KB |
Output is correct |
24 |
Correct |
46 ms |
6908 KB |
Output is correct |
25 |
Correct |
44 ms |
6776 KB |
Output is correct |
26 |
Correct |
54 ms |
7416 KB |
Output is correct |
27 |
Correct |
57 ms |
7288 KB |
Output is correct |
28 |
Correct |
54 ms |
7416 KB |
Output is correct |
29 |
Correct |
55 ms |
7284 KB |
Output is correct |
30 |
Correct |
135 ms |
11892 KB |
Output is correct |
31 |
Correct |
180 ms |
12668 KB |
Output is correct |
32 |
Correct |
226 ms |
13048 KB |
Output is correct |
33 |
Correct |
334 ms |
14580 KB |
Output is correct |
34 |
Correct |
120 ms |
11820 KB |
Output is correct |
35 |
Correct |
341 ms |
14956 KB |
Output is correct |
36 |
Correct |
338 ms |
14444 KB |
Output is correct |
37 |
Correct |
334 ms |
14824 KB |
Output is correct |
38 |
Correct |
342 ms |
14860 KB |
Output is correct |
39 |
Correct |
341 ms |
15424 KB |
Output is correct |
40 |
Correct |
278 ms |
13304 KB |
Output is correct |
41 |
Correct |
341 ms |
15312 KB |
Output is correct |
42 |
Correct |
327 ms |
15224 KB |
Output is correct |
43 |
Correct |
218 ms |
13304 KB |
Output is correct |
44 |
Correct |
212 ms |
13308 KB |
Output is correct |
45 |
Correct |
219 ms |
13432 KB |
Output is correct |
46 |
Correct |
225 ms |
13872 KB |
Output is correct |
47 |
Correct |
211 ms |
13576 KB |
Output is correct |
48 |
Correct |
292 ms |
15468 KB |
Output is correct |
49 |
Correct |
276 ms |
15704 KB |
Output is correct |