#include<bits/stdc++.h>
using namespace std;
#define N 200005
class node{
public:
node *l,*r;
int cnt;
node(int cnt):l(NULL),r(NULL),cnt(cnt){}
node(node *l,node *r):l(l),r(r),cnt(0){
if(l)cnt+=l->cnt;
if(r)cnt+=r->cnt;
}
};
using pnode = node*;
pnode root[N];
pnode cre(int l,int r){
if(l==r)return new node(0);
int mid=(l+r)/2;
return new node(cre(l,mid),cre(mid+1,r));
}
pnode update(pnode x,int l,int r,int p){
if(l==r)return new node(x->cnt+1);
int mid=(l+r)/2;
if(p<=mid)return new node(update(x->l,l,mid,p),x->r);
else return new node(x->l,update(x->r,mid+1,r,p));
}
int sol(pnode x,int l,int r,int ll,int rr){
if(l>rr||ll>r||ll>rr)return 0;
if(ll<=l&&r<=rr)return x->cnt;
int mid=(l+r)/2;
return sol(x->l,l,mid,ll,rr)+sol(x->r,mid+1,r,ll,rr);
}
int a[N],b[N],t[N],sz;
long long ans;
vector<int> v;
map<int,int> mp;
int main(){
int n,m,i,pmi,pma,pos,l,r,mid,cnt,sum;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++){
scanf("%d %d",&a[i],&b[i]);
}
for(i=1;i<=m;i++)scanf("%d",&t[i]),mp[t[i]],v.push_back(t[i]);
v.push_back(-1);
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
for(auto &[x,y]:mp)y=++sz;
root[0]=cre(1,sz);
for(i=1;i<=m;i++){
root[i]=update(root[i-1],1,sz,mp[t[i]]);
}
for(i=1;i<=n;i++){
auto [mi,ma]=minmax(a[i],b[i]);
pmi=lower_bound(v.begin(),v.end(),mi)-v.begin();
pma=lower_bound(v.begin(),v.end(),ma)-v.begin();
pos=0;
l=1,r=m;
sum=sol(root[m],1,sz,pmi,pma-1);
while(l<=r){
mid=(l+r)/2;
cnt=sum-sol(root[mid-1],1,sz,pmi,pma-1);
if(cnt)pos=mid,l=mid+1;
else r=mid-1;
}
if(pos){
cnt=sol(root[m],1,sz,pma,sz)-sol(root[pos],1,sz,pma,sz);
ans+=(cnt%2)?mi:ma;
}
else{
cnt=sol(root[m],1,sz,pma,sz);
ans+=(cnt%2)?b[i]:a[i];
}
}
printf("%lld",ans);
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
fortune_telling2.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d %d",&a[i],&b[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:50:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | for(i=1;i<=m;i++)scanf("%d",&t[i]),mp[t[i]],v.push_back(t[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
820 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
820 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
57 ms |
6344 KB |
Output is correct |
15 |
Correct |
109 ms |
12852 KB |
Output is correct |
16 |
Correct |
171 ms |
19392 KB |
Output is correct |
17 |
Correct |
308 ms |
26316 KB |
Output is correct |
18 |
Correct |
241 ms |
26332 KB |
Output is correct |
19 |
Correct |
324 ms |
26388 KB |
Output is correct |
20 |
Correct |
293 ms |
26392 KB |
Output is correct |
21 |
Correct |
311 ms |
26268 KB |
Output is correct |
22 |
Correct |
191 ms |
24872 KB |
Output is correct |
23 |
Correct |
170 ms |
24512 KB |
Output is correct |
24 |
Correct |
198 ms |
24148 KB |
Output is correct |
25 |
Correct |
215 ms |
25288 KB |
Output is correct |
26 |
Correct |
173 ms |
26332 KB |
Output is correct |
27 |
Correct |
245 ms |
26388 KB |
Output is correct |
28 |
Correct |
189 ms |
26260 KB |
Output is correct |
29 |
Correct |
172 ms |
26388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
820 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
57 ms |
6344 KB |
Output is correct |
15 |
Correct |
109 ms |
12852 KB |
Output is correct |
16 |
Correct |
171 ms |
19392 KB |
Output is correct |
17 |
Correct |
308 ms |
26316 KB |
Output is correct |
18 |
Correct |
241 ms |
26332 KB |
Output is correct |
19 |
Correct |
324 ms |
26388 KB |
Output is correct |
20 |
Correct |
293 ms |
26392 KB |
Output is correct |
21 |
Correct |
311 ms |
26268 KB |
Output is correct |
22 |
Correct |
191 ms |
24872 KB |
Output is correct |
23 |
Correct |
170 ms |
24512 KB |
Output is correct |
24 |
Correct |
198 ms |
24148 KB |
Output is correct |
25 |
Correct |
215 ms |
25288 KB |
Output is correct |
26 |
Correct |
173 ms |
26332 KB |
Output is correct |
27 |
Correct |
245 ms |
26388 KB |
Output is correct |
28 |
Correct |
189 ms |
26260 KB |
Output is correct |
29 |
Correct |
172 ms |
26388 KB |
Output is correct |
30 |
Correct |
711 ms |
142692 KB |
Output is correct |
31 |
Correct |
1117 ms |
142900 KB |
Output is correct |
32 |
Correct |
1643 ms |
143240 KB |
Output is correct |
33 |
Correct |
2352 ms |
143944 KB |
Output is correct |
34 |
Correct |
604 ms |
144316 KB |
Output is correct |
35 |
Correct |
2498 ms |
149860 KB |
Output is correct |
36 |
Correct |
2352 ms |
149752 KB |
Output is correct |
37 |
Correct |
2368 ms |
149764 KB |
Output is correct |
38 |
Correct |
2403 ms |
149756 KB |
Output is correct |
39 |
Correct |
2339 ms |
149708 KB |
Output is correct |
40 |
Correct |
1870 ms |
149548 KB |
Output is correct |
41 |
Correct |
2326 ms |
149780 KB |
Output is correct |
42 |
Correct |
2276 ms |
149768 KB |
Output is correct |
43 |
Correct |
1349 ms |
149048 KB |
Output is correct |
44 |
Correct |
1443 ms |
149084 KB |
Output is correct |
45 |
Correct |
1550 ms |
149076 KB |
Output is correct |
46 |
Correct |
1512 ms |
138948 KB |
Output is correct |
47 |
Correct |
1208 ms |
135116 KB |
Output is correct |
48 |
Correct |
1887 ms |
149820 KB |
Output is correct |
49 |
Correct |
1639 ms |
149820 KB |
Output is correct |