#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
#define int long long
vector<int>xs;
int bit[2][3000];
void add(int t,int k,int x){
k=lower_bound(xs.begin(),xs.end(),k)-xs.begin()+1;
while(k<3000){
bit[t][k]+=x;
k+=k&-k;
}
}
int query(int t,int k){
k=lower_bound(xs.begin(),xs.end(),k)-xs.begin()+1;
int res=0;
while(k){
res+=bit[t][k];
k-=k&-k;
}
return res;
}
char p[200000][2],q[200000][2];
int s[200000],t[200000];
signed main(){
int K,n;cin>>K>>n;
if(K==1){
vector<int>v;
ll sum=0;
rep(i,n){
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
if(p[i][0]==q[i][0]){
sum+=abs(s[i]-t[i]);
continue;
}
sum++;
v.push_back(s[i]);
v.push_back(t[i]);
}
sort(v.begin(),v.end());
if(!v.empty()){
int md=v[v.size()/2];
for(int i:v)sum+=abs(i-md);
}
cout<<sum<<endl;
return 0;
}
ll sum=0;
vector<P>v;
rep(i,n){
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
if(p[i][0]==q[i][0]){
sum+=abs(s[i]-t[i]);
continue;
}
sum++;
v.push_back(P(min(s[i],t[i]),max(s[i],t[i])));
xs.push_back(s[i]);
xs.push_back(t[i]);
}
if(v.empty()){
cout<<sum<<endl;
return 0;
}
sort(xs.begin(),xs.end());
xs.erase(unique(xs.begin(),xs.end()),xs.end());
ll Min=LLONG_MAX;
for(int i:xs){
rep(j,2){
memset(bit[j],0,sizeof(bit[j]));
}
ll cnt=0;
for(auto p:v){
if(p.first<=i){
cnt+=abs(p.first-i)+abs(p.second-i);
continue;
}
add(1,0,abs(p.first-i)+abs(p.second-i));// a
add(1,i,-abs(p.first-i)-abs(p.second-i));// a'
add(0,i,-2);// b
add(1,i,abs(p.first-i)+abs(p.second-i)-(-2*i));// c
add(0,p.first,2);// b'
add(1,p.first,-abs(p.first-i)-abs(p.second-i)+(-2*i));// c'
add(1,p.first,p.second-p.first);// d
add(1,p.second,p.first-p.second);// d'
add(0,p.second,2);//e
add(1,p.second,(p.second-p.first)-2*p.second);//f
int ok=p.second,ng=1e9+2;
while(ng-ok>1){
int t=(ok+ng)/2;
if(abs(t-p.first)+abs(t-p.second)<=abs(p.first-i)+abs(p.second-i))ok=t;
else ng=t;
}
add(0,ng,-2);//e'
add(1,ng,-((p.second-p.first)-2*p.second));//f'
add(1,ng,abs(p.first-i)+abs(p.second-i));//g
//~ cout<<"---- "<<i<<' '<<p.first<<' '<<p.second<<" ----"<<endl;
//~ for(int j=0;j<=10;j++){
//~ cout<<j<<' '<<query(0,j)*j+query(1,j)<<endl;
//~ }
}
ll d=LLONG_MAX;
for(int j:xs){
d=min(d,query(0,j)*(ll)j+query(1,j));
}
Min=min(Min,cnt+d);
}
cout<<sum+Min<<endl;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:35:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
356 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
47 ms |
4068 KB |
Output is correct |
13 |
Correct |
81 ms |
3984 KB |
Output is correct |
14 |
Correct |
47 ms |
3820 KB |
Output is correct |
15 |
Correct |
47 ms |
2540 KB |
Output is correct |
16 |
Correct |
56 ms |
4068 KB |
Output is correct |
17 |
Correct |
61 ms |
4036 KB |
Output is correct |
18 |
Correct |
73 ms |
3940 KB |
Output is correct |
19 |
Correct |
77 ms |
4068 KB |
Output is correct |
20 |
Correct |
68 ms |
4068 KB |
Output is correct |
21 |
Correct |
57 ms |
3944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
7 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
13 ms |
384 KB |
Output is correct |
9 |
Correct |
9 ms |
384 KB |
Output is correct |
10 |
Correct |
14 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
9 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
13 ms |
384 KB |
Output is correct |
9 |
Correct |
8 ms |
512 KB |
Output is correct |
10 |
Correct |
12 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
13 ms |
436 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
24 ms |
512 KB |
Output is correct |
15 |
Correct |
966 ms |
476 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
15 ms |
512 KB |
Output is correct |
18 |
Correct |
145 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1176 ms |
512 KB |
Output is correct |
21 |
Correct |
765 ms |
504 KB |
Output is correct |
22 |
Correct |
1216 ms |
636 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
1083 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
7 ms |
428 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
12 ms |
384 KB |
Output is correct |
9 |
Correct |
8 ms |
384 KB |
Output is correct |
10 |
Correct |
12 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
77 ms |
512 KB |
Output is correct |
15 |
Correct |
1112 ms |
484 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
13 ms |
512 KB |
Output is correct |
18 |
Correct |
141 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
1147 ms |
608 KB |
Output is correct |
21 |
Correct |
724 ms |
512 KB |
Output is correct |
22 |
Correct |
1251 ms |
520 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
1098 ms |
608 KB |
Output is correct |
25 |
Correct |
40 ms |
5756 KB |
Output is correct |
26 |
Correct |
1906 ms |
6004 KB |
Output is correct |
27 |
Runtime error |
173 ms |
11488 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
28 |
Halted |
0 ms |
0 KB |
- |