This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pairll pair<ll,ll>
#define lpairll pair<ll,pairll>
#define repp(i,a,b) for (ll i = a; i <= b; i++)
#define repz(i,a,b) for (ll i = a; i < b; i++)
#define repm(i,a,b) for (ll i = a; i >= b; i--)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
const ll N = 3e5+5, MOD = 1e9+7, M = 1e5+5;
ll tc = 1, n, m, lr[N], rr[N];
ll x, y, k, otsum, dg;
string s, s1, s2, ye = "YES", no = "NO";
vector<pairll> v;
ll kir[N], kan[N], val[N];
map<ll,ll> mapz;
void input(){
cin >> k >> n;
repp(i,1,n){
char A, B;
ll L, R;
cin >> A >> L >> B >> R;
if (L > R) swap(L,R);
if (A == B){
otsum += abs(L-R);
}
else{
otsum += abs(L-R)+1;
v.pb(mp(L,0));
v.pb(mp(R,1));
ll md = (L+R)/2;
v.pb(mp(md,2));
v.pb(mp(md+1,2));
}
}
}
void solve(){
if (k == 1){
sort(v.begin(),v.end());
ll cur = -1, num = 0;
for (auto i : v){
ll idx;
if (i.fr > cur){
cur = i.fr;
num++;
val[num] = cur;
idx = num;
}
else idx = num;
if (i.sc == 0) lr[num]++;
if (i.sc == 1) rr[num]++;
}
ll ans = 1e18;
ll nyal = 0;
// repp(i,1,num){
// cout << i << " " << val[i] << " " << lr[i] << " " << rr[i] << endl;
// }
repp(i,1,num){
kir[i] = kir[i-1]+nyal*2*(val[i]-val[i-1]);
nyal += rr[i];
}
nyal = 0;
val[num+1] = val[num];
repm(i,num,1){
kan[i] = kan[i+1]+nyal*2*(val[i+1]-val[i]);
nyal += lr[i];
}
repp(i,1,num){
ans = min(ans,kir[i]+kan[i]);
//cout << i << " " << kir[i] << " " << kan[i] << endl;
}
cout << ans+otsum << endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
//cin >> tc;
while(tc--){
input();
solve();
}
}
Compilation message (stderr)
bridge.cpp: In function 'void solve()':
bridge.cpp:51:10: warning: variable 'idx' set but not used [-Wunused-but-set-variable]
51 | ll idx;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |