#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
int n, m;
const int maxn = 2e5+5;
struct node
{
int val;
node *left, *right;
node(int a, node *b, node *c)
{
val = a;
left = b;
right = c;
}
node* insert(int x, int L = 0, int R = m-1)
{
if(L<= x && x<= R)
{
if(L == R) return new node(val+1, NULL, NULL);
int M = (L+R)/2;
return new node(val+1, left->insert(x, L, M), right->insert(x, M+1, R));
}
return this;
}
int ask(int i, int j, int L = 0, int R = m-1)
{
if(i> R || j< L) return 0;
if(i<= L && R<= j) return val;
int M = (L+R)/2;
int x = left->ask(i, j, L, M);
int y = right->ask(i, j, M+1, R);
return x+y;
}
};
node* zero = new node(0, NULL, NULL);
int A[maxn], B[maxn];
int op[maxn];
node* ps[maxn];
vector< ii > foo;
int main()
{
scanf("%d %d", &n, &m);
for(int i = 1; i<= n; i++) scanf("%d %d", &A[i], &B[i]);
zero->left = zero->right = zero;
for(int i = 0; i< m; i++)
{
scanf("%d", &op[i]);
foo.pb(ii(op[i], i));
}
sort(foo.begin(), foo.end());
ps[m] = zero;
for(int i = m-1; i>= 0; i--)
{
ps[i] = ps[i+1];
ps[i] = ps[i]->insert(foo[i].Y);
// printf("insert %d\n", foo[i].Y);
}
ll ans = 0;
for(int i = 1; i<= n; i++)
{
int a = A[i], b = B[i];
if(a> b) swap(a, b);
int pa = lower_bound(foo.begin(), foo.end(), ii(a, 0))-foo.begin();
int pb = lower_bound(foo.begin(), foo.end(), ii(b, 0))-foo.begin();
// printf("pa pb %d %d\n", pa, pb);
int lo = 0, hi = m-1;
while(lo< hi)
{
int mid = (lo+hi+1)/2;
if((ps[pa])->ask(mid, m-1) == (ps[pb])->ask(mid, m-1)) hi = mid-1;
else lo = mid;
}
// printf("lo = %d\n", lo);
if(ps[pa]->ask(lo, m-1) == ps[pb]->ask(lo, m-1))
{
int swaps = ps[pa]->ask(0, m-1);
if(swaps%2) ans += B[i];
else ans += A[i];
continue;
}
int swaps = ps[pa]->ask(lo+1, m-1);
// printf("swaps %d\n", swaps);
if(swaps%2) ans += a;
else ans += b;
}
printf("%lld\n", ans);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:56:7: 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:57:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i<= n; i++) scanf("%d %d", &A[i], &B[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &op[i]);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
4 ms |
768 KB |
Output is correct |
4 |
Correct |
4 ms |
768 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
4 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
768 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
4 ms |
768 KB |
Output is correct |
11 |
Correct |
4 ms |
768 KB |
Output is correct |
12 |
Correct |
4 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
4 ms |
768 KB |
Output is correct |
4 |
Correct |
4 ms |
768 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
4 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
768 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
4 ms |
768 KB |
Output is correct |
11 |
Correct |
4 ms |
768 KB |
Output is correct |
12 |
Correct |
4 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
896 KB |
Output is correct |
14 |
Correct |
33 ms |
5496 KB |
Output is correct |
15 |
Correct |
69 ms |
11112 KB |
Output is correct |
16 |
Correct |
122 ms |
17012 KB |
Output is correct |
17 |
Correct |
163 ms |
23152 KB |
Output is correct |
18 |
Correct |
152 ms |
23280 KB |
Output is correct |
19 |
Correct |
152 ms |
23024 KB |
Output is correct |
20 |
Correct |
166 ms |
23140 KB |
Output is correct |
21 |
Correct |
134 ms |
22996 KB |
Output is correct |
22 |
Correct |
117 ms |
22640 KB |
Output is correct |
23 |
Correct |
137 ms |
22680 KB |
Output is correct |
24 |
Correct |
129 ms |
22648 KB |
Output is correct |
25 |
Correct |
120 ms |
22684 KB |
Output is correct |
26 |
Correct |
149 ms |
23004 KB |
Output is correct |
27 |
Correct |
190 ms |
23152 KB |
Output is correct |
28 |
Correct |
163 ms |
23028 KB |
Output is correct |
29 |
Correct |
221 ms |
23152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
4 ms |
768 KB |
Output is correct |
4 |
Correct |
4 ms |
768 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
4 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
768 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
4 ms |
768 KB |
Output is correct |
11 |
Correct |
4 ms |
768 KB |
Output is correct |
12 |
Correct |
4 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
896 KB |
Output is correct |
14 |
Correct |
33 ms |
5496 KB |
Output is correct |
15 |
Correct |
69 ms |
11112 KB |
Output is correct |
16 |
Correct |
122 ms |
17012 KB |
Output is correct |
17 |
Correct |
163 ms |
23152 KB |
Output is correct |
18 |
Correct |
152 ms |
23280 KB |
Output is correct |
19 |
Correct |
152 ms |
23024 KB |
Output is correct |
20 |
Correct |
166 ms |
23140 KB |
Output is correct |
21 |
Correct |
134 ms |
22996 KB |
Output is correct |
22 |
Correct |
117 ms |
22640 KB |
Output is correct |
23 |
Correct |
137 ms |
22680 KB |
Output is correct |
24 |
Correct |
129 ms |
22648 KB |
Output is correct |
25 |
Correct |
120 ms |
22684 KB |
Output is correct |
26 |
Correct |
149 ms |
23004 KB |
Output is correct |
27 |
Correct |
190 ms |
23152 KB |
Output is correct |
28 |
Correct |
163 ms |
23028 KB |
Output is correct |
29 |
Correct |
221 ms |
23152 KB |
Output is correct |
30 |
Correct |
394 ms |
123440 KB |
Output is correct |
31 |
Correct |
565 ms |
124028 KB |
Output is correct |
32 |
Correct |
727 ms |
124488 KB |
Output is correct |
33 |
Correct |
1491 ms |
125228 KB |
Output is correct |
34 |
Correct |
424 ms |
123228 KB |
Output is correct |
35 |
Correct |
1179 ms |
125264 KB |
Output is correct |
36 |
Correct |
1166 ms |
125228 KB |
Output is correct |
37 |
Correct |
1140 ms |
125072 KB |
Output is correct |
38 |
Correct |
1444 ms |
124900 KB |
Output is correct |
39 |
Correct |
1442 ms |
124864 KB |
Output is correct |
40 |
Correct |
1085 ms |
124900 KB |
Output is correct |
41 |
Correct |
1151 ms |
124904 KB |
Output is correct |
42 |
Correct |
1146 ms |
124924 KB |
Output is correct |
43 |
Correct |
723 ms |
124904 KB |
Output is correct |
44 |
Correct |
717 ms |
124976 KB |
Output is correct |
45 |
Correct |
713 ms |
125028 KB |
Output is correct |
46 |
Correct |
899 ms |
124900 KB |
Output is correct |
47 |
Correct |
806 ms |
125028 KB |
Output is correct |
48 |
Correct |
1209 ms |
125028 KB |
Output is correct |
49 |
Correct |
1200 ms |
124872 KB |
Output is correct |