#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=2e5+6;
const ll MAXM=6e5+6;
const ll INF=2e9;
map<ll,ll>mp;
set<ll>xs;
ll n,k;
ll t[MAXN],a[MAXN],b[MAXN];
bool cmp(pair<ll,ll> p1,pair<ll,ll>p2)
{
return make_pair(max(p1.first,p1.second),min(p1.first,p1.second))>make_pair(max(p2.first,p2.second),min(p2.first,p2.second));
}
vector<pair<ll,ll> >v;
struct segment_tree
{
ll tree[4*MAXM];
void init()
{
memset(tree,0,sizeof(tree));
}
void update(ll l,ll r,ll idx,ll q,ll val)
{
if(l>q)return;
if(r<q)return;
if(l==r)
{
tree[idx]=max(tree[idx],val);
return;
}
ll mid=(l+r)/2;
update(l,mid,idx*2,q,val);
update(mid+1,r,idx*2+1,q,val);
tree[idx]=max(tree[idx*2],tree[idx*2+1]);
}
ll query(ll l,ll r,ll idx,ll ql,ll qr)
{
if(ql>qr)return 0;
if(l>qr||r<ql)return 0;
if(l>=ql&&r<=qr)return tree[idx];
ll mid=(l+r)/2;
return max(query(l,mid,idx*2,ql,qr),query(mid+1,r,idx*2+1,ql,qr));
}
}ivan;
struct BIT
{
ll fen[MAXM];
void init()
{
memset(fen,0,sizeof(fen));
}
void update(ll idx,ll val)
{
idx++;
for(;idx<MAXM;idx+=idx&(-idx))
{
fen[idx]+=val;
}
}
ll query(ll idx)
{
idx++;
ll ret=0;
for(;idx>0;idx-=idx&(-idx))
{
ret+=fen[idx];
}
return ret;
}
ll query(ll l,ll r)
{
return query(r)-query(l-1);
}
}vanka;
ll ans2[MAXN];
int main()
{
cin>>n>>k;
for(ll i=1;i<=n;i++)
{
cin>>a[i]>>b[i];
xs.insert(a[i]);
xs.insert(b[i]);
v.push_back({a[i],b[i]});
}
sort(v.begin(),v.end(),cmp);
for(ll i=1;i<=k;i++)
{
cin>>t[i];
xs.insert(t[i]);
}
xs.insert(INF);
ll cnt=0;
for(auto xd:xs)
{
cnt++;
mp[xd]=cnt;
}
vector<pair<ll,ll> >v1;
ivan.init();vanka.init();
ll ans=0,ans1=0;
for(ll i=1;i<=k;i++)
{
v1.push_back({t[i],i});
ivan.update(1,cnt,1,mp[t[i]],i);
}
sort(v1.begin(),v1.end());
ll r=v1.size()-1;
/*for(int i=1;i<=n;i++)
{
ans2[i]=ans1;
int cnt1=0;
for(int j=k;j>=0;j--)
{
if(j==0)
{
if(cnt1%2==0)ans1+=a[i];
else ans1+=b[i];
break;
}
if(t[j]>=max(a[i],b[i]))cnt1++;
if(t[j]>=min(a[i],b[i])&&t[j]<max(a[i],b[i]))
{
if(cnt1%2==0)ans1+=max(a[i],b[i]);
else ans1+=min(a[i],b[i]);
break;
}
}
}*/
for(ll i=0;i<v.size();i++)
{
while(r>=0&&v1[r].first>=max(v[i].first,v[i].second))
{
//cout<<"*\n";
vanka.update(v1[r].second,+1);
r--;
}
ll lastxd=ivan.query(1,cnt,1,mp[min(v[i].first,v[i].second)],mp[max(v[i].first,v[i].second)]-1);
//cout<<lastxd<<endl;
if(lastxd==0)
{
ll f=vanka.query(1,k);
//cout<<f<<endl;
if(f%2==1)
{
ans+=v[i].second;
}
else ans+=v[i].first;
}
else
{
ll f=vanka.query(lastxd+1,k);
if(f%2==0)
{
ans+=max(v[i].first,v[i].second);
}
else
{
ans+=min(v[i].first,v[i].second);
}
}
//cout<<v[i].first<<" "<<v[i].second<<" "<<ans<<endl;
}
cout<<ans<<endl;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:133:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for(ll i=0;i<v.size();i++)
| ~^~~~~~~~~
fortune_telling2.cpp:102:11: warning: unused variable 'ans1' [-Wunused-variable]
102 | ll ans=0,ans1=0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23972 KB |
Output is correct |
2 |
Correct |
13 ms |
24012 KB |
Output is correct |
3 |
Correct |
14 ms |
24116 KB |
Output is correct |
4 |
Correct |
14 ms |
24156 KB |
Output is correct |
5 |
Correct |
19 ms |
24152 KB |
Output is correct |
6 |
Correct |
14 ms |
24140 KB |
Output is correct |
7 |
Correct |
15 ms |
24152 KB |
Output is correct |
8 |
Correct |
14 ms |
24116 KB |
Output is correct |
9 |
Correct |
13 ms |
24140 KB |
Output is correct |
10 |
Correct |
14 ms |
23884 KB |
Output is correct |
11 |
Correct |
13 ms |
24004 KB |
Output is correct |
12 |
Correct |
18 ms |
24012 KB |
Output is correct |
13 |
Correct |
17 ms |
24012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23972 KB |
Output is correct |
2 |
Correct |
13 ms |
24012 KB |
Output is correct |
3 |
Correct |
14 ms |
24116 KB |
Output is correct |
4 |
Correct |
14 ms |
24156 KB |
Output is correct |
5 |
Correct |
19 ms |
24152 KB |
Output is correct |
6 |
Correct |
14 ms |
24140 KB |
Output is correct |
7 |
Correct |
15 ms |
24152 KB |
Output is correct |
8 |
Correct |
14 ms |
24116 KB |
Output is correct |
9 |
Correct |
13 ms |
24140 KB |
Output is correct |
10 |
Correct |
14 ms |
23884 KB |
Output is correct |
11 |
Correct |
13 ms |
24004 KB |
Output is correct |
12 |
Correct |
18 ms |
24012 KB |
Output is correct |
13 |
Correct |
17 ms |
24012 KB |
Output is correct |
14 |
Correct |
54 ms |
27840 KB |
Output is correct |
15 |
Correct |
112 ms |
31892 KB |
Output is correct |
16 |
Correct |
198 ms |
35540 KB |
Output is correct |
17 |
Correct |
268 ms |
39960 KB |
Output is correct |
18 |
Correct |
302 ms |
40044 KB |
Output is correct |
19 |
Correct |
266 ms |
39972 KB |
Output is correct |
20 |
Correct |
251 ms |
39956 KB |
Output is correct |
21 |
Correct |
255 ms |
39944 KB |
Output is correct |
22 |
Correct |
160 ms |
35656 KB |
Output is correct |
23 |
Correct |
137 ms |
33464 KB |
Output is correct |
24 |
Correct |
134 ms |
31784 KB |
Output is correct |
25 |
Correct |
176 ms |
36964 KB |
Output is correct |
26 |
Correct |
152 ms |
34660 KB |
Output is correct |
27 |
Correct |
172 ms |
35652 KB |
Output is correct |
28 |
Correct |
180 ms |
35632 KB |
Output is correct |
29 |
Correct |
209 ms |
37804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23972 KB |
Output is correct |
2 |
Correct |
13 ms |
24012 KB |
Output is correct |
3 |
Correct |
14 ms |
24116 KB |
Output is correct |
4 |
Correct |
14 ms |
24156 KB |
Output is correct |
5 |
Correct |
19 ms |
24152 KB |
Output is correct |
6 |
Correct |
14 ms |
24140 KB |
Output is correct |
7 |
Correct |
15 ms |
24152 KB |
Output is correct |
8 |
Correct |
14 ms |
24116 KB |
Output is correct |
9 |
Correct |
13 ms |
24140 KB |
Output is correct |
10 |
Correct |
14 ms |
23884 KB |
Output is correct |
11 |
Correct |
13 ms |
24004 KB |
Output is correct |
12 |
Correct |
18 ms |
24012 KB |
Output is correct |
13 |
Correct |
17 ms |
24012 KB |
Output is correct |
14 |
Correct |
54 ms |
27840 KB |
Output is correct |
15 |
Correct |
112 ms |
31892 KB |
Output is correct |
16 |
Correct |
198 ms |
35540 KB |
Output is correct |
17 |
Correct |
268 ms |
39960 KB |
Output is correct |
18 |
Correct |
302 ms |
40044 KB |
Output is correct |
19 |
Correct |
266 ms |
39972 KB |
Output is correct |
20 |
Correct |
251 ms |
39956 KB |
Output is correct |
21 |
Correct |
255 ms |
39944 KB |
Output is correct |
22 |
Correct |
160 ms |
35656 KB |
Output is correct |
23 |
Correct |
137 ms |
33464 KB |
Output is correct |
24 |
Correct |
134 ms |
31784 KB |
Output is correct |
25 |
Correct |
176 ms |
36964 KB |
Output is correct |
26 |
Correct |
152 ms |
34660 KB |
Output is correct |
27 |
Correct |
172 ms |
35652 KB |
Output is correct |
28 |
Correct |
180 ms |
35632 KB |
Output is correct |
29 |
Correct |
209 ms |
37804 KB |
Output is correct |
30 |
Correct |
605 ms |
56040 KB |
Output is correct |
31 |
Correct |
937 ms |
66936 KB |
Output is correct |
32 |
Correct |
1257 ms |
80376 KB |
Output is correct |
33 |
Correct |
2072 ms |
109260 KB |
Output is correct |
34 |
Correct |
615 ms |
53372 KB |
Output is correct |
35 |
Correct |
2007 ms |
109236 KB |
Output is correct |
36 |
Correct |
2064 ms |
109452 KB |
Output is correct |
37 |
Correct |
2133 ms |
109220 KB |
Output is correct |
38 |
Correct |
1938 ms |
109248 KB |
Output is correct |
39 |
Correct |
1959 ms |
109260 KB |
Output is correct |
40 |
Correct |
1918 ms |
109028 KB |
Output is correct |
41 |
Correct |
1800 ms |
109256 KB |
Output is correct |
42 |
Correct |
1746 ms |
109304 KB |
Output is correct |
43 |
Correct |
1257 ms |
108544 KB |
Output is correct |
44 |
Correct |
1270 ms |
108548 KB |
Output is correct |
45 |
Correct |
1182 ms |
108672 KB |
Output is correct |
46 |
Correct |
1022 ms |
74528 KB |
Output is correct |
47 |
Correct |
832 ms |
63476 KB |
Output is correct |
48 |
Correct |
1221 ms |
87440 KB |
Output is correct |
49 |
Correct |
1278 ms |
87424 KB |
Output is correct |