#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) <= int(size(v)); j++) {
jmp.push_back(vector<int>(int(size(v)) - (1<<j) + 1));
for(int i = 0; i < int(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);
vector<int> X;
for(int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
X.push_back(A[i]); X.push_back(B[i]);
}
for(int i = 0; i < Q; i++) {
cin >> T[i];
X.push_back(T[i]);
}
sort(begin(X), end(X));
X.erase(unique(begin(X), end(X)), end(X));
vector<int> a = A, b = B;
int M = size(X);
auto get = [&](int x) {
return lower_bound(begin(X), end(X), x) - begin(X);
};
for(auto &x : a) x = get(x);
for(auto &x : b) x = get(x);
for(auto &x : T) x = get(x);
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
12 ms |
2900 KB |
Output is correct |
15 |
Correct |
20 ms |
5804 KB |
Output is correct |
16 |
Correct |
30 ms |
8736 KB |
Output is correct |
17 |
Correct |
42 ms |
11756 KB |
Output is correct |
18 |
Correct |
43 ms |
11884 KB |
Output is correct |
19 |
Correct |
41 ms |
11764 KB |
Output is correct |
20 |
Correct |
43 ms |
11740 KB |
Output is correct |
21 |
Correct |
41 ms |
11860 KB |
Output is correct |
22 |
Correct |
30 ms |
8572 KB |
Output is correct |
23 |
Correct |
30 ms |
7108 KB |
Output is correct |
24 |
Correct |
29 ms |
5980 KB |
Output is correct |
25 |
Correct |
31 ms |
9716 KB |
Output is correct |
26 |
Correct |
32 ms |
7928 KB |
Output is correct |
27 |
Correct |
38 ms |
8692 KB |
Output is correct |
28 |
Correct |
36 ms |
8644 KB |
Output is correct |
29 |
Correct |
43 ms |
10292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
1 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 |
12 ms |
2900 KB |
Output is correct |
15 |
Correct |
20 ms |
5804 KB |
Output is correct |
16 |
Correct |
30 ms |
8736 KB |
Output is correct |
17 |
Correct |
42 ms |
11756 KB |
Output is correct |
18 |
Correct |
43 ms |
11884 KB |
Output is correct |
19 |
Correct |
41 ms |
11764 KB |
Output is correct |
20 |
Correct |
43 ms |
11740 KB |
Output is correct |
21 |
Correct |
41 ms |
11860 KB |
Output is correct |
22 |
Correct |
30 ms |
8572 KB |
Output is correct |
23 |
Correct |
30 ms |
7108 KB |
Output is correct |
24 |
Correct |
29 ms |
5980 KB |
Output is correct |
25 |
Correct |
31 ms |
9716 KB |
Output is correct |
26 |
Correct |
32 ms |
7928 KB |
Output is correct |
27 |
Correct |
38 ms |
8692 KB |
Output is correct |
28 |
Correct |
36 ms |
8644 KB |
Output is correct |
29 |
Correct |
43 ms |
10292 KB |
Output is correct |
30 |
Correct |
82 ms |
23968 KB |
Output is correct |
31 |
Correct |
116 ms |
32100 KB |
Output is correct |
32 |
Correct |
164 ms |
42296 KB |
Output is correct |
33 |
Correct |
250 ms |
63252 KB |
Output is correct |
34 |
Correct |
73 ms |
21948 KB |
Output is correct |
35 |
Correct |
249 ms |
63256 KB |
Output is correct |
36 |
Correct |
249 ms |
63276 KB |
Output is correct |
37 |
Correct |
248 ms |
63080 KB |
Output is correct |
38 |
Correct |
244 ms |
63172 KB |
Output is correct |
39 |
Correct |
262 ms |
63092 KB |
Output is correct |
40 |
Correct |
236 ms |
63724 KB |
Output is correct |
41 |
Correct |
247 ms |
63064 KB |
Output is correct |
42 |
Correct |
243 ms |
62896 KB |
Output is correct |
43 |
Correct |
180 ms |
63764 KB |
Output is correct |
44 |
Correct |
183 ms |
63764 KB |
Output is correct |
45 |
Correct |
177 ms |
63648 KB |
Output is correct |
46 |
Correct |
160 ms |
36932 KB |
Output is correct |
47 |
Correct |
150 ms |
28480 KB |
Output is correct |
48 |
Correct |
199 ms |
45592 KB |
Output is correct |
49 |
Correct |
187 ms |
45852 KB |
Output is correct |