Submission #1307676

#TimeUsernameProblemLanguageResultExecution timeMemory
1307676JuanJLPalembang Bridges (APIO15_bridge)C++20
0 / 100
1 ms400 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define forn(i,a,b) for(int i = a; i< b; i++)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;

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

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

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

	//cout<<resbase<<'\n';

	sort(ALL(rng));

	imset<pair<ll,ll>> left;
	imset<pair<ll,ll>> right;
	set<ll> vals; vals.insert(-1);
	auto it = vals.begin();
	ll aux = 0;
	vector<ll> ltr(SZ(rng),0);
	ll resaux=0;
	forn(i,0,SZ(rng)){
		left.insert({rng[i].fst,aux}); aux++;
		right.insert({rng[i].snd,aux}); aux++;
		vals.insert(rng[i].fst);
		vals.insert(rng[i].snd);

		resaux+=(rng[i].fst-(*it))*2;
		//while(true){
		ll cleft=0;
		auto lait = left.lower_bound({*it+1,-1});
		if(lait!=left.end()){
			cleft=SZ(left)-left.order_of_key(*lait);			
		}

		ll cright=SZ(right);
		auto rait = right.upper_bound({*it,1000000000000000});
		if(rait!=right.end()){
			cright=right.order_of_key(*rait);
		}

		if(cleft>cright){
			ll last=*it; it++;
			ll neww = *it;
			resaux+=(neww-last)*2*cright;
			resaux-=(neww-last)*2*cleft;
		}

		ltr[i]=resaux;
		//cout<<resaux<<" "<<i<<'\n';
	}	

	cout<<ltr[SZ(rng)-1]+resbase<<'\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...