#include<bits/stdc++.h>
#define MAX_N 100005
#define inf (1LL<<60)
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pil;
struct node{
LL cnt,sum;
node *l,*r;
node(LL cnt,LL sum,node *l,node *r):cnt(cnt),sum(sum),l(l),r(r){}
}*R1,*R2;
LL n,k,m;
LL ans,aa;
vector<LL> X;
pil arr[MAX_N];
pil serch(LL idx,node *x,LL cnt,LL p){
if(idx<0 && p>=X.size()) return {0,0};
if(idx<0) return {X[p],X[p]*cnt};
if(x->l->cnt<cnt){
pil q=serch(idx-1,x->r,cnt-x->l->cnt,(p|(1LL<<idx)));
q.se+=x->l->sum;
return q;
}
return serch(idx-1,x->l,cnt,p);
}
LL read(node *RX){
pil p=serch(17,RX,RX->cnt/2,0);
return (RX->cnt/2)*p.fi-p.se+(RX->sum-p.se)-(RX->cnt-RX->cnt/2)*p.fi;
}
void make_tree(LL idx,node *RX){
if(idx<0) return;
RX->l=new node(0,0,NULL,NULL);
RX->r=new node(0,0,NULL,NULL);
make_tree(idx-1,RX->l);make_tree(idx-1,RX->r);
}
void update(LL idx,node *RX,LL p,LL x){
RX->cnt+=x;
RX->sum+=x*X[p];
if(idx<0) return;
if(p>>idx&(1LL)) update(idx-1,RX->r,p,x);
else update(idx-1,RX->l,p,x);
}
int main(){
LL i,x;
char a[5],b[5];
scanf("%lld %lld",&k,&n);
for(i=1;i<=n;i++){
scanf("%s %lld %s %lld",a,&arr[m].fi,b,&arr[m].se);
if(a[0]==b[0]){
aa+=abs(arr[m].fi-arr[m].se);
continue;
}
X.pb(arr[m].fi);X.pb(arr[m].se);
m++;
aa++;
}
sort(X.begin(),X.end());X.erase(unique(X.begin(),X.end()),X.end());
sort(arr,arr+m,[&](const pil x,const pil y){
return x.fi+x.se<y.fi+y.se;
});
R1=new node(0,0,NULL,NULL);make_tree(17,R1);
R2=new node(0,0,NULL,NULL);make_tree(17,R2);
for(i=0;i<m;i++){
update(17,R2,(lower_bound(X.begin(),X.end(),arr[i].fi)-X.begin()),1);
update(17,R2,(lower_bound(X.begin(),X.end(),arr[i].se)-X.begin()),1);
}
ans=read(R2);
if(k==1) return !printf("%lld\n",aa+ans);
for(i=0;i<m;i++){
update(17,R1,(lower_bound(X.begin(),X.end(),arr[i].fi)-X.begin()),1);
update(17,R1,(lower_bound(X.begin(),X.end(),arr[i].se)-X.begin()),1);
update(17,R2,(lower_bound(X.begin(),X.end(),arr[i].fi)-X.begin()),-1);
update(17,R2,(lower_bound(X.begin(),X.end(),arr[i].se)-X.begin()),-1);
x=read(R1)+read(R2);
ans=min(ans,x);
}
printf("%lld\n",aa+ans);
return 0;
}
Compilation message
bridge.cpp: In function 'pil serch(LL, node*, LL, LL)':
bridge.cpp:20:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx<0 && p>=X.size()) return {0,0};
^
bridge.cpp: In function 'int main()':
bridge.cpp:49:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&k,&n);
^
bridge.cpp:51:53: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s %lld %s %lld",a,&arr[m].fi,b,&arr[m].se);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
49528 KB |
Output is correct |
2 |
Correct |
61 ms |
49648 KB |
Output is correct |
3 |
Correct |
87 ms |
49816 KB |
Output is correct |
4 |
Correct |
62 ms |
49816 KB |
Output is correct |
5 |
Correct |
83 ms |
49816 KB |
Output is correct |
6 |
Correct |
67 ms |
49816 KB |
Output is correct |
7 |
Correct |
63 ms |
49816 KB |
Output is correct |
8 |
Correct |
64 ms |
49884 KB |
Output is correct |
9 |
Correct |
63 ms |
49884 KB |
Output is correct |
10 |
Correct |
63 ms |
49884 KB |
Output is correct |
11 |
Correct |
63 ms |
49884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
49884 KB |
Output is correct |
2 |
Correct |
71 ms |
49884 KB |
Output is correct |
3 |
Correct |
68 ms |
49884 KB |
Output is correct |
4 |
Correct |
63 ms |
49884 KB |
Output is correct |
5 |
Correct |
72 ms |
49900 KB |
Output is correct |
6 |
Correct |
71 ms |
49900 KB |
Output is correct |
7 |
Correct |
62 ms |
49900 KB |
Output is correct |
8 |
Correct |
68 ms |
49900 KB |
Output is correct |
9 |
Correct |
62 ms |
49900 KB |
Output is correct |
10 |
Correct |
72 ms |
49900 KB |
Output is correct |
11 |
Correct |
62 ms |
49900 KB |
Output is correct |
12 |
Correct |
125 ms |
53080 KB |
Output is correct |
13 |
Correct |
277 ms |
53080 KB |
Output is correct |
14 |
Correct |
153 ms |
53080 KB |
Output is correct |
15 |
Correct |
165 ms |
53080 KB |
Output is correct |
16 |
Correct |
125 ms |
53080 KB |
Output is correct |
17 |
Correct |
163 ms |
53080 KB |
Output is correct |
18 |
Correct |
149 ms |
53080 KB |
Output is correct |
19 |
Correct |
163 ms |
53080 KB |
Output is correct |
20 |
Correct |
128 ms |
53080 KB |
Output is correct |
21 |
Correct |
153 ms |
53080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
53080 KB |
Output is correct |
2 |
Correct |
63 ms |
53080 KB |
Output is correct |
3 |
Correct |
62 ms |
53080 KB |
Output is correct |
4 |
Correct |
68 ms |
53080 KB |
Output is correct |
5 |
Correct |
63 ms |
53080 KB |
Output is correct |
6 |
Correct |
74 ms |
53080 KB |
Output is correct |
7 |
Correct |
62 ms |
53080 KB |
Output is correct |
8 |
Correct |
73 ms |
53080 KB |
Output is correct |
9 |
Correct |
62 ms |
53080 KB |
Output is correct |
10 |
Correct |
61 ms |
53080 KB |
Output is correct |
11 |
Correct |
72 ms |
53080 KB |
Output is correct |
12 |
Correct |
62 ms |
53080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
53080 KB |
Output is correct |
2 |
Correct |
63 ms |
53080 KB |
Output is correct |
3 |
Correct |
63 ms |
53080 KB |
Output is correct |
4 |
Correct |
64 ms |
53080 KB |
Output is correct |
5 |
Correct |
61 ms |
53080 KB |
Output is correct |
6 |
Correct |
65 ms |
53080 KB |
Output is correct |
7 |
Correct |
61 ms |
53080 KB |
Output is correct |
8 |
Correct |
62 ms |
53080 KB |
Output is correct |
9 |
Correct |
61 ms |
53080 KB |
Output is correct |
10 |
Correct |
61 ms |
53080 KB |
Output is correct |
11 |
Correct |
62 ms |
53080 KB |
Output is correct |
12 |
Correct |
63 ms |
53080 KB |
Output is correct |
13 |
Correct |
69 ms |
53080 KB |
Output is correct |
14 |
Correct |
66 ms |
53080 KB |
Output is correct |
15 |
Correct |
64 ms |
53080 KB |
Output is correct |
16 |
Correct |
70 ms |
53080 KB |
Output is correct |
17 |
Correct |
62 ms |
53080 KB |
Output is correct |
18 |
Correct |
62 ms |
53080 KB |
Output is correct |
19 |
Correct |
63 ms |
53080 KB |
Output is correct |
20 |
Correct |
68 ms |
53080 KB |
Output is correct |
21 |
Correct |
63 ms |
53080 KB |
Output is correct |
22 |
Correct |
66 ms |
53080 KB |
Output is correct |
23 |
Correct |
67 ms |
53080 KB |
Output is correct |
24 |
Correct |
64 ms |
53080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
53080 KB |
Output is correct |
2 |
Correct |
61 ms |
53080 KB |
Output is correct |
3 |
Correct |
62 ms |
53080 KB |
Output is correct |
4 |
Correct |
62 ms |
53080 KB |
Output is correct |
5 |
Correct |
68 ms |
53080 KB |
Output is correct |
6 |
Correct |
73 ms |
53080 KB |
Output is correct |
7 |
Correct |
68 ms |
53080 KB |
Output is correct |
8 |
Correct |
76 ms |
53080 KB |
Output is correct |
9 |
Correct |
61 ms |
53080 KB |
Output is correct |
10 |
Correct |
61 ms |
53080 KB |
Output is correct |
11 |
Correct |
63 ms |
53080 KB |
Output is correct |
12 |
Correct |
65 ms |
53080 KB |
Output is correct |
13 |
Correct |
64 ms |
53080 KB |
Output is correct |
14 |
Correct |
63 ms |
53080 KB |
Output is correct |
15 |
Correct |
65 ms |
53080 KB |
Output is correct |
16 |
Correct |
62 ms |
53080 KB |
Output is correct |
17 |
Correct |
63 ms |
53080 KB |
Output is correct |
18 |
Correct |
62 ms |
53080 KB |
Output is correct |
19 |
Correct |
72 ms |
53080 KB |
Output is correct |
20 |
Correct |
64 ms |
53080 KB |
Output is correct |
21 |
Correct |
63 ms |
53080 KB |
Output is correct |
22 |
Correct |
72 ms |
53080 KB |
Output is correct |
23 |
Correct |
64 ms |
53080 KB |
Output is correct |
24 |
Correct |
64 ms |
53080 KB |
Output is correct |
25 |
Correct |
165 ms |
53080 KB |
Output is correct |
26 |
Correct |
187 ms |
53080 KB |
Output is correct |
27 |
Correct |
659 ms |
53080 KB |
Output is correct |
28 |
Correct |
671 ms |
53080 KB |
Output is correct |
29 |
Correct |
689 ms |
53080 KB |
Output is correct |
30 |
Correct |
337 ms |
53080 KB |
Output is correct |
31 |
Correct |
170 ms |
53080 KB |
Output is correct |
32 |
Correct |
254 ms |
53080 KB |
Output is correct |
33 |
Correct |
292 ms |
53080 KB |
Output is correct |
34 |
Correct |
268 ms |
53080 KB |
Output is correct |
35 |
Correct |
187 ms |
53080 KB |
Output is correct |
36 |
Correct |
270 ms |
53100 KB |
Output is correct |
37 |
Correct |
172 ms |
53100 KB |
Output is correct |