#include <cstdio>
#include <vector>
#include <algorithm>
#define PB push_back
using namespace std;
typedef long long ll;
typedef vector < int > vi;
const int N = 4e5 + 500;
const int OFF = (1 << 19);
int n, q, A[N], B[N], Q[N];
int loga[N], l_siz, izb[N];
vi saz, T[2 * OFF];
void add(int x){
l_siz++;
for(x += 5 ; x < N ; x += x & -x)
loga[x]++;
}
int query(int x){
x--;
int ret = 0;
for(x += 5 ; x ; x -= x & -x)
ret += loga[x];
return l_siz - ret;
}
ll sol = 0;
void izbaci(int i){
if(izb[i]) return;
izb[i] = 1;
if(query(B[i]) & 1)
sol += saz[min(A[i], B[i])];
else
sol += saz[max(A[i], B[i])];
}
void dodaj(int i, int a, int b, int lo, int hi, int x){
if(lo <= a && b <= hi){
T[i].PB(x); return;
}
if(a > hi || b < lo) return;
dodaj(2 * i, a, (a + b) / 2, lo, hi, x);
dodaj(2 * i + 1, (a + b) / 2 + 1, b, lo, hi, x);
}
void unisti(int x){
x += OFF;
for(; x ; x /= 2){
for(int y : T[x])
izbaci(y);
T[x].clear();
}
}
int main(){
scanf("%d%d", &n, &q);
for(int i = 0;i < n;i++){
scanf("%d%d", A + i, B + i);
saz.PB(A[i]), saz.PB(B[i]);
}
for(int i = 0;i < q;i++){
scanf("%d", Q + i);
saz.PB(Q[i]);
}
sort(saz.begin(), saz.end());
saz.erase(unique(saz.begin(), saz.end()), saz.end());
for(int i = 0;i < n;i++){
A[i] = lower_bound(saz.begin(), saz.end(), A[i]) - saz.begin();
B[i] = lower_bound(saz.begin(), saz.end(), B[i]) - saz.begin();
}
for(int i = 0;i < q;i++){
Q[i] = lower_bound(saz.begin(), saz.end(), Q[i]) - saz.begin();
}
for(int i = 0;i < n;i++){
if(A[i] == B[i]){
izb[i] = 1;
sol += saz[A[i]]; continue;
}
dodaj(1, 0, OFF - 1, min(A[i], B[i]), max(A[i], B[i]) - 1, i);
}
for(int i = q - 1;i >= 0;i--){
unisti(Q[i]); add(Q[i]);
}
for(int i = 0;i < n;i++){
if(!izb[i]){
if(query(B[i]) & 1)
sol += saz[B[i]];
else
sol += saz[A[i]];
}
}
printf("%lld\n", sol);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
63 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d%d", A + i, B + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%d", Q + i);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24960 KB |
Output is correct |
2 |
Correct |
18 ms |
25088 KB |
Output is correct |
3 |
Correct |
19 ms |
25088 KB |
Output is correct |
4 |
Correct |
21 ms |
25088 KB |
Output is correct |
5 |
Correct |
19 ms |
25088 KB |
Output is correct |
6 |
Correct |
20 ms |
25088 KB |
Output is correct |
7 |
Correct |
19 ms |
25088 KB |
Output is correct |
8 |
Correct |
19 ms |
25088 KB |
Output is correct |
9 |
Correct |
19 ms |
25088 KB |
Output is correct |
10 |
Correct |
18 ms |
25088 KB |
Output is correct |
11 |
Correct |
19 ms |
25088 KB |
Output is correct |
12 |
Correct |
18 ms |
25088 KB |
Output is correct |
13 |
Correct |
18 ms |
25088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24960 KB |
Output is correct |
2 |
Correct |
18 ms |
25088 KB |
Output is correct |
3 |
Correct |
19 ms |
25088 KB |
Output is correct |
4 |
Correct |
21 ms |
25088 KB |
Output is correct |
5 |
Correct |
19 ms |
25088 KB |
Output is correct |
6 |
Correct |
20 ms |
25088 KB |
Output is correct |
7 |
Correct |
19 ms |
25088 KB |
Output is correct |
8 |
Correct |
19 ms |
25088 KB |
Output is correct |
9 |
Correct |
19 ms |
25088 KB |
Output is correct |
10 |
Correct |
18 ms |
25088 KB |
Output is correct |
11 |
Correct |
19 ms |
25088 KB |
Output is correct |
12 |
Correct |
18 ms |
25088 KB |
Output is correct |
13 |
Correct |
18 ms |
25088 KB |
Output is correct |
14 |
Correct |
41 ms |
26752 KB |
Output is correct |
15 |
Correct |
72 ms |
28660 KB |
Output is correct |
16 |
Correct |
119 ms |
30572 KB |
Output is correct |
17 |
Correct |
174 ms |
32484 KB |
Output is correct |
18 |
Correct |
152 ms |
32496 KB |
Output is correct |
19 |
Correct |
147 ms |
32624 KB |
Output is correct |
20 |
Correct |
146 ms |
32240 KB |
Output is correct |
21 |
Correct |
136 ms |
31472 KB |
Output is correct |
22 |
Correct |
95 ms |
30704 KB |
Output is correct |
23 |
Correct |
90 ms |
30448 KB |
Output is correct |
24 |
Correct |
88 ms |
30064 KB |
Output is correct |
25 |
Correct |
94 ms |
30816 KB |
Output is correct |
26 |
Correct |
91 ms |
28928 KB |
Output is correct |
27 |
Correct |
108 ms |
29168 KB |
Output is correct |
28 |
Correct |
97 ms |
29420 KB |
Output is correct |
29 |
Correct |
94 ms |
27888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24960 KB |
Output is correct |
2 |
Correct |
18 ms |
25088 KB |
Output is correct |
3 |
Correct |
19 ms |
25088 KB |
Output is correct |
4 |
Correct |
21 ms |
25088 KB |
Output is correct |
5 |
Correct |
19 ms |
25088 KB |
Output is correct |
6 |
Correct |
20 ms |
25088 KB |
Output is correct |
7 |
Correct |
19 ms |
25088 KB |
Output is correct |
8 |
Correct |
19 ms |
25088 KB |
Output is correct |
9 |
Correct |
19 ms |
25088 KB |
Output is correct |
10 |
Correct |
18 ms |
25088 KB |
Output is correct |
11 |
Correct |
19 ms |
25088 KB |
Output is correct |
12 |
Correct |
18 ms |
25088 KB |
Output is correct |
13 |
Correct |
18 ms |
25088 KB |
Output is correct |
14 |
Correct |
41 ms |
26752 KB |
Output is correct |
15 |
Correct |
72 ms |
28660 KB |
Output is correct |
16 |
Correct |
119 ms |
30572 KB |
Output is correct |
17 |
Correct |
174 ms |
32484 KB |
Output is correct |
18 |
Correct |
152 ms |
32496 KB |
Output is correct |
19 |
Correct |
147 ms |
32624 KB |
Output is correct |
20 |
Correct |
146 ms |
32240 KB |
Output is correct |
21 |
Correct |
136 ms |
31472 KB |
Output is correct |
22 |
Correct |
95 ms |
30704 KB |
Output is correct |
23 |
Correct |
90 ms |
30448 KB |
Output is correct |
24 |
Correct |
88 ms |
30064 KB |
Output is correct |
25 |
Correct |
94 ms |
30816 KB |
Output is correct |
26 |
Correct |
91 ms |
28928 KB |
Output is correct |
27 |
Correct |
108 ms |
29168 KB |
Output is correct |
28 |
Correct |
97 ms |
29420 KB |
Output is correct |
29 |
Correct |
94 ms |
27888 KB |
Output is correct |
30 |
Correct |
178 ms |
29808 KB |
Output is correct |
31 |
Correct |
352 ms |
40548 KB |
Output is correct |
32 |
Correct |
554 ms |
50148 KB |
Output is correct |
33 |
Runtime error |
779 ms |
124256 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |