#include <bits/stdc++.h>
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
//#pragma GCC optimize("unroll-loops")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 2e5+20,mod = 1e9+7 ,inf = 1e9+10,maxm = (1 << 18)+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
inline bool cmp(pll x,pll y){
return (x > y);
}
int fen[N],sp[N][20],k;
void upd(int l){
for(; l < k; l |= (l+1))
fen[l]++;
}
int que(int l,int r){
r--;
l--;
int ans = 0;
for (; r >= 0; r = (r&(r+1))-1 ){
ans += fen[r];
}
for (; l >= 0; l = (l&(l+1))-1){
ans -= fen[l];
}
return ans;
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n;
ll ans = 0;
vector<pll> v1,v2,q;
cin >> n >> k;
rep(i,0,n){
int a,b;
cin >> a >> b;
if (a == b){
ans += b;
continue;
}
if (a > b) v1.pb({a,b});
else v2.pb({b,a});
}
sort(v1.begin(),v1.end(),cmp);
sort(v2.begin(),v2.end(),cmp);
rep(i,0,k){
int t;
cin >> t;
q.pb({t,i});
}
sort(q.begin(),q.end());
rep(i,0,k) sp[i][0] = q[i].Y;
rep(j,1,20){
rep(i,0,k){
sp[i][j] = sp[i][j-1];
if (i-(1 << (j-1)) >= 0)
sp[i][j] = max(sp[i][j],sp[i-(1 << (j-1))][j-1]);
}
}
int po = k-1;
for (pll p : v1){
while (po >= 0 && q[po].X >= p.X){
upd(q[po].Y);
po--;
}
if (po < 0 || q[po].X < p.Y){
if ((k+1-po)&1) ans += p.Y;
else ans += p.X;
continue;
}
int ind = lower_bound(q.begin(),q.end(),make_pair(p.Y,0))-q.begin();
int g = 31-__builtin_clz(po-ind+1);
int mx = max(sp[po][g],sp[ind+(1 << g)-1][g]);
if (que(mx+1,k)&1) ans += p.Y;
else ans += p.X;
}
po = k-1;
memset(fen,0,sizeof fen);
for (pll p : v2){
while (po >= 0 && q[po].X >= p.X){
upd(q[po].Y);
po--;
}
if (po < 0 || (po >= 0 && q[po].X < p.Y)){
if ((k-po+1)&1) ans += p.X;
else ans += p.Y;
continue;
}
int ind = lower_bound(q.begin(),q.end(),make_pair(p.Y,0))-q.begin();
int g = 31-__builtin_clz(po-ind+1);
int mx = max(sp[po][g],sp[ind+(1 << g)-1][g]);
if (que(mx+1,k)&1) ans += p.Y;
else ans += p.X;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1136 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1232 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1136 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1232 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
8 ms |
2432 KB |
Output is correct |
15 |
Correct |
22 ms |
3684 KB |
Output is correct |
16 |
Correct |
26 ms |
4940 KB |
Output is correct |
17 |
Correct |
29 ms |
6160 KB |
Output is correct |
18 |
Correct |
30 ms |
6068 KB |
Output is correct |
19 |
Correct |
30 ms |
6088 KB |
Output is correct |
20 |
Correct |
35 ms |
6288 KB |
Output is correct |
21 |
Correct |
28 ms |
6216 KB |
Output is correct |
22 |
Correct |
21 ms |
5584 KB |
Output is correct |
23 |
Correct |
27 ms |
5688 KB |
Output is correct |
24 |
Correct |
33 ms |
5660 KB |
Output is correct |
25 |
Correct |
26 ms |
5656 KB |
Output is correct |
26 |
Correct |
23 ms |
5876 KB |
Output is correct |
27 |
Correct |
27 ms |
6220 KB |
Output is correct |
28 |
Correct |
25 ms |
6072 KB |
Output is correct |
29 |
Correct |
28 ms |
6148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1136 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1232 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
8 ms |
2432 KB |
Output is correct |
15 |
Correct |
22 ms |
3684 KB |
Output is correct |
16 |
Correct |
26 ms |
4940 KB |
Output is correct |
17 |
Correct |
29 ms |
6160 KB |
Output is correct |
18 |
Correct |
30 ms |
6068 KB |
Output is correct |
19 |
Correct |
30 ms |
6088 KB |
Output is correct |
20 |
Correct |
35 ms |
6288 KB |
Output is correct |
21 |
Correct |
28 ms |
6216 KB |
Output is correct |
22 |
Correct |
21 ms |
5584 KB |
Output is correct |
23 |
Correct |
27 ms |
5688 KB |
Output is correct |
24 |
Correct |
33 ms |
5660 KB |
Output is correct |
25 |
Correct |
26 ms |
5656 KB |
Output is correct |
26 |
Correct |
23 ms |
5876 KB |
Output is correct |
27 |
Correct |
27 ms |
6220 KB |
Output is correct |
28 |
Correct |
25 ms |
6072 KB |
Output is correct |
29 |
Correct |
28 ms |
6148 KB |
Output is correct |
30 |
Correct |
89 ms |
20732 KB |
Output is correct |
31 |
Correct |
114 ms |
21716 KB |
Output is correct |
32 |
Correct |
150 ms |
23068 KB |
Output is correct |
33 |
Correct |
194 ms |
25768 KB |
Output is correct |
34 |
Correct |
87 ms |
20280 KB |
Output is correct |
35 |
Correct |
201 ms |
25788 KB |
Output is correct |
36 |
Correct |
222 ms |
25820 KB |
Output is correct |
37 |
Correct |
203 ms |
25952 KB |
Output is correct |
38 |
Correct |
187 ms |
25872 KB |
Output is correct |
39 |
Correct |
200 ms |
25804 KB |
Output is correct |
40 |
Correct |
167 ms |
25600 KB |
Output is correct |
41 |
Correct |
167 ms |
25840 KB |
Output is correct |
42 |
Correct |
204 ms |
25780 KB |
Output is correct |
43 |
Correct |
123 ms |
25176 KB |
Output is correct |
44 |
Correct |
133 ms |
25224 KB |
Output is correct |
45 |
Correct |
159 ms |
25156 KB |
Output is correct |
46 |
Correct |
148 ms |
23844 KB |
Output is correct |
47 |
Correct |
126 ms |
23728 KB |
Output is correct |
48 |
Correct |
152 ms |
25828 KB |
Output is correct |
49 |
Correct |
189 ms |
25884 KB |
Output is correct |