//```// YF YUSUF
#define YF ios_base::sync_with_stdio(0);cout.setf(ios::fixed);
#define YUSUF cout.precision(0);cout.tie(0);cin.tie(0);
#include <bits/stdc++.h>
// #include <iostream>
// #include <vector>
// #include <set>
// #include <map>
// #include <algorithm>
// #include <unordered_map>
// #include <cmath>
// #include <numeric>
// #include <queue>
// #include <stack>
// #include <cassert>
// #include <climits>
// #include <string>
// #include <cstdlib>
using namespace std;
#ifdef YF_CHECK
bool Output=1;
// #include "g/debug.h"
// #include "g/bigint.h"
// #include "g/text.h"
#else
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("O3")
bool Output=0;
#define deb(x...) 42
#endif
#ifdef DEV
bool LOUT=1;
#else
bool LOUT=0;
#endif
using ll = long long;
using ld = long double;
using sll = set <ll>;
using vll = vector <ll>;
using mll = map <ll,ll>;
using pll = pair <ll,ll>;
using vvl = vector <vll>;
using vpll = vector <pll>;
template<class T>T MIN(T&a,T b){a=min(a,b);return a;}
template<class T>T MAX(T&a,T b){a=max(a,b);return a;}
#define revers(a) reverse(all(a))
#define all(a) a.begin(),a.end()
#define sortt(a) sort(all(a))
#define sgr v+v+1,(tl+tr)/2+1,tr
#define sgl v+v,tl,(tl+tr)/2
#define pb push_back
#define ins insert
#define S second
#define F first
ll BP(ll a,ll b,ll mod=1e9+7){
if(b==0)return 1;
ll q=BP(a,b/2,mod);
return ((q*q)%mod*(b%2?a:1ll))%mod;
}
ll f(ll x){return x*(x+1)/2;}
ll dup(ll a,ll b){return (a+b-1)/b;}
ll lcm(ll a,ll b){return a/__gcd(a,b)*b;}
ll invf(ll x){return (-1+sqrt(1+8*x))/2;}
ll log(ll x){return 63 - __builtin_clzll(x);}
const ll mod=998244353;
const ll INF=1e18;
const ll inf=1e9+7;
const ll N =1e6+7;
ll n,k,nn;
set<ll>st;
mll mp;
ll cnt = 0;
vll ARR = {0};
struct MED{
vll t;
ll nn=(1e5+7);
void init(ll N){
nn = N;
t.assign(4 * N + 7, 0);
return;
}
ll merge(ll a,ll b){
return a+b;
}
void up(ll pos,ll x = 1){
up(mp[pos],x,1,1,nn);
}
void up(ll pos,ll x,ll v,ll tl,ll tr){
if(tl==tr){
t[v]+=x;
}
else{
ll tm=(tl+tr)/2;
if(pos<=tm)up(pos,x,sgl);
else up(pos,x,sgr);
t[v]=merge(t[v+v],t[v+v+1]);
}
}
ll get(ll l,ll r,ll v,ll tl,ll tr){
if(l>tr || tl > r || l>r || tl>tr)return 0;
if(l<=tl && tr<=r)return t[v];
ll tm = (tl+tr)/2;
return merge(get(l,r,sgl),get(l,r,sgr));
}
ll get(ll l,ll r){
ll L = mp[l], R = mp[r];
ll gt = get(L, R, 1, 1, nn);
return gt;
}
ll MD(ll k,ll v,ll tl,ll tr){
if(tl==tr)return tl;
if(t[v+v] < k)return MD(k - t[v+v], sgr);
else return MD(k, sgl);
}
ll med(){
ll k = (t[1]+1)/2;
ll a = ARR[MD(k,1,1,nn)];
return a;
}
} led, red;
struct SM{
vll t;
ll nn = (1e5 + 7);
void init(ll N){
nn = N;
t.assign(4 * N + 7, 0);
return;
}
ll merge(ll a,ll b){
return a+b;
}
void up(ll pos,ll x = 1){
up(mp[pos],x,1,1,nn);
}
void up(ll pos,ll x,ll v,ll tl,ll tr){
if(tl==tr){
t[v]+=x*ARR[tl];
}
else{
ll tm=(tl+tr)/2;
if(pos<=tm)up(pos,x,sgl);
else up(pos,x,sgr);
t[v]=merge(t[v+v],t[v+v+1]);
}
}
ll get(ll l,ll r,ll v,ll tl,ll tr){
if(l>tr || tl > r || l>r || tl>tr)return 0;
if(l<=tl && tr<=r)return t[v];
ll tm = (tl+tr)/2;
return merge(get(l,r,sgl),get(l,r,sgr));
}
ll get(ll l,ll r){
ll L = mp[l], R = mp[r];
ll gt = get(L, R, 1, 1, nn);
return gt;
}
} sml, smr;
void lad(pll a,ll x){
led.up(a.F, x);
led.up(a.S, x);
sml.up(a.F, x);
sml.up(a.S, x);
}
void rad(pll a,ll x){
red.up(a.F, x);
red.up(a.S, x);
smr.up(a.F, x);
smr.up(a.S, x);
}
void YF_MAIN(ll TEST){
cin>>k>>n;
ll sum = 0, add = 10 * n * inf;
vvl vv;
vpll V;
for(int i=1;i<=n;i++){
char a,b;
ll v,u;
cin>>a>>v>>b>>u;
sum+=abs(v - u);
if(a==b)continue;
sum-=abs(v - u);
if(v>u)swap(v, u);
vv.pb({v+u,v,u});
st.ins(v);
st.ins(u);
st.ins(v+1);
st.ins(u+1);
sum++;
}
sortt(vv);
for(auto v : vv){
V.pb({v[1], v[2]});
}
for(auto i : st){
mp[i] = ++cnt;
ARR.pb(i);
}
nn = ARR.size();
sml.init(nn);
smr.init(nn);
red.init(nn);
led.init(nn);
ll m = V.size();
vll pl={0},pr={0};
for(auto [aa,bb] : V){
pl.pb(pl.back() + aa);
pr.pb(pr.back() + bb);
}
if(!m){
cout<<sum;
return;
}
for(int k=0;k<m;k++){
rad(V[k], 1);
}
if(k==1){
ll mr = red.med();
ll d = -1, e = mr, f = ARR.back();
ll smrl = smr.get(d+1, e);
ll smrr = smr.get(e+1, f);
ll _RL = red.get(d+1, e);
ll _RR = red.get(e+1, f);
ll rg = mr * _RL - smrl + smrr - mr * _RR;
cout<<sum + rg;
return;
}
for(int k=0;k<m-1;k++){
lad(V[k], 1);
rad(V[k], -1);
ll ml = led.med();
ll mr = red.med();
ll a = -1, b = ml, c = ARR.back();
ll d = -1, e = mr, f = ARR.back();
ll smll = sml.get(a+1, b);
ll smlr = sml.get(b+1, c);
ll smrl = smr.get(d+1, e);
ll smrr = smr.get(e+1, f);
ll _LL = led.get(a+1, b);
ll _LR = led.get(b+1, c);
ll _RL = red.get(d+1, e);
ll _RR = red.get(e+1, f);
ll lf = ml * _LL - smll + smlr - ml * _LR;
ll rg = mr * _RL - smrl + smrr - mr * _RR;
MIN(add, lf + rg);
}
cout<<sum + add;
}
bool TECT=0;
bool CASE=0;
bool OUTP=1;
const ll LN=1e1 + 7; ll lg[LN];
const ll SN=2e1 + 7; ll SM[SN];
const ll FN=2e1 + 7;
ll FACT[FN],inv[FN],FMOD=inf;
ll PER(ll n,ll k){return FACT[n] *inv[n-k]%FMOD;}
ll CNK(ll n,ll k){return PER(n,k)*inv[k ]%FMOD;}
void BEFORE(){
for(ll i=2;i<SN;i++){
if(SM[i])continue;
for(ll j=i;j<SN;j+=i)
MAX(SM[j],i);
}
for(int i=2;i<LN;i++) lg[i]=lg[i/2]+1;
FACT[0]=inv[0]=1;
for(int i=1;i<FN;i++){
FACT[i]=FACT[i-1]*i%FMOD;
if(i<5e5+7)inv[i]=BP(FACT[i],FMOD-2,FMOD);
}
}
signed main(){
if(OUTP){YF YUSUF}
// freopen((".in"),"r",stdin);freopen((".out"),"w",stdout);
cout<<(LOUT && Output ? "\nYF_OUTPUT:\n\n" : "");
int TEST=1;
if(TECT)
cin>>TEST;
BEFORE();
for(int T=1;T<=TEST;T++){
if(CASE)
cout<<"Case "<<T<<": ";
YF_MAIN(T);
cout<<(T==TEST ? "" : "\n");
}
return 0;
}
//```
# | 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... |