#include <bits/stdc++.h>
using namespace std;
struct treemax {
typedef int T;
T f(T a, T b) { return max(a, b); }
vector<T> s;
int n;
T unit = 0;
treemax(int nn, T fil) : s(2 * nn, fil), n(nn), unit(fil) {}
void update(int p, T v)
{
for(s[p += n] = v; p /= 2;) {
s[p] = f(s[2 * p], s[2 * p + 1]);
}
}
T query(int a, int b)
{
T ra = unit, rb = unit;
for(a += n, b += n + 1; a < b; a /= 2, b /= 2) {
if(a % 2) ra = f(ra, s[a++]);
if(b % 2) rb = f(rb, s[--b]);
}
return f(ra, rb);
}
};
struct treesum {
typedef int T;
vector<T> s;
treesum(int n) : s(n) {}
void update(int pos, T dif)
{
for(; pos < (int)s.size(); pos |= pos + 1) {
s[pos] += dif;
}
}
T query(int pos)
{
T res = 0;
for(pos++; pos >= 1; pos &= pos - 1) {
res += s[pos - 1];
}
return res;
}
};
#define make_unique(v) sort((v).begin(), (v).end()), (v).erase(unique((v).begin(), (v).end()), (v).end())
struct scaler {
vector<int> all;
void insert(int x) { all.emplace_back(x); }
void init() { make_unique(all); }
int get(int x) { return lower_bound(all.begin(), all.end(), x) - all.begin(); }
};
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<int> flip(n + 1);
vector<int> a(n + 1), b(n + 1), c(q + 1);
scaler s;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
if(a[i] > b[i]) {
swap(a[i], b[i]);
flip[i] = 1;
}
s.insert(a[i]), s.insert(b[i]);
}
for(int i = 1; i <= q; i++) {
cin >> c[i];
s.insert(c[i]);
}
s.insert(-1);
s.init();
int szz = (int)s.all.size();
treemax rmq(szz + 123, 0);
for(int i = 1; i <= q; i++) {
c[i] = s.get(c[i]);
rmq.update(c[i], i);
}
vector<pair<int, int>> intervals;
for(int i = 1; i <= n; i++) {
a[i] = s.get(a[i]), b[i] = s.get(b[i]);
int when = rmq.query(a[i], b[i] - 1);
if(when) {
flip[i] = 1;
}
intervals.emplace_back(when + 1, i);
}
sort(intervals.begin(), intervals.end());
treesum sum(szz + 123);
int ptr = q;
for(int i = n - 1; i >= 0; i--) {
while(ptr >= intervals[i].first) {
sum.update(c[ptr], 1);
ptr--;
}
flip[intervals[i].second] ^= ((q - ptr - sum.query(b[intervals[i].second] - 1)) % 2);
}
long long ans = 0;
for(int i = 1; i <= n; i++) {
if(!flip[i]) {
ans += s.all[a[i]];
}
else {
ans += s.all[b[i]];
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
368 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
368 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
11 ms |
1232 KB |
Output is correct |
15 |
Correct |
23 ms |
2316 KB |
Output is correct |
16 |
Correct |
33 ms |
3480 KB |
Output is correct |
17 |
Correct |
51 ms |
4384 KB |
Output is correct |
18 |
Correct |
51 ms |
4380 KB |
Output is correct |
19 |
Correct |
52 ms |
4372 KB |
Output is correct |
20 |
Correct |
49 ms |
4396 KB |
Output is correct |
21 |
Correct |
45 ms |
4440 KB |
Output is correct |
22 |
Correct |
32 ms |
3492 KB |
Output is correct |
23 |
Correct |
31 ms |
3264 KB |
Output is correct |
24 |
Correct |
31 ms |
3148 KB |
Output is correct |
25 |
Correct |
32 ms |
3568 KB |
Output is correct |
26 |
Correct |
42 ms |
3688 KB |
Output is correct |
27 |
Correct |
42 ms |
4000 KB |
Output is correct |
28 |
Correct |
38 ms |
3892 KB |
Output is correct |
29 |
Correct |
41 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
368 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
11 ms |
1232 KB |
Output is correct |
15 |
Correct |
23 ms |
2316 KB |
Output is correct |
16 |
Correct |
33 ms |
3480 KB |
Output is correct |
17 |
Correct |
51 ms |
4384 KB |
Output is correct |
18 |
Correct |
51 ms |
4380 KB |
Output is correct |
19 |
Correct |
52 ms |
4372 KB |
Output is correct |
20 |
Correct |
49 ms |
4396 KB |
Output is correct |
21 |
Correct |
45 ms |
4440 KB |
Output is correct |
22 |
Correct |
32 ms |
3492 KB |
Output is correct |
23 |
Correct |
31 ms |
3264 KB |
Output is correct |
24 |
Correct |
31 ms |
3148 KB |
Output is correct |
25 |
Correct |
32 ms |
3568 KB |
Output is correct |
26 |
Correct |
42 ms |
3688 KB |
Output is correct |
27 |
Correct |
42 ms |
4000 KB |
Output is correct |
28 |
Correct |
38 ms |
3892 KB |
Output is correct |
29 |
Correct |
41 ms |
4180 KB |
Output is correct |
30 |
Correct |
79 ms |
6884 KB |
Output is correct |
31 |
Correct |
130 ms |
10232 KB |
Output is correct |
32 |
Correct |
196 ms |
14264 KB |
Output is correct |
33 |
Correct |
290 ms |
22332 KB |
Output is correct |
34 |
Correct |
73 ms |
6296 KB |
Output is correct |
35 |
Correct |
329 ms |
22296 KB |
Output is correct |
36 |
Correct |
293 ms |
22292 KB |
Output is correct |
37 |
Correct |
331 ms |
22224 KB |
Output is correct |
38 |
Correct |
291 ms |
22276 KB |
Output is correct |
39 |
Correct |
287 ms |
22176 KB |
Output is correct |
40 |
Correct |
311 ms |
22048 KB |
Output is correct |
41 |
Correct |
283 ms |
22228 KB |
Output is correct |
42 |
Correct |
300 ms |
22200 KB |
Output is correct |
43 |
Correct |
172 ms |
21556 KB |
Output is correct |
44 |
Correct |
194 ms |
21576 KB |
Output is correct |
45 |
Correct |
181 ms |
21468 KB |
Output is correct |
46 |
Correct |
185 ms |
15832 KB |
Output is correct |
47 |
Correct |
188 ms |
14788 KB |
Output is correct |
48 |
Correct |
225 ms |
18352 KB |
Output is correct |
49 |
Correct |
223 ms |
18472 KB |
Output is correct |