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());
}
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;
}
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(){
oper(0); reverse(a + 1,a + n + 1); oper(1);
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;
}
void Sub1(){
oper(0);
cout<<kq[n][0] + ans;
}
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();
ll inc = inf; sort(a + 1,a + n + 1,lf);
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());
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 'int main()':
bridge.cpp:110:8: warning: unused variable 'inc' [-Wunused-variable]
110 | ll inc = inf; sort(a + 1,a + n + 1,lf);
| ^~~
bridge.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | 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... |