Submission #1308335

#TimeUsernameProblemLanguageResultExecution timeMemory
1308335JuanJLPalembang Bridges (APIO15_bridge)C++20
100 / 100
522 ms23980 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i< b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;

template<typename T>
using iset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

vector<pair<ll,ll>> rng;
set<ll> vals;

ll pA = -1;
ll resauxA = 0;
iset<pair<ll,ll>> leftA;
iset<pair<ll,ll>> rightA;

//acomodar A 
// a partir de la posicion del puente lo acomoda segun sea necesario (izq o der)

void acomodarA(){
	while(true){
		ll cleft=0;
		auto lait = leftA.lower_bound({pA+1,-1});
		if(lait!=leftA.end()){
			cleft=SZ(leftA)-leftA.order_of_key(*lait);			
		}

		ll cright=SZ(rightA);
		auto rait = rightA.upper_bound({pA,1000000000000000});
		if(rait!=rightA.end()){
			cright=rightA.order_of_key(*rait);
		}
		
		if(cleft>cright){
			ll last=pA;
			auto it = vals.lower_bound(pA); it++;
			ll neww = *it;
			resauxA+=(neww-last)*2*cright;
			resauxA-=(neww-last)*2*cleft;
			pA=neww;
		}else{
			break;
		}
	}
}

ll pB = -1;
ll resauxB = 0;
iset<pair<ll,ll>> leftB;
iset<pair<ll,ll>> rightB;

void acomodarB(){
	while(true){
		ll cleft=0;
		auto lait = leftB.lower_bound({pB+1,-1});
		if(lait!=leftB.end()){
			cleft=SZ(leftB)-leftB.order_of_key(*lait);			
		}

		ll cright=SZ(rightB);
		auto rait = rightB.upper_bound({pB,1000000000000000});
		if(rait!=rightB.end()){
			cright=rightB.order_of_key(*rait);
		}
		
		if(cleft>cright){
			ll last=pB;
			auto it = vals.lower_bound(pB); it++;
			ll neww = *it;
			resauxB+=(neww-last)*2*cright;
			resauxB-=(neww-last)*2*cleft;
			pB=neww;
		}else{
			break;
		}
	}
}

bool cmp(const pair<ll,ll> &a, const pair<ll,ll> &b){
	if(float(a.fst+a.snd)/float(2)<float(b.fst+b.snd)/float(2)) return true;
	return false;
}

int main(){
	ll k,n; cin>>k>>n;

	ll resbase = 0;
	forn(i,0,n){
		char a,b; 
		ll l,r;
		cin>>a>>l>>b>>r;
		if(l>r) swap(l,r);
		if(a!=b) rng.pb({l,r}),resbase++;
		resbase+=r-l;
	}

	sort(ALL(rng),cmp); vals.insert(-1);
	forn(i,0,SZ(rng)) vals.insert(rng[i].fst),vals.insert(rng[i].snd);
	ll aux = 0;
	forn(i,0,SZ(rng)) leftA.insert({rng[i].fst,aux}), aux++;
	forn(i,0,SZ(rng)) rightA.insert({rng[i].snd,aux}), aux++;

	forn(i,0,SZ(rng)){
		resauxA+=(rng[i].fst+1)*2;
	}
	
	acomodarA();
	//cout<<resauxA<<" "<<resauxB<<'\n';
	ll mini = resbase+resauxA+resauxB;
	if(k==1){
		cout<<mini<<'\n';
		return 0;
	}

	forn(i,0,SZ(rng)){
		leftA.erase(leftA.lower_bound({rng[i].fst,0}));
		rightA.erase(rightA.lower_bound({rng[i].snd,0}));
		if(pA<rng[i].fst || pA>rng[i].snd)	resauxA-=min(abs(pA-rng[i].fst),abs(pA-rng[i].snd))*2;

		if(pB<rng[i].fst || pB>rng[i].snd) resauxB+=min(abs(pB-rng[i].fst),abs(pB-rng[i].snd))*2;
		leftB.insert({rng[i].fst,aux}); aux++;
		rightB.insert({rng[i].snd,aux}); aux++;
		//cout<<resauxA<<'\n';
		//cout<<"pres "<<pA<<" "<<pB<<'\n';
		acomodarA();
		acomodarB();
		/*cout<<"pos "<<pA<<" "<<pB<<'\n';
		cout<<i<<" "<<rng[i].fst<<" "<<rng[i].snd<<'\n';
		cout<<resbase<<" "<<resauxA<<" "<<resauxB<<'\n';*/
		mini=min(mini,resbase+resauxA+resauxB);
	}
	

	cout<<mini<<'\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...