#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
using namespace std;
typedef pair<int,int> ii;
ii arr[200005];
int changes[200005];
struct segmax{
int tree[1200000];
int n;
void init(int N){
n = N;
fill(tree,tree+2*n,-1);
}
void update(int i, int x){
for(i += n;i > 0;i >>= 1) tree[i] = max(tree[i], x);
}
int query(int l, int r){
int res = -1;
for(l += n, r += n;l < r;l >>= 1, r >>= 1){
if(l&1) res = max(res, tree[l++]);
if(r&1) res = max(res, tree[--r]);
}
return res;
}
} RMAX;
struct segxor{
bitset<400005> tree;
int n;
void init(int N){
n = N;
}
void update(int i){
for(i += n;i > 0;i >>= 1) tree[i] = !tree[i];
}
int query(int l, int r){
if(l == -1) l = 0;
int res = 0;
for(l += n, r += n;l < r;l >>= 1, r >>= 1){
if(l&1) res ^= tree[l++];
if(r&1) res ^= tree[--r];
}
return res;
}
} RXOR;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, Q; cin >> n >> Q;
vector<int> dis;
for(int i = 0;i < n;i++){
int a, b; cin >> a >> b;
arr[i] = ii(a,b);
dis.push_back(a);
dis.push_back(b);
}
for(int i = 0;i < Q;i++){
int x; cin >> x;
changes[i] = x;
dis.push_back(x);
}
sort(all(dis));
dis.erase(unique(all(dis)), dis.end());
for(int i = 0;i < n;i++){
arr[i].first = lower_bound(all(dis), arr[i].first) - dis.begin();
arr[i].second = lower_bound(all(dis), arr[i].second) - dis.begin();
}
for(int i = 0;i < Q;i++){
changes[i] = lower_bound(all(dis), changes[i]) - dis.begin();
}
RMAX.init(sz(dis));
for(int i = 0;i < Q;i++){
RMAX.update(changes[i], i);
}
long long ans = 0;
vector<ii> updates;
for(int i = 0;i < Q;i++) updates.push_back(ii(changes[i], i));
sort(all(updates));
sort(arr,arr+n,[&](ii a, ii b){ return max(a.first,a.second) > max(b.first,b.second); });
RXOR.init(Q);
for(int i = 0;i < n;i++){
int L = arr[i].first, R = arr[i].second;
if(L > R) swap(L,R);
while(!updates.empty()){
ii top = updates.back();
if(top.first < R) break;
RXOR.update(top.second);
updates.pop_back();
}
int T = RMAX.query(L, R); ///latest flip which forces it to become R value
int after = RXOR.query(T, Q);
if(T != -1){ ///was flipped to max at some point
if(after == 0) ans += dis[R];
else ans += dis[L];
}
else{
if(after == 0) ans += dis[arr[i].first];
else ans += dis[arr[i].second];
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
14 ms |
1408 KB |
Output is correct |
15 |
Correct |
30 ms |
2296 KB |
Output is correct |
16 |
Correct |
45 ms |
3316 KB |
Output is correct |
17 |
Correct |
63 ms |
4212 KB |
Output is correct |
18 |
Correct |
68 ms |
4212 KB |
Output is correct |
19 |
Correct |
62 ms |
4212 KB |
Output is correct |
20 |
Correct |
61 ms |
4208 KB |
Output is correct |
21 |
Correct |
58 ms |
4212 KB |
Output is correct |
22 |
Correct |
44 ms |
3444 KB |
Output is correct |
23 |
Correct |
44 ms |
3316 KB |
Output is correct |
24 |
Correct |
41 ms |
3188 KB |
Output is correct |
25 |
Correct |
43 ms |
3572 KB |
Output is correct |
26 |
Correct |
45 ms |
3696 KB |
Output is correct |
27 |
Correct |
56 ms |
3956 KB |
Output is correct |
28 |
Correct |
49 ms |
3872 KB |
Output is correct |
29 |
Correct |
58 ms |
4084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
14 ms |
1408 KB |
Output is correct |
15 |
Correct |
30 ms |
2296 KB |
Output is correct |
16 |
Correct |
45 ms |
3316 KB |
Output is correct |
17 |
Correct |
63 ms |
4212 KB |
Output is correct |
18 |
Correct |
68 ms |
4212 KB |
Output is correct |
19 |
Correct |
62 ms |
4212 KB |
Output is correct |
20 |
Correct |
61 ms |
4208 KB |
Output is correct |
21 |
Correct |
58 ms |
4212 KB |
Output is correct |
22 |
Correct |
44 ms |
3444 KB |
Output is correct |
23 |
Correct |
44 ms |
3316 KB |
Output is correct |
24 |
Correct |
41 ms |
3188 KB |
Output is correct |
25 |
Correct |
43 ms |
3572 KB |
Output is correct |
26 |
Correct |
45 ms |
3696 KB |
Output is correct |
27 |
Correct |
56 ms |
3956 KB |
Output is correct |
28 |
Correct |
49 ms |
3872 KB |
Output is correct |
29 |
Correct |
58 ms |
4084 KB |
Output is correct |
30 |
Correct |
126 ms |
8176 KB |
Output is correct |
31 |
Correct |
173 ms |
11112 KB |
Output is correct |
32 |
Correct |
238 ms |
13672 KB |
Output is correct |
33 |
Correct |
342 ms |
19168 KB |
Output is correct |
34 |
Correct |
108 ms |
7664 KB |
Output is correct |
35 |
Correct |
361 ms |
19296 KB |
Output is correct |
36 |
Correct |
362 ms |
19168 KB |
Output is correct |
37 |
Correct |
353 ms |
19168 KB |
Output is correct |
38 |
Correct |
353 ms |
19164 KB |
Output is correct |
39 |
Correct |
352 ms |
19168 KB |
Output is correct |
40 |
Correct |
338 ms |
19040 KB |
Output is correct |
41 |
Correct |
340 ms |
19420 KB |
Output is correct |
42 |
Correct |
347 ms |
19296 KB |
Output is correct |
43 |
Correct |
234 ms |
18504 KB |
Output is correct |
44 |
Correct |
228 ms |
18528 KB |
Output is correct |
45 |
Correct |
225 ms |
18524 KB |
Output is correct |
46 |
Correct |
231 ms |
15068 KB |
Output is correct |
47 |
Correct |
235 ms |
14136 KB |
Output is correct |
48 |
Correct |
293 ms |
17908 KB |
Output is correct |
49 |
Correct |
296 ms |
17784 KB |
Output is correct |