#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
using ll = long long;
struct BIT {
vector<int> data; int N;
void init(int n) { N = n; data = vector<int>(N, 0); }
void add(int p, int x) { for(++p;p<=N;p+=p&-p) data[p-1] += x; }
int sum(int l, int r) { return sum(r+1) - sum(l); }
int sum(int r) { int s = 0; for(;r;r-=r&-r) s += data[r-1]; return s; }
};
struct ST {
int level(int x) { return 31 - __builtin_clz(x); }
int comb(int a, int b) { return max(a, b); }
vector<int> v; vector<vector<int>> jmp;
void init(const vector<int> &_v) {
v = _v; jmp = {v};
for(int j = 1; (1<<j) <= size(v); j++) {
jmp.push_back(vector<int>(size(v) - (1<<j) + 1));
for(int i = 0; i < size(jmp[j]); i++) {
jmp[j][i] = comb(jmp[j-1][i], jmp[j-1][i + (1<<(j-1))]);
}
}
}
int query(int l, int r) {
if (l > r) return -1;
int d = level(r - l + 1);
return comb(jmp[d][l], jmp[d][r - (1<<d) + 1]);
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, Q; cin >> N >> Q;
vector<int> A(N), B(N), T(Q);
for(int i = 0; i < N; i++) cin >> A[i] >> B[i];
for(int i = 0; i < Q; i++) cin >> T[i];
vector<int> a = A, b = B;
int M = -1;
{
map<int, int> mp; int cur = 0;
for(auto x : A) mp[x]++;
for(auto x : B) mp[x]++;
for(auto x : T) mp[x]++;
for(auto &p : mp) p.second = cur++;
for(auto &x : a) x = mp[x];
for(auto &x : b) x = mp[x];
for(auto &x : T) x = mp[x];
M = cur;
}
vector<int> ti(M, -1); for(int i = 0; i < Q; i++) ti[T[i]] = max(ti[T[i]], i);
ST st; st.init(ti);
vector<vector<int>> qry(Q);
vector<int> S(N, 0);
for(int i = 0; i < N; i++) {
int L = min(a[i], b[i]), R = max(a[i], b[i]) - 1;
int time = st.query(L, R);
if (time != -1) S[i] = (a[i] >= b[i] ? 0 : 1);
else time = 0;
qry[time].push_back(i);
}
ll ans = 0;
BIT F; F.init(M);
for(int i = Q - 1; i >= 0; i--) {
F.add(T[i], 1);
for(auto x : qry[i]) {
int mx = max(a[x], b[x]);
int amt = F.sum(mx, M-1);
S[x] ^= (amt % 2);
// cout << x << " " << S[x] << nl;
if (S[x]) ans += B[x];
else ans += A[x];
// cout << ans << nl;
}
}
cout << ans << nl;
return 0;
}
Compilation message
fortune_telling2.cpp: In member function 'void ST::init(const std::vector<int>&)':
fortune_telling2.cpp:23:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int j = 1; (1<<j) <= size(v); j++) {
| ~~~~~~~^~~~~~~~~~
fortune_telling2.cpp:25:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i = 0; i < size(jmp[j]); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
18 ms |
2856 KB |
Output is correct |
15 |
Correct |
43 ms |
5832 KB |
Output is correct |
16 |
Correct |
69 ms |
8464 KB |
Output is correct |
17 |
Correct |
89 ms |
11624 KB |
Output is correct |
18 |
Correct |
86 ms |
12100 KB |
Output is correct |
19 |
Correct |
87 ms |
12228 KB |
Output is correct |
20 |
Correct |
86 ms |
11804 KB |
Output is correct |
21 |
Correct |
113 ms |
11748 KB |
Output is correct |
22 |
Correct |
59 ms |
8196 KB |
Output is correct |
23 |
Correct |
44 ms |
6564 KB |
Output is correct |
24 |
Correct |
50 ms |
6228 KB |
Output is correct |
25 |
Correct |
55 ms |
9876 KB |
Output is correct |
26 |
Correct |
72 ms |
8776 KB |
Output is correct |
27 |
Correct |
64 ms |
9652 KB |
Output is correct |
28 |
Correct |
66 ms |
9360 KB |
Output is correct |
29 |
Correct |
78 ms |
11632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
18 ms |
2856 KB |
Output is correct |
15 |
Correct |
43 ms |
5832 KB |
Output is correct |
16 |
Correct |
69 ms |
8464 KB |
Output is correct |
17 |
Correct |
89 ms |
11624 KB |
Output is correct |
18 |
Correct |
86 ms |
12100 KB |
Output is correct |
19 |
Correct |
87 ms |
12228 KB |
Output is correct |
20 |
Correct |
86 ms |
11804 KB |
Output is correct |
21 |
Correct |
113 ms |
11748 KB |
Output is correct |
22 |
Correct |
59 ms |
8196 KB |
Output is correct |
23 |
Correct |
44 ms |
6564 KB |
Output is correct |
24 |
Correct |
50 ms |
6228 KB |
Output is correct |
25 |
Correct |
55 ms |
9876 KB |
Output is correct |
26 |
Correct |
72 ms |
8776 KB |
Output is correct |
27 |
Correct |
64 ms |
9652 KB |
Output is correct |
28 |
Correct |
66 ms |
9360 KB |
Output is correct |
29 |
Correct |
78 ms |
11632 KB |
Output is correct |
30 |
Correct |
216 ms |
26068 KB |
Output is correct |
31 |
Correct |
399 ms |
34756 KB |
Output is correct |
32 |
Correct |
557 ms |
44936 KB |
Output is correct |
33 |
Correct |
957 ms |
68024 KB |
Output is correct |
34 |
Correct |
178 ms |
24024 KB |
Output is correct |
35 |
Correct |
1006 ms |
72380 KB |
Output is correct |
36 |
Correct |
962 ms |
67196 KB |
Output is correct |
37 |
Correct |
983 ms |
67804 KB |
Output is correct |
38 |
Correct |
946 ms |
67196 KB |
Output is correct |
39 |
Correct |
956 ms |
68876 KB |
Output is correct |
40 |
Correct |
859 ms |
67796 KB |
Output is correct |
41 |
Correct |
982 ms |
69132 KB |
Output is correct |
42 |
Correct |
989 ms |
68436 KB |
Output is correct |
43 |
Correct |
403 ms |
65764 KB |
Output is correct |
44 |
Correct |
409 ms |
65696 KB |
Output is correct |
45 |
Correct |
442 ms |
65756 KB |
Output is correct |
46 |
Correct |
371 ms |
38412 KB |
Output is correct |
47 |
Correct |
324 ms |
29972 KB |
Output is correct |
48 |
Correct |
526 ms |
49348 KB |
Output is correct |
49 |
Correct |
494 ms |
49500 KB |
Output is correct |