#include<bits/stdc++.h>
using namespace std;
#define Mask(i) (1 << i)
#define ll long long
const int ar = 2e5 + 5;
const int N = 6e5 + 5;
struct query {
int x, v, pos;
bool operator < (query &other) {
if(x != other.x) return x > other.x;
return pos < other.pos;
}
}; vector<query> queries;
int n, q;
int a[ar][2];
int pos[ar], t[ar];
vector<int> Numbers;
int st[N << 2], cnt[ar];
int Max[N][21];
int ok[N];
int getmax(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return max(Max[l][k], Max[r - Mask(k) + 1][k]);
}
int ID(int x) {
return lower_bound(Numbers.begin(), Numbers.end(), x) - Numbers.begin() + 1;
}
void update(int u, int val) {
int id = 1, l = 1, r = Numbers.size();
while(l < r) {
int mid = l + r >> 1;
if(u > mid) id = id << 1 | 1, l = mid + 1;
else id = id << 1, r = mid;
}
st[id] += val;
while(id > 1) id = id >> 1, st[id] = st[id << 1] + st[id << 1 | 1];
}
int get(int id, int l, int r, int u, int v) {
if(u > r || v < l) return 0;
if(u <= l && r <= v) return st[id];
int mid = l + r >> 1;
int tmp1 = get(id << 1, l, mid, u, v);
int tmp2 = get(id << 1 | 1, mid + 1, r, u, v);
return tmp1 + tmp2;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> a[i][0] >> a[i][1];
Numbers.push_back(a[i][0]);
Numbers.push_back(a[i][1]);
}
for(int i = 1; i <= q; ++i) {
cin >> t[i];
Numbers.push_back(t[i]);
}
sort(Numbers.begin(), Numbers.end());
Numbers.resize(unique(Numbers.begin(), Numbers.end()) - Numbers.begin());
for(int i = 1; i <= q; ++i) {
int vt = ID(t[i]);
Max[vt][0] = i;
}
for(int j = 1; j < 21; j++) {
for(int i = 1; i <= (int)Numbers.size() - Mask(j) + 1; i++) {
Max[i][j] = max(Max[i][j - 1], Max[i + Mask(j - 1)][j - 1]);
}
}
for(int i = 1; i <= n; ++i) {
int L = ID(a[i][0]), R = ID(a[i][1]);
if(L > R) swap(L, R);
pos[i] = getmax(L, R - 1);
if(pos[i]) if(a[i][0] < a[i][1]) cnt[i] = 1;
}
for(int i = 1; i <= n; ++i) {
queries.push_back({max(a[i][0], a[i][1]), i, 1});
}
for(int i = 1; i <= q; ++i) {
queries.push_back({t[i], i, 0});
}
sort(queries.begin(), queries.end());
for(auto it : queries) {
if(it.pos == 0) update(it.v, 1);
else cnt[it.v] += get(1, 1, Numbers.size(), pos[it.v] + 1, Numbers.size());
}
ll ans = 0;
for(int i = 1; i <= n; ++i) {
ans += a[i][cnt[i] & 1];
}
cout << ans << '\n';
}
Compilation message
fortune_telling2.cpp: In function 'void update(int, int)':
fortune_telling2.cpp:36:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int get(int, int, int, int, int)':
fortune_telling2.cpp:47:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:74:59: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
74 | Max[i][j] = max(Max[i][j - 1], Max[i + Mask(j - 1)][j - 1]);
| ~~^~~
fortune_telling2.cpp:3:23: note: in definition of macro 'Mask'
3 | #define Mask(i) (1 << i)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6660 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6628 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6660 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6628 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6624 KB |
Output is correct |
14 |
Correct |
12 ms |
9564 KB |
Output is correct |
15 |
Correct |
24 ms |
12444 KB |
Output is correct |
16 |
Correct |
35 ms |
15060 KB |
Output is correct |
17 |
Correct |
49 ms |
18256 KB |
Output is correct |
18 |
Correct |
51 ms |
18240 KB |
Output is correct |
19 |
Correct |
48 ms |
18252 KB |
Output is correct |
20 |
Correct |
50 ms |
18244 KB |
Output is correct |
21 |
Correct |
48 ms |
18252 KB |
Output is correct |
22 |
Correct |
36 ms |
15696 KB |
Output is correct |
23 |
Correct |
36 ms |
13652 KB |
Output is correct |
24 |
Correct |
36 ms |
11528 KB |
Output is correct |
25 |
Correct |
42 ms |
16204 KB |
Output is correct |
26 |
Correct |
37 ms |
13904 KB |
Output is correct |
27 |
Correct |
44 ms |
16216 KB |
Output is correct |
28 |
Correct |
40 ms |
16460 KB |
Output is correct |
29 |
Correct |
48 ms |
18160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6660 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Correct |
2 ms |
6628 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6624 KB |
Output is correct |
14 |
Correct |
12 ms |
9564 KB |
Output is correct |
15 |
Correct |
24 ms |
12444 KB |
Output is correct |
16 |
Correct |
35 ms |
15060 KB |
Output is correct |
17 |
Correct |
49 ms |
18256 KB |
Output is correct |
18 |
Correct |
51 ms |
18240 KB |
Output is correct |
19 |
Correct |
48 ms |
18252 KB |
Output is correct |
20 |
Correct |
50 ms |
18244 KB |
Output is correct |
21 |
Correct |
48 ms |
18252 KB |
Output is correct |
22 |
Correct |
36 ms |
15696 KB |
Output is correct |
23 |
Correct |
36 ms |
13652 KB |
Output is correct |
24 |
Correct |
36 ms |
11528 KB |
Output is correct |
25 |
Correct |
42 ms |
16204 KB |
Output is correct |
26 |
Correct |
37 ms |
13904 KB |
Output is correct |
27 |
Correct |
44 ms |
16216 KB |
Output is correct |
28 |
Correct |
40 ms |
16460 KB |
Output is correct |
29 |
Correct |
48 ms |
18160 KB |
Output is correct |
30 |
Correct |
127 ms |
31180 KB |
Output is correct |
31 |
Correct |
168 ms |
43712 KB |
Output is correct |
32 |
Correct |
224 ms |
54288 KB |
Output is correct |
33 |
Correct |
345 ms |
77944 KB |
Output is correct |
34 |
Correct |
108 ms |
30976 KB |
Output is correct |
35 |
Correct |
333 ms |
78268 KB |
Output is correct |
36 |
Correct |
337 ms |
78808 KB |
Output is correct |
37 |
Correct |
318 ms |
77088 KB |
Output is correct |
38 |
Correct |
327 ms |
79388 KB |
Output is correct |
39 |
Correct |
323 ms |
76212 KB |
Output is correct |
40 |
Correct |
318 ms |
76980 KB |
Output is correct |
41 |
Correct |
320 ms |
77520 KB |
Output is correct |
42 |
Correct |
323 ms |
77488 KB |
Output is correct |
43 |
Correct |
254 ms |
75696 KB |
Output is correct |
44 |
Correct |
253 ms |
76284 KB |
Output is correct |
45 |
Correct |
257 ms |
75836 KB |
Output is correct |
46 |
Correct |
222 ms |
53536 KB |
Output is correct |
47 |
Correct |
212 ms |
41756 KB |
Output is correct |
48 |
Correct |
257 ms |
61264 KB |
Output is correct |
49 |
Correct |
260 ms |
60676 KB |
Output is correct |