This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 9;
const ll Log2 = 20;
const ll inf = 1e16 + 7;
typedef pair<ll,ll> LL;
vector<ll> v;
ll k,n,ans,inc,in[N],out[N];
struct human{
char x,y;
ll L,R;
};
human a[N];
ll Find(ll x){
return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;
}
void compress(){
sort(v.begin(),v.end()); v.resize(unique(v.begin(),v.end()) - v.begin());
//for (ll i = 1;i <= n;i++) a[i].L = Find(a[i].L),a[i].R = Find(a[i].R);
}
ll bit1[N],bit2[N];
void upd(ll p,ll val,ll bit[]){
for (ll i = p;i < N;i += i & -i) bit[i] += val;
}
ll Get(ll p,ll bit[]){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res += bit[i];
return res;
}
ll cal_opt(ll opt){
ll inc = 0;
for (ll i = 1;i <= n;i++){
if (a[i].x == a[i].y) continue;
if (a[i].L > opt) inc += 2*(a[i].L - opt);
if (a[i].R < opt) inc += 2*(opt - a[i].R);
}
return inc;
}
ll cal_opt2(ll opt1,ll opt2){
ll inc = 0;
for (ll i = 1;i <= n;i++){
if (a[i].x == a[i].y) continue;
ll now1 = 0,now2 = 0;
if (a[i].L > opt1) now1 = 2*(a[i].L - opt1);
if (a[i].R < opt1) now1 = 2*(opt1 - a[i].R);
if (a[i].L > opt2) now2 = 2*(a[i].L - opt2);
if (a[i].R < opt2) now2 = 2*(opt2 - a[i].R);
inc += min(now1,now2);
}
return inc;
}
void Sub1(){
ll inc = inf;
for (ll i = 1;i <= n;i++)
inc = min(inc,cal_opt(a[i].L)),inc = min(inc,cal_opt(a[i].R));
cout<<ans + inc;
}
void Sub2(){
ll inc = inf;
for (ll i = 0;i < v.size();i++){
for (ll j = i + 1;j < v.size();j++) inc = min(inc,cal_opt2(v[i],v[j]));
}
cout<<ans + inc;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "tst"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
}
cin>>k>>n;
for (ll i = 1;i <= n;i++){
cin>>a[i].x>>a[i].L>>a[i].y>>a[i].R;
if (a[i].L > a[i].R) swap(a[i].L,a[i].R);
ans += a[i].R - a[i].L;
v.push_back(a[i].L); v.push_back(a[i].R);
if (a[i].x != a[i].y) ans++;
}
compress();
if (k == 1) Sub1();
else Sub2();
}
Compilation message (stderr)
bridge.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization "O2"
|
bridge.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
bridge.cpp: In function 'void Sub2()':
bridge.cpp:77:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (ll i = 0;i < v.size();i++){
| ~~^~~~~~~~~~
bridge.cpp:78:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (ll j = i + 1;j < v.size();j++) inc = min(inc,cal_opt2(v[i],v[j]));
| ~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |