#include<bits/stdc++.h>
#define ll long long
#define f first
#define sc second
#define pb push_back
using namespace std;
ll a,b,c,d,i,e,f,g,n,m,k,l,x,BITree[3000006],tree[3000006],q,B[500005],idx,C[500005],fix[500005],ans;
pair <ll,ll> A[500005];
vector <ll> v,v1[500005];
map <ll,ll> mp,mp1;
void shekumshe(vector <ll> vc) {
sort(vc.begin(),vc.end());
x=1;
mp[vc[0]]=x;
mp1[x]=vc[0];
for(ll i=1;i<vc.size();i++) {
if(vc[i]!=vc[i-1]) x++;
mp[vc[i]]=x;
mp1[x]=vc[i];
}
}
void update(ll node,ll le,ll ri,ll idx,ll val) {
if(idx<le || ri<idx) return;
if(le==ri) {
tree[node]=max(tree[node],val);
return;
}
update(2*node,le,(le+ri)/2,idx,val);
update(2*node+1,(le+ri)/2+1,ri,idx,val);
tree[node]=max(tree[2*node],tree[2*node+1]);
}
ll getmax(ll node,ll le,ll ri,ll start,ll end) {
if(start>ri || le>end) return 0;
if(start<=le && ri<=end) return tree[node];
return max(getmax(2*node,le,(le+ri)/2,start,end),getmax(2*node+1,(le+ri)/2+1,ri,start,end));
}
void updateBIT(ll idx,ll val) {
idx++;
while(idx<=x) {
BITree[idx]+=val;
idx+=idx&(-idx);
}
}
ll getSum(ll idx) {
idx++;
ll sum=0;
while(idx>0) {
sum+=BITree[idx];
idx-=idx&(-idx);
}
return sum;
}
int main() {
cin>>n>>q;
for(ll i=1;i<=n;i++) {
cin>>A[i].f>>A[i].sc;
v.pb(A[i].f);
v.pb(A[i].sc);
}
for(ll i=1;i<=q;i++) {
cin>>B[i];
v.pb(B[i]);
}
shekumshe(v);
//cout<<endl;
for(ll i=1;i<=n;i++) {
A[i].f=mp[A[i].f];
A[i].sc=mp[A[i].sc];
// cout<<A[i].f<<" "<<A[i].sc<<endl;
}
for(ll i=1;i<=q;i++) {
B[i]=mp[B[i]];
// cout<<B[i]<<" ";
update(1,1,x,B[i],i);
}
//cout<<endl;
for(ll i=1;i<=n;i++) {
if(A[i].f==A[i].sc) continue;
idx=getmax(1,1,x,min(A[i].f,A[i].sc),max(A[i].f,A[i].sc)-1);
// cout<<idx<<" ";
v1[idx].pb(i);
}
//cout<<endl<<endl;
for(ll i=q;i>=1;i--) {
//cout<<i<<" ";
for(ll j=0;j<v1[i].size();j++) {
idx=v1[i][j];
// cout<<idx<<" ";
C[idx]=(q-i)-getSum(max(A[idx].f,A[idx].sc)-1);
fix[idx]=1;
}
//cout<<endl;
updateBIT(B[i],1);
}
//cout<<endl;
for(ll j=0;j<v1[0].size();j++) {
idx=v1[i][j];
C[idx]=q-getSum(max(A[idx].f,A[idx].sc)-1);
}
for(ll i=1;i<=n;i++) {
A[i].f=mp1[A[i].f]; A[i].sc=mp1[A[i].sc];
//cout<<A[i].f<<" "<<A[i].sc<<" "<<" "<<C[i]<<" "<<fix[i]<<" "<<ans<<endl;
if(A[i].f==A[i].sc) { ans+=A[i].f; continue; }
if(fix[i]==0 || A[i].f==max(A[i].f,A[i].sc)) {
if(C[i]%2==1) ans+=A[i].sc;
else ans+=A[i].f;
}
else {
if(C[i]%2==1) ans+=A[i].f;
else ans+=A[i].sc;
}
}
cout<<ans;
}
/*
5 3
4 6
9 1
8 8
4 2
3 7
8
2
9
*/
Compilation message
fortune_telling2.cpp: In function 'void shekumshe(std::vector<long long int>)':
fortune_telling2.cpp:16:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(ll i=1;i<vc.size();i++) {
| ~^~~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:86:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(ll j=0;j<v1[i].size();j++) {
| ~^~~~~~~~~~~~~
fortune_telling2.cpp:96:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(ll j=0;j<v1[0].size();j++) {
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12236 KB |
Output is correct |
2 |
Correct |
10 ms |
12364 KB |
Output is correct |
3 |
Correct |
11 ms |
12620 KB |
Output is correct |
4 |
Correct |
11 ms |
12620 KB |
Output is correct |
5 |
Correct |
11 ms |
12748 KB |
Output is correct |
6 |
Correct |
13 ms |
12544 KB |
Output is correct |
7 |
Correct |
11 ms |
12620 KB |
Output is correct |
8 |
Correct |
11 ms |
12576 KB |
Output is correct |
9 |
Correct |
11 ms |
12568 KB |
Output is correct |
10 |
Correct |
9 ms |
12236 KB |
Output is correct |
11 |
Correct |
10 ms |
12364 KB |
Output is correct |
12 |
Correct |
10 ms |
12412 KB |
Output is correct |
13 |
Correct |
10 ms |
12404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12236 KB |
Output is correct |
2 |
Correct |
10 ms |
12364 KB |
Output is correct |
3 |
Correct |
11 ms |
12620 KB |
Output is correct |
4 |
Correct |
11 ms |
12620 KB |
Output is correct |
5 |
Correct |
11 ms |
12748 KB |
Output is correct |
6 |
Correct |
13 ms |
12544 KB |
Output is correct |
7 |
Correct |
11 ms |
12620 KB |
Output is correct |
8 |
Correct |
11 ms |
12576 KB |
Output is correct |
9 |
Correct |
11 ms |
12568 KB |
Output is correct |
10 |
Correct |
9 ms |
12236 KB |
Output is correct |
11 |
Correct |
10 ms |
12364 KB |
Output is correct |
12 |
Correct |
10 ms |
12412 KB |
Output is correct |
13 |
Correct |
10 ms |
12404 KB |
Output is correct |
14 |
Correct |
57 ms |
17228 KB |
Output is correct |
15 |
Correct |
131 ms |
22484 KB |
Output is correct |
16 |
Correct |
220 ms |
28292 KB |
Output is correct |
17 |
Correct |
355 ms |
33056 KB |
Output is correct |
18 |
Correct |
343 ms |
33164 KB |
Output is correct |
19 |
Correct |
345 ms |
33000 KB |
Output is correct |
20 |
Correct |
330 ms |
33124 KB |
Output is correct |
21 |
Correct |
298 ms |
33284 KB |
Output is correct |
22 |
Correct |
181 ms |
27656 KB |
Output is correct |
23 |
Correct |
163 ms |
24192 KB |
Output is correct |
24 |
Correct |
146 ms |
22004 KB |
Output is correct |
25 |
Correct |
215 ms |
29580 KB |
Output is correct |
26 |
Correct |
184 ms |
26424 KB |
Output is correct |
27 |
Correct |
281 ms |
27788 KB |
Output is correct |
28 |
Correct |
197 ms |
27768 KB |
Output is correct |
29 |
Correct |
264 ms |
30344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12236 KB |
Output is correct |
2 |
Correct |
10 ms |
12364 KB |
Output is correct |
3 |
Correct |
11 ms |
12620 KB |
Output is correct |
4 |
Correct |
11 ms |
12620 KB |
Output is correct |
5 |
Correct |
11 ms |
12748 KB |
Output is correct |
6 |
Correct |
13 ms |
12544 KB |
Output is correct |
7 |
Correct |
11 ms |
12620 KB |
Output is correct |
8 |
Correct |
11 ms |
12576 KB |
Output is correct |
9 |
Correct |
11 ms |
12568 KB |
Output is correct |
10 |
Correct |
9 ms |
12236 KB |
Output is correct |
11 |
Correct |
10 ms |
12364 KB |
Output is correct |
12 |
Correct |
10 ms |
12412 KB |
Output is correct |
13 |
Correct |
10 ms |
12404 KB |
Output is correct |
14 |
Correct |
57 ms |
17228 KB |
Output is correct |
15 |
Correct |
131 ms |
22484 KB |
Output is correct |
16 |
Correct |
220 ms |
28292 KB |
Output is correct |
17 |
Correct |
355 ms |
33056 KB |
Output is correct |
18 |
Correct |
343 ms |
33164 KB |
Output is correct |
19 |
Correct |
345 ms |
33000 KB |
Output is correct |
20 |
Correct |
330 ms |
33124 KB |
Output is correct |
21 |
Correct |
298 ms |
33284 KB |
Output is correct |
22 |
Correct |
181 ms |
27656 KB |
Output is correct |
23 |
Correct |
163 ms |
24192 KB |
Output is correct |
24 |
Correct |
146 ms |
22004 KB |
Output is correct |
25 |
Correct |
215 ms |
29580 KB |
Output is correct |
26 |
Correct |
184 ms |
26424 KB |
Output is correct |
27 |
Correct |
281 ms |
27788 KB |
Output is correct |
28 |
Correct |
197 ms |
27768 KB |
Output is correct |
29 |
Correct |
264 ms |
30344 KB |
Output is correct |
30 |
Correct |
594 ms |
49204 KB |
Output is correct |
31 |
Correct |
936 ms |
66248 KB |
Output is correct |
32 |
Correct |
1433 ms |
82532 KB |
Output is correct |
33 |
Correct |
2431 ms |
123336 KB |
Output is correct |
34 |
Correct |
551 ms |
45968 KB |
Output is correct |
35 |
Correct |
2402 ms |
123460 KB |
Output is correct |
36 |
Correct |
2524 ms |
123064 KB |
Output is correct |
37 |
Correct |
2513 ms |
123092 KB |
Output is correct |
38 |
Correct |
2542 ms |
123348 KB |
Output is correct |
39 |
Correct |
2563 ms |
123132 KB |
Output is correct |
40 |
Correct |
2334 ms |
124308 KB |
Output is correct |
41 |
Correct |
2504 ms |
122956 KB |
Output is correct |
42 |
Correct |
2402 ms |
122844 KB |
Output is correct |
43 |
Correct |
1416 ms |
113324 KB |
Output is correct |
44 |
Correct |
1316 ms |
114148 KB |
Output is correct |
45 |
Correct |
1322 ms |
116560 KB |
Output is correct |
46 |
Correct |
1210 ms |
75216 KB |
Output is correct |
47 |
Correct |
971 ms |
57280 KB |
Output is correct |
48 |
Correct |
1677 ms |
88224 KB |
Output is correct |
49 |
Correct |
1538 ms |
88680 KB |
Output is correct |