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 = 1e5 + 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());
}
ll bit1[N],bit2[N],bitVal1[N],bitVal2[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;
}
void Sub1(){
for (ll i = 1;i <= n;i++){
if (a[i].x != a[i].y) in[Find(a[i].L)]++,out[Find(a[i].R)]++;
}
for (ll i = 1;i < N;i++) upd(i,out[i],bit1),upd(i,in[i],bit2);
ll opt,inc = inf;
for (ll j = 0;j < v.size();j++){
ll i = j + 1;
ll l = Get(i - 1,bit1),r = Get(N - 1,bit2) - Get(i,bit2);
if (l >= r){
opt = j; break;
}
}
cout<<ans + min(cal_opt(v[opt]),cal_opt(v[opt - 1]));
}
bool lf(human x,human y){
return x.L + x.R < y.L + y.R;
}
ll kq[N][2];
priority_queue<ll> pq1;
priority_queue<ll,vector<ll>,greater<ll>> pq2;
void Push_in(ll x,ll y){
pq1.push(x); pq1.push(y);
while(pq1.size() > pq2.size()) pq2.push(pq1.top()),pq1.pop();
while(pq2.top() < pq1.top()){
ll x = pq1.top(),y = pq2.top();
pq1.pop(); pq2.pop(); pq1.push(y); pq2.push(x);
}
}
void oper(ll cond){
while(!pq1.empty()) pq1.pop();
while(!pq2.empty()) pq2.pop();
memset(bit1,0,sizeof(bit1)); memset(bit2,0,sizeof(bit2));
memset(bitVal1,0,sizeof(bitVal1)); memset(bitVal2,0,sizeof(bitVal2));
/// bit1 out bit2 in bitVal12 val
for (ll i = 1;i <= n;i++){
if (a[i].x == a[i].y) continue;
upd(a[i].R,1,bit1); upd(a[i].L,1,bit2);
upd(a[i].R,v[a[i].R],bitVal1); upd(a[i].L,v[a[i].L],bitVal2);
Push_in(a[i].L,a[i].R);
ll med = pq1.top(); ll val = v[med];
ll u = Get(N - 1,bitVal2) - Get(med,bitVal2);
ll v = Get(N - 1,bit2) - Get(med,bit2);
kq[i][cond] = val*Get(med,bit1) - Get(med,bitVal1)
+ u - val*v; kq[i][cond] *= 2; //cout<<med<<" ";
//cout<<kq[i][cond]<<"\n";
}
//exit(0);
}
void Sub2(){
ll inc = inf; sort(a + 1,a + n + 1,lf);
//for (ll i = 1;i <= n;i++) cout<<a[i].x<<" "<<a[i].L<<" "<<a[i].y<<" "<<a[i].R<<"\n"; exit(0);
for (ll i = 1;i <= n;i++) a[i].L = Find(a[i].L),a[i].R = Find(a[i].R);
v.push_back(-1); sort(v.begin(),v.end());
oper(0); reverse(a + 1,a + n + 1); oper(1);
//for (ll i = 1;i <= n;i++) cout<<kq[i][1]<<" "; exit(0);
for (ll i = 1;i < n;i++) inc = min(inc,kq[i][0] + kq[n - i][1]);
if (n == 1) inc = 0;
cout<<ans + inc;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "test"
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 Sub1()':
bridge.cpp:58: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]
58 | for (ll j = 0;j < v.size();j++){
| ~~^~~~~~~~~~
bridge.cpp:57:12: warning: unused variable 'inc' [-Wunused-variable]
57 | ll opt,inc = inf;
| ^~~
bridge.cpp: In function 'int main()':
bridge.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp: In function 'void Sub1()':
bridge.cpp:65:34: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | cout<<ans + min(cal_opt(v[opt]),cal_opt(v[opt - 1]));
| ^
# | 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... |