#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) begin(x), end(x)
using namespace std;
using ll=long long;
using pii=pair<int, int>;
const int N=2e5+13;
int qr[N];
struct Segtree{
vector<int> st[N << 2];
void build(int id, int l, int r){
if(l==r){
st[id].push_back(qr[l]);
return;
}
int mid=l+r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid+1, r);
st[id].resize(st[id << 1].size() + st[id << 1 | 1].size());
merge(all(st[id << 1]), all(st[id << 1 | 1]), begin(st[id]));
}
int get(int id, int l, int r, int u, int v, int x, int y){
if(l > v || r < u) return 0;
if(u<=l && r<=v){
return upper_bound(all(st[id]), y)-lower_bound(all(st[id]), x);
}
int mid=l+r >> 1;
int get1=get(id << 1, l, mid, u, v, x, y);
int get2=get(id << 1 | 1, mid+1, r, u, v, x, y);
return get1+get2;
}
int walk(int id, int l, int r, int x, int y){
if(upper_bound(all(st[id]), y) == lower_bound(all(st[id]), x)) return -1;
if(l==r) return l;
int mid=l+r >> 1;
int sus=walk(id << 1 | 1, mid+1, r, x, y);
if(sus!=-1) return sus;
return walk(id << 1, l, mid, x, y);
}
} st;
int n, q;
pii a[N];
bool sus[N];
ll res=0;
void scan(){
cin >> n >> q;
for(int i=1; i<=n; i++){
cin >> a[i].fi >> a[i].se;
if(a[i].fi > a[i].se){
swap(a[i].fi, a[i].se);
sus[i]=1;
}
}
for(int i=1; i<=q; i++){
cin >> qr[i];
}
st.build(1, 1, q);
}
void solve(){
for(int i=1; i<=n; i++){
if(a[i].fi == a[i].se){
res += a[i].fi;
continue;
}
int id=st.walk(1, 1, q, a[i].fi, a[i].se-1);
// cerr << i << ' ' << id << '\n';
if(id==-1){
int cnt=st.get(1, 1, q, 1, q, a[i].se, 1e9);
//cerr << "? " << a[i].se << ' ' << cnt << '\n';
cnt%=2;
sus[i]^=cnt;
if(sus[i]) res += a[i].se;
else res += a[i].fi;
continue;
}
int cnt=st.get(1, 1, q, id+1, q, a[i].se, 1e9);
if(cnt%2==0){
res += a[i].se;
//cout << i << ' ' << a[i].se << '\n';
}
else{
res += a[i].fi;
//cout << i << ' ' << a[i].fi << '\n';
}
}
cout << res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
scan();
solve();
}
Compilation message
fortune_telling2.cpp: In member function 'void Segtree::build(int, int, int)':
fortune_telling2.cpp:21:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int mid=l+r >> 1;
| ~^~
fortune_telling2.cpp: In member function 'int Segtree::get(int, int, int, int, int, int, int)':
fortune_telling2.cpp:33:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int mid=l+r >> 1;
| ~^~
fortune_telling2.cpp: In member function 'int Segtree::walk(int, int, int, int, int)':
fortune_telling2.cpp:42:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int mid=l+r >> 1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
5 ms |
21072 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Correct |
6 ms |
21072 KB |
Output is correct |
8 |
Correct |
4 ms |
21072 KB |
Output is correct |
9 |
Correct |
4 ms |
21244 KB |
Output is correct |
10 |
Correct |
4 ms |
21072 KB |
Output is correct |
11 |
Correct |
4 ms |
21072 KB |
Output is correct |
12 |
Correct |
4 ms |
21072 KB |
Output is correct |
13 |
Correct |
4 ms |
21072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
5 ms |
21072 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Correct |
6 ms |
21072 KB |
Output is correct |
8 |
Correct |
4 ms |
21072 KB |
Output is correct |
9 |
Correct |
4 ms |
21244 KB |
Output is correct |
10 |
Correct |
4 ms |
21072 KB |
Output is correct |
11 |
Correct |
4 ms |
21072 KB |
Output is correct |
12 |
Correct |
4 ms |
21072 KB |
Output is correct |
13 |
Correct |
4 ms |
21072 KB |
Output is correct |
14 |
Correct |
17 ms |
21840 KB |
Output is correct |
15 |
Correct |
34 ms |
23080 KB |
Output is correct |
16 |
Correct |
52 ms |
24144 KB |
Output is correct |
17 |
Correct |
73 ms |
25424 KB |
Output is correct |
18 |
Correct |
83 ms |
25328 KB |
Output is correct |
19 |
Correct |
69 ms |
25424 KB |
Output is correct |
20 |
Correct |
78 ms |
25424 KB |
Output is correct |
21 |
Correct |
58 ms |
25424 KB |
Output is correct |
22 |
Correct |
31 ms |
25168 KB |
Output is correct |
23 |
Correct |
33 ms |
25168 KB |
Output is correct |
24 |
Correct |
34 ms |
25168 KB |
Output is correct |
25 |
Correct |
29 ms |
25168 KB |
Output is correct |
26 |
Correct |
55 ms |
25424 KB |
Output is correct |
27 |
Correct |
85 ms |
25440 KB |
Output is correct |
28 |
Correct |
55 ms |
25420 KB |
Output is correct |
29 |
Correct |
66 ms |
25376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
5 ms |
21072 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Correct |
6 ms |
21072 KB |
Output is correct |
8 |
Correct |
4 ms |
21072 KB |
Output is correct |
9 |
Correct |
4 ms |
21244 KB |
Output is correct |
10 |
Correct |
4 ms |
21072 KB |
Output is correct |
11 |
Correct |
4 ms |
21072 KB |
Output is correct |
12 |
Correct |
4 ms |
21072 KB |
Output is correct |
13 |
Correct |
4 ms |
21072 KB |
Output is correct |
14 |
Correct |
17 ms |
21840 KB |
Output is correct |
15 |
Correct |
34 ms |
23080 KB |
Output is correct |
16 |
Correct |
52 ms |
24144 KB |
Output is correct |
17 |
Correct |
73 ms |
25424 KB |
Output is correct |
18 |
Correct |
83 ms |
25328 KB |
Output is correct |
19 |
Correct |
69 ms |
25424 KB |
Output is correct |
20 |
Correct |
78 ms |
25424 KB |
Output is correct |
21 |
Correct |
58 ms |
25424 KB |
Output is correct |
22 |
Correct |
31 ms |
25168 KB |
Output is correct |
23 |
Correct |
33 ms |
25168 KB |
Output is correct |
24 |
Correct |
34 ms |
25168 KB |
Output is correct |
25 |
Correct |
29 ms |
25168 KB |
Output is correct |
26 |
Correct |
55 ms |
25424 KB |
Output is correct |
27 |
Correct |
85 ms |
25440 KB |
Output is correct |
28 |
Correct |
55 ms |
25420 KB |
Output is correct |
29 |
Correct |
66 ms |
25376 KB |
Output is correct |
30 |
Correct |
94 ms |
45384 KB |
Output is correct |
31 |
Correct |
189 ms |
45264 KB |
Output is correct |
32 |
Correct |
280 ms |
45452 KB |
Output is correct |
33 |
Correct |
497 ms |
45456 KB |
Output is correct |
34 |
Correct |
68 ms |
45128 KB |
Output is correct |
35 |
Correct |
502 ms |
45384 KB |
Output is correct |
36 |
Correct |
487 ms |
45548 KB |
Output is correct |
37 |
Correct |
502 ms |
45548 KB |
Output is correct |
38 |
Correct |
477 ms |
45384 KB |
Output is correct |
39 |
Correct |
515 ms |
45384 KB |
Output is correct |
40 |
Correct |
355 ms |
45544 KB |
Output is correct |
41 |
Correct |
464 ms |
45384 KB |
Output is correct |
42 |
Correct |
500 ms |
45456 KB |
Output is correct |
43 |
Correct |
146 ms |
45128 KB |
Output is correct |
44 |
Correct |
151 ms |
45384 KB |
Output is correct |
45 |
Correct |
151 ms |
45392 KB |
Output is correct |
46 |
Correct |
175 ms |
45128 KB |
Output is correct |
47 |
Correct |
212 ms |
45380 KB |
Output is correct |
48 |
Correct |
391 ms |
45364 KB |
Output is correct |
49 |
Correct |
350 ms |
45384 KB |
Output is correct |