#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=2e5+69;
const ll offset=2e5;
const ll inf=1e18;
const int base=350;
const ll mod=998244353;
int n,k;
int a[maxn],b[maxn],t[maxn];
pii c[maxn];
int rmq[maxn][19];
vector<pii> qr[maxn];
int dd[maxn];
int st[maxn];
void Update(int u)
{
for(;u<=k;u+=u&-u) st[u]+=1;
}
int Get1(int u)
{
int r=0;
for(;u>0;u-=u&-u) r+=st[u];
return r;
}
int Get(int l,int r)
{
return Get1(r)-Get1(l-1);
}
int Get_max(int l,int r)
{
int lg=__lg(r-l+1);
return max(rmq[l][lg],rmq[r-(1<<lg)+1][lg]);
}
void sol()
{
cin >> n>>k;
for1(i,1,n) cin >> a[i]>> b[i];
for1(i,1,k)
{
cin >> t[i];
c[i]={t[i],i};
}
sort(c+1,c+1+k);
for1(i,1,k) rmq[i][0]=c[i].se;
for1(j,1,18) for1(i,1,k-(1<<j)+1) rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
for1(i,1,n)
{
int l=min(a[i],b[i]);
int r=max(a[i],b[i]);
int cl= upper_bound(c+1,c+1+k,make_pair(l,-1LL))-c;
int cr=upper_bound(c+1,c+1+k,make_pair(r,-1LL))-c-1;
int j=0;
if (cl<=cr)
{
j=Get_max(cl,cr);
if (a[i]<b[i]) swap(a[i],b[i]);
}
qr[cr+1].pb({i,j});
// cerr<< i<<' '<<j<<" c "<<a[i]<<' '<<b[i]<<'\n';
}
for2(i,k,1)
{
// cerr<< c[i].se<<'\n';
Update(c[i].se);
for(pii x: qr[i])
{
dd[x.fi]=Get(x.se+1,k)&1;
// cerr<< x.fi<<' '<<dd[x.fi]<<'\n';
}
// cerr<<i<<'\n';
}
// return;
int res=0;
for1(i,1,n)
{
if (!dd[i]) res+=a[i];
else res+=b[i];
}
cout << res;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("1017G.inp","r",stdin);
// freopen("1017G.out","w",stdout);
int t=1;//cin >> t;
while (t--) {
sol();
}
}
/*
4 12
1 2 3
1 3
2 3
2 1
1 2
1 2
1 4
1 1
1 1
2 4
2 3
1 1
3 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16720 KB |
Output is correct |
2 |
Correct |
2 ms |
16724 KB |
Output is correct |
3 |
Correct |
3 ms |
16724 KB |
Output is correct |
4 |
Correct |
3 ms |
16768 KB |
Output is correct |
5 |
Correct |
3 ms |
16880 KB |
Output is correct |
6 |
Correct |
3 ms |
16724 KB |
Output is correct |
7 |
Correct |
3 ms |
16724 KB |
Output is correct |
8 |
Correct |
3 ms |
16724 KB |
Output is correct |
9 |
Correct |
2 ms |
14676 KB |
Output is correct |
10 |
Correct |
3 ms |
16724 KB |
Output is correct |
11 |
Correct |
3 ms |
16724 KB |
Output is correct |
12 |
Correct |
3 ms |
16856 KB |
Output is correct |
13 |
Correct |
3 ms |
16736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16720 KB |
Output is correct |
2 |
Correct |
2 ms |
16724 KB |
Output is correct |
3 |
Correct |
3 ms |
16724 KB |
Output is correct |
4 |
Correct |
3 ms |
16768 KB |
Output is correct |
5 |
Correct |
3 ms |
16880 KB |
Output is correct |
6 |
Correct |
3 ms |
16724 KB |
Output is correct |
7 |
Correct |
3 ms |
16724 KB |
Output is correct |
8 |
Correct |
3 ms |
16724 KB |
Output is correct |
9 |
Correct |
2 ms |
14676 KB |
Output is correct |
10 |
Correct |
3 ms |
16724 KB |
Output is correct |
11 |
Correct |
3 ms |
16724 KB |
Output is correct |
12 |
Correct |
3 ms |
16856 KB |
Output is correct |
13 |
Correct |
3 ms |
16736 KB |
Output is correct |
14 |
Correct |
8 ms |
19540 KB |
Output is correct |
15 |
Correct |
15 ms |
22232 KB |
Output is correct |
16 |
Correct |
22 ms |
20820 KB |
Output is correct |
17 |
Correct |
30 ms |
25164 KB |
Output is correct |
18 |
Correct |
30 ms |
25172 KB |
Output is correct |
19 |
Correct |
29 ms |
25164 KB |
Output is correct |
20 |
Correct |
28 ms |
25176 KB |
Output is correct |
21 |
Correct |
26 ms |
25172 KB |
Output is correct |
22 |
Correct |
21 ms |
24660 KB |
Output is correct |
23 |
Correct |
22 ms |
24752 KB |
Output is correct |
24 |
Correct |
22 ms |
24916 KB |
Output is correct |
25 |
Correct |
20 ms |
24660 KB |
Output is correct |
26 |
Correct |
24 ms |
24936 KB |
Output is correct |
27 |
Correct |
28 ms |
25172 KB |
Output is correct |
28 |
Correct |
34 ms |
25164 KB |
Output is correct |
29 |
Correct |
35 ms |
25176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16720 KB |
Output is correct |
2 |
Correct |
2 ms |
16724 KB |
Output is correct |
3 |
Correct |
3 ms |
16724 KB |
Output is correct |
4 |
Correct |
3 ms |
16768 KB |
Output is correct |
5 |
Correct |
3 ms |
16880 KB |
Output is correct |
6 |
Correct |
3 ms |
16724 KB |
Output is correct |
7 |
Correct |
3 ms |
16724 KB |
Output is correct |
8 |
Correct |
3 ms |
16724 KB |
Output is correct |
9 |
Correct |
2 ms |
14676 KB |
Output is correct |
10 |
Correct |
3 ms |
16724 KB |
Output is correct |
11 |
Correct |
3 ms |
16724 KB |
Output is correct |
12 |
Correct |
3 ms |
16856 KB |
Output is correct |
13 |
Correct |
3 ms |
16736 KB |
Output is correct |
14 |
Correct |
8 ms |
19540 KB |
Output is correct |
15 |
Correct |
15 ms |
22232 KB |
Output is correct |
16 |
Correct |
22 ms |
20820 KB |
Output is correct |
17 |
Correct |
30 ms |
25164 KB |
Output is correct |
18 |
Correct |
30 ms |
25172 KB |
Output is correct |
19 |
Correct |
29 ms |
25164 KB |
Output is correct |
20 |
Correct |
28 ms |
25176 KB |
Output is correct |
21 |
Correct |
26 ms |
25172 KB |
Output is correct |
22 |
Correct |
21 ms |
24660 KB |
Output is correct |
23 |
Correct |
22 ms |
24752 KB |
Output is correct |
24 |
Correct |
22 ms |
24916 KB |
Output is correct |
25 |
Correct |
20 ms |
24660 KB |
Output is correct |
26 |
Correct |
24 ms |
24936 KB |
Output is correct |
27 |
Correct |
28 ms |
25172 KB |
Output is correct |
28 |
Correct |
34 ms |
25164 KB |
Output is correct |
29 |
Correct |
35 ms |
25176 KB |
Output is correct |
30 |
Correct |
60 ms |
48208 KB |
Output is correct |
31 |
Correct |
80 ms |
50000 KB |
Output is correct |
32 |
Correct |
119 ms |
52196 KB |
Output is correct |
33 |
Correct |
162 ms |
56664 KB |
Output is correct |
34 |
Correct |
50 ms |
47716 KB |
Output is correct |
35 |
Correct |
161 ms |
56504 KB |
Output is correct |
36 |
Correct |
173 ms |
56652 KB |
Output is correct |
37 |
Correct |
165 ms |
56532 KB |
Output is correct |
38 |
Correct |
164 ms |
56652 KB |
Output is correct |
39 |
Correct |
158 ms |
56664 KB |
Output is correct |
40 |
Correct |
130 ms |
56148 KB |
Output is correct |
41 |
Correct |
168 ms |
56652 KB |
Output is correct |
42 |
Correct |
163 ms |
56640 KB |
Output is correct |
43 |
Correct |
85 ms |
54628 KB |
Output is correct |
44 |
Correct |
87 ms |
54588 KB |
Output is correct |
45 |
Correct |
89 ms |
54796 KB |
Output is correct |
46 |
Correct |
98 ms |
54860 KB |
Output is correct |
47 |
Correct |
107 ms |
54872 KB |
Output is correct |
48 |
Correct |
117 ms |
56656 KB |
Output is correct |
49 |
Correct |
117 ms |
56652 KB |
Output is correct |