#pragma GCC optimize ("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define task "task"
#define task "swapcard"
#define prll pair<ll,ll>
#define pb push_back
#define ld long double
#define int ll
const ll MIN=-1e18,MAX=1e18,MOD=1e9+7;
vector<int> f[800005];
int tree[3000005];
map<ll,int> danh;
int d=0;
pair<ll,ll> a[200005];
ll t[200005];
ll rev[200005];
void build(ll node,ll l,ll r){
if(l==r) {f[node].push_back(t[l]);return;}
ll m=(l+r)/2;
build(node*2,l,m);
build(node*2+1,m+1,r);
merge(f[node*2].begin(),f[node*2].end(),f[node*2+1].begin(),f[node*2+1].end(),back_inserter(f[node]));
}
ll chat(ll node,ll x){
ll l=0,r=f[node].size()-1,fl=-1;
while(l<=r){
ll m=(l+r)/2;
if(f[node][m]>=x){
fl=m;
r=m-1;
}
else l=m+1;
}
return (fl==-1?0:f[node].size()-fl);
}
ll get(ll node,ll l,ll r,ll u,ll v,ll x){
if(l>v or r<u) return 0;
if(l>=u and r<=v) return chat(node,x);
ll m=(l+r)/2;
return get(node*2,l,m,u,v,x)+get(node*2+1,m+1,r,u,v,x);
}
void update2(int node,int l,int r,int u,int v)
{
if(l>u or r<u) return;
if(l==r)
{
tree[node]=v;
return;
}
int m=(l+r)/2;
update2(node*2,l,m,u,v);
update2(node*2+1,m+1,r,u,v);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
int get2(int node,int l ,int r ,int u, int v)
{
if(l>v or r<u) return -1;
if(l>=u and r<=v) return tree[node];
int m=(l+r)/2;
return max(get2(node*2,l,m,u,v),get2(node*2+1,m+1,r,u,v));
}
vector<ll> lst;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i].fi>>a[i].se;
lst.push_back(a[i].fi);
lst.push_back(a[i].se);
}
for(int i=1;i<=k;i++)
{
cin>>t[i];
lst.push_back(t[i]);
}
sort(lst.begin(),lst.end());
for(auto i:lst)
{
if(!danh[i])
{
danh[i]=++d;
rev[d]=i;
}
}
for(int i=1;i<=n;i++)
{
a[i].fi=danh[a[i].fi];
a[i].se=danh[a[i].se];
}
for(int i=1;i<=4*d;i++)
{
tree[i]=-1;
}
for(int i=1;i<=k;i++)
{
t[i]=danh[t[i]];
update2(1,1,d,t[i],i);
}
build(1,1,k);
ll ans=0;
for(int i=1;i<=n;i++)
{
int u=a[i].fi,v=a[i].se;
if(u>v) swap(u,v);
int pos=get2(1,1,d,u,v-1);
// cout<<u<<" "<<v<<" "<<pos<<endl;
if(pos==-1)
{
int sl=get(1,1,k,1,k,v);
if(sl%2==0) ans+=rev[a[i].fi];
else ans+=rev[a[i].se];
// if(sl%2==0) cout<<a[i].fi<<endl;
// else cout<<a[i].se;
}
else
{
int sl=get(1,1,k,pos,k,v);
if(sl%2==0) ans+=rev[v];
else ans+=rev[u];
// if(sl%2==0) cout<<u<<endl;
// else cout<<u<<endl;
}
// cout<<endl;
}
cout<<ans;
}
/*
*/
Compilation message
fortune_telling2.cpp:9: warning: "task" redefined
9 | #define task "swapcard"
|
fortune_telling2.cpp:8: note: this is the location of the previous definition
8 | #define task "task"
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
26456 KB |
Output is correct |
2 |
Correct |
6 ms |
26460 KB |
Output is correct |
3 |
Correct |
7 ms |
28508 KB |
Output is correct |
4 |
Correct |
7 ms |
28504 KB |
Output is correct |
5 |
Correct |
7 ms |
28504 KB |
Output is correct |
6 |
Correct |
8 ms |
28504 KB |
Output is correct |
7 |
Correct |
7 ms |
28504 KB |
Output is correct |
8 |
Correct |
7 ms |
28508 KB |
Output is correct |
9 |
Correct |
9 ms |
28504 KB |
Output is correct |
10 |
Correct |
6 ms |
26616 KB |
Output is correct |
11 |
Correct |
7 ms |
26460 KB |
Output is correct |
12 |
Correct |
7 ms |
26456 KB |
Output is correct |
13 |
Correct |
7 ms |
28504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
26456 KB |
Output is correct |
2 |
Correct |
6 ms |
26460 KB |
Output is correct |
3 |
Correct |
7 ms |
28508 KB |
Output is correct |
4 |
Correct |
7 ms |
28504 KB |
Output is correct |
5 |
Correct |
7 ms |
28504 KB |
Output is correct |
6 |
Correct |
8 ms |
28504 KB |
Output is correct |
7 |
Correct |
7 ms |
28504 KB |
Output is correct |
8 |
Correct |
7 ms |
28508 KB |
Output is correct |
9 |
Correct |
9 ms |
28504 KB |
Output is correct |
10 |
Correct |
6 ms |
26616 KB |
Output is correct |
11 |
Correct |
7 ms |
26460 KB |
Output is correct |
12 |
Correct |
7 ms |
26456 KB |
Output is correct |
13 |
Correct |
7 ms |
28504 KB |
Output is correct |
14 |
Correct |
30 ms |
32624 KB |
Output is correct |
15 |
Correct |
59 ms |
36832 KB |
Output is correct |
16 |
Correct |
85 ms |
42184 KB |
Output is correct |
17 |
Correct |
119 ms |
47820 KB |
Output is correct |
18 |
Correct |
120 ms |
47924 KB |
Output is correct |
19 |
Correct |
123 ms |
47816 KB |
Output is correct |
20 |
Correct |
121 ms |
47824 KB |
Output is correct |
21 |
Correct |
118 ms |
47856 KB |
Output is correct |
22 |
Correct |
88 ms |
45516 KB |
Output is correct |
23 |
Correct |
82 ms |
42188 KB |
Output is correct |
24 |
Correct |
77 ms |
41164 KB |
Output is correct |
25 |
Correct |
95 ms |
46384 KB |
Output is correct |
26 |
Correct |
90 ms |
44900 KB |
Output is correct |
27 |
Correct |
104 ms |
45328 KB |
Output is correct |
28 |
Correct |
94 ms |
45608 KB |
Output is correct |
29 |
Correct |
112 ms |
46792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
26456 KB |
Output is correct |
2 |
Correct |
6 ms |
26460 KB |
Output is correct |
3 |
Correct |
7 ms |
28508 KB |
Output is correct |
4 |
Correct |
7 ms |
28504 KB |
Output is correct |
5 |
Correct |
7 ms |
28504 KB |
Output is correct |
6 |
Correct |
8 ms |
28504 KB |
Output is correct |
7 |
Correct |
7 ms |
28504 KB |
Output is correct |
8 |
Correct |
7 ms |
28508 KB |
Output is correct |
9 |
Correct |
9 ms |
28504 KB |
Output is correct |
10 |
Correct |
6 ms |
26616 KB |
Output is correct |
11 |
Correct |
7 ms |
26460 KB |
Output is correct |
12 |
Correct |
7 ms |
26456 KB |
Output is correct |
13 |
Correct |
7 ms |
28504 KB |
Output is correct |
14 |
Correct |
30 ms |
32624 KB |
Output is correct |
15 |
Correct |
59 ms |
36832 KB |
Output is correct |
16 |
Correct |
85 ms |
42184 KB |
Output is correct |
17 |
Correct |
119 ms |
47820 KB |
Output is correct |
18 |
Correct |
120 ms |
47924 KB |
Output is correct |
19 |
Correct |
123 ms |
47816 KB |
Output is correct |
20 |
Correct |
121 ms |
47824 KB |
Output is correct |
21 |
Correct |
118 ms |
47856 KB |
Output is correct |
22 |
Correct |
88 ms |
45516 KB |
Output is correct |
23 |
Correct |
82 ms |
42188 KB |
Output is correct |
24 |
Correct |
77 ms |
41164 KB |
Output is correct |
25 |
Correct |
95 ms |
46384 KB |
Output is correct |
26 |
Correct |
90 ms |
44900 KB |
Output is correct |
27 |
Correct |
104 ms |
45328 KB |
Output is correct |
28 |
Correct |
94 ms |
45608 KB |
Output is correct |
29 |
Correct |
112 ms |
46792 KB |
Output is correct |
30 |
Incorrect |
331 ms |
94856 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |