#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 600010, LOG=20;
int n, m, k, u, v, x, y, t, a, b;
ll ans;
int L[MAXN], R[MAXN], T[MAXN];
int seg[MAXN<<2], fen[MAXN];
vector<int> Q[MAXN], comp;
void Set(int id, int tl, int tr, int pos, int val){
if (pos<tl || tr<=pos) return ;
if (tr-tl==1){
seg[id]=val;
return ;
}
int mid=(tl+tr)>>1;
Set(id<<1, tl, mid, pos, val);
Set(id<<1 | 1, mid, tr, pos, val);
seg[id]=max(seg[id<<1], seg[id<<1 | 1]);
}
int Get(int id, int tl, int tr, int l, int r){
if (r<=tl || tr<=l) return seg[0];
if (l<=tl && tr<=r) return seg[id];
int mid=(tl+tr)>>1;
return max(Get(id<<1, tl, mid, l, r), Get(id<<1 | 1, mid, tr, l, r));
}
void add(int pos){ for (; pos<MAXN; pos+=pos&-pos) fen[pos]++;}
int get(int pos){
int res=0;
for (; pos; pos-=pos&-pos) res+=fen[pos];
return res;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>L[i]>>R[i], comp.pb(L[i]), comp.pb(R[i]);
for (int i=1; i<=m; i++) cin>>T[i], comp.pb(T[i]);
comp.pb(-1);
sort(all(comp));
comp.resize(unique(all(comp))-comp.begin());
for (int i=1; i<=n; i++){
L[i]=lower_bound(all(comp), L[i])-comp.begin();
R[i]=lower_bound(all(comp), R[i])-comp.begin();
swap(L[i], R[i]);
}
for (int i=1; i<=m; i++){
T[i]=lower_bound(all(comp), T[i])-comp.begin();
Set(1, 0, MAXN, T[i], i);
}
for (int i=1; i<=n; i++){
int t=Get(1, 0, MAXN, min(L[i], R[i]), max(L[i], R[i]));
if (t && L[i]>R[i]) swap(L[i], R[i]);
Q[t].pb(i);
//debug2(i, t)
}
for (int i=m; ~i; i--){
for (int id:Q[i]){
int x=get(MAXN-1)-get(max(L[id], R[id])-1);
if (x&1) swap(L[id], R[id]);
ans+=comp[R[id]];
//cerr<<"found "<<id<<" --> "<<comp[R[id]]<<'\n';
}
if (i) add(T[i]);
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14584 KB |
Output is correct |
2 |
Correct |
17 ms |
14584 KB |
Output is correct |
3 |
Correct |
18 ms |
14712 KB |
Output is correct |
4 |
Correct |
17 ms |
14712 KB |
Output is correct |
5 |
Correct |
17 ms |
14712 KB |
Output is correct |
6 |
Correct |
17 ms |
14712 KB |
Output is correct |
7 |
Correct |
19 ms |
14840 KB |
Output is correct |
8 |
Correct |
20 ms |
14716 KB |
Output is correct |
9 |
Correct |
17 ms |
14712 KB |
Output is correct |
10 |
Correct |
16 ms |
14584 KB |
Output is correct |
11 |
Correct |
17 ms |
14584 KB |
Output is correct |
12 |
Correct |
17 ms |
14712 KB |
Output is correct |
13 |
Correct |
17 ms |
14712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14584 KB |
Output is correct |
2 |
Correct |
17 ms |
14584 KB |
Output is correct |
3 |
Correct |
18 ms |
14712 KB |
Output is correct |
4 |
Correct |
17 ms |
14712 KB |
Output is correct |
5 |
Correct |
17 ms |
14712 KB |
Output is correct |
6 |
Correct |
17 ms |
14712 KB |
Output is correct |
7 |
Correct |
19 ms |
14840 KB |
Output is correct |
8 |
Correct |
20 ms |
14716 KB |
Output is correct |
9 |
Correct |
17 ms |
14712 KB |
Output is correct |
10 |
Correct |
16 ms |
14584 KB |
Output is correct |
11 |
Correct |
17 ms |
14584 KB |
Output is correct |
12 |
Correct |
17 ms |
14712 KB |
Output is correct |
13 |
Correct |
17 ms |
14712 KB |
Output is correct |
14 |
Correct |
34 ms |
15864 KB |
Output is correct |
15 |
Correct |
54 ms |
16760 KB |
Output is correct |
16 |
Correct |
75 ms |
17916 KB |
Output is correct |
17 |
Correct |
97 ms |
19088 KB |
Output is correct |
18 |
Correct |
98 ms |
19068 KB |
Output is correct |
19 |
Correct |
95 ms |
19056 KB |
Output is correct |
20 |
Correct |
96 ms |
19060 KB |
Output is correct |
21 |
Correct |
95 ms |
19184 KB |
Output is correct |
22 |
Correct |
75 ms |
17828 KB |
Output is correct |
23 |
Correct |
74 ms |
17652 KB |
Output is correct |
24 |
Correct |
72 ms |
17140 KB |
Output is correct |
25 |
Correct |
75 ms |
18164 KB |
Output is correct |
26 |
Correct |
76 ms |
17912 KB |
Output is correct |
27 |
Correct |
87 ms |
18444 KB |
Output is correct |
28 |
Correct |
81 ms |
18416 KB |
Output is correct |
29 |
Correct |
91 ms |
18904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14584 KB |
Output is correct |
2 |
Correct |
17 ms |
14584 KB |
Output is correct |
3 |
Correct |
18 ms |
14712 KB |
Output is correct |
4 |
Correct |
17 ms |
14712 KB |
Output is correct |
5 |
Correct |
17 ms |
14712 KB |
Output is correct |
6 |
Correct |
17 ms |
14712 KB |
Output is correct |
7 |
Correct |
19 ms |
14840 KB |
Output is correct |
8 |
Correct |
20 ms |
14716 KB |
Output is correct |
9 |
Correct |
17 ms |
14712 KB |
Output is correct |
10 |
Correct |
16 ms |
14584 KB |
Output is correct |
11 |
Correct |
17 ms |
14584 KB |
Output is correct |
12 |
Correct |
17 ms |
14712 KB |
Output is correct |
13 |
Correct |
17 ms |
14712 KB |
Output is correct |
14 |
Correct |
34 ms |
15864 KB |
Output is correct |
15 |
Correct |
54 ms |
16760 KB |
Output is correct |
16 |
Correct |
75 ms |
17916 KB |
Output is correct |
17 |
Correct |
97 ms |
19088 KB |
Output is correct |
18 |
Correct |
98 ms |
19068 KB |
Output is correct |
19 |
Correct |
95 ms |
19056 KB |
Output is correct |
20 |
Correct |
96 ms |
19060 KB |
Output is correct |
21 |
Correct |
95 ms |
19184 KB |
Output is correct |
22 |
Correct |
75 ms |
17828 KB |
Output is correct |
23 |
Correct |
74 ms |
17652 KB |
Output is correct |
24 |
Correct |
72 ms |
17140 KB |
Output is correct |
25 |
Correct |
75 ms |
18164 KB |
Output is correct |
26 |
Correct |
76 ms |
17912 KB |
Output is correct |
27 |
Correct |
87 ms |
18444 KB |
Output is correct |
28 |
Correct |
81 ms |
18416 KB |
Output is correct |
29 |
Correct |
91 ms |
18904 KB |
Output is correct |
30 |
Correct |
193 ms |
22368 KB |
Output is correct |
31 |
Correct |
268 ms |
25448 KB |
Output is correct |
32 |
Correct |
340 ms |
29272 KB |
Output is correct |
33 |
Correct |
506 ms |
36964 KB |
Output is correct |
34 |
Correct |
175 ms |
21844 KB |
Output is correct |
35 |
Correct |
518 ms |
36840 KB |
Output is correct |
36 |
Correct |
508 ms |
36608 KB |
Output is correct |
37 |
Correct |
510 ms |
36556 KB |
Output is correct |
38 |
Correct |
497 ms |
36840 KB |
Output is correct |
39 |
Correct |
499 ms |
36768 KB |
Output is correct |
40 |
Correct |
491 ms |
37152 KB |
Output is correct |
41 |
Correct |
506 ms |
36796 KB |
Output is correct |
42 |
Correct |
516 ms |
36576 KB |
Output is correct |
43 |
Correct |
352 ms |
31892 KB |
Output is correct |
44 |
Correct |
351 ms |
32356 KB |
Output is correct |
45 |
Correct |
349 ms |
33672 KB |
Output is correct |
46 |
Correct |
350 ms |
29668 KB |
Output is correct |
47 |
Correct |
334 ms |
27724 KB |
Output is correct |
48 |
Correct |
403 ms |
33376 KB |
Output is correct |
49 |
Correct |
394 ms |
33596 KB |
Output is correct |