#include <bits/stdc++.h>
#define stop system("pause")
#define stop2 char o; cin >> o
#define INP freopen("pcb.in","r",stdin)
#define OUTP freopen ("pcb.out","w",stdout)
#define int long long
using namespace std;
const int maxn = 1e5+228;
int up[maxn],down[maxn];
int prefup[maxn],prefdown[maxn];
int sufup[maxn],sufdown[maxn];
unordered_map<int,int> cntup,cntdown;
unordered_map<int,int> rl;
main(){
ios_base::sync_with_stdio(0);
int k,n; cin >> k >> n;
int ans = 0;
set<int> qwe;
int all = 0;
for(int i(0); i < n;i++){
char p,q;
int s,t;
cin >> p >> s >> q >> t;
if(p == q){
ans+=abs(s-t);
continue;
}
if(p == 'B'){
swap(p,q);
swap(s,t);
}
qwe.insert(s);
qwe.insert(t);
cntup[s]++;
cntdown[t]++;
all++;
}
int pnt = 0;
for(auto&a : qwe){
up[pnt]+=cntup[a];
down[pnt]+=cntdown[a];
rl[pnt] = a;
pnt++;
}
int now1 = up[0];
int now2 = down[0];
for(int i(1); i < pnt;i++){
prefup[i]=prefup[i-1];
prefdown[i]=prefdown[i-1];
prefup[i]+=now1*(rl[i]-rl[i-1]);
prefdown[i]+=now2*(rl[i]-rl[i-1]);
now1+=up[i];
now2+=down[i];
//cout << rl[i] << ' ' << i << ' ' << prefdown[i] << endl;
}
//cout << now1 << ' ' << now2 << endl;
//cout << pnt << ' ' << prefup[pnt-1] << endl;
now1 = up[pnt-1];
now2 = down[pnt-1];
for(int i(pnt-2); i >= 0;i--){
sufup[i]=sufup[i+1];
sufdown[i]=sufdown[i+1];
sufup[i]+=now1*(rl[i+1]-rl[i]);
sufdown[i]+=now2*(rl[i+1]-rl[i]);
now1+=up[i];
now2+=down[i];
}
int res = 1e15;
for(int i(0); i < pnt;i++){
//cout << rl[i] << ' ' << sufdown[i] << ' ' << sufup[i] << ' ' << prefdown[i] << ' ' << prefup[i] << endl;
res = min(res,sufdown[i]+sufup[i]+prefdown[i]+prefup[i]);
}
cout << ans+all+res;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message
bridge.cpp:17:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |