Submission #391797

#TimeUsernameProblemLanguageResultExecution timeMemory
391797osaaateiasavtnlPalembang Bridges (APIO15_bridge)C++14
100 / 100
252 ms15524 KiB
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
    for (T& x : data)
        input >> x;
    return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down 
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT> void re(V<T>& x) { 
    trav(a, x)
        cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; 
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; 
}
ll gcd(ll a, ll b) {
    while (b) {
        tie(a, b) = mp(b, a % b);
    }
    return a;
}

const int LG = 18, N = 1 << LG;
int f[1 << LG];

void clear() {
	memset(f, 0, sizeof f);
}
void add(int i) {
	for (; i < N; i |= i + 1) {
		f[i]++;
	}
}
int get(int k) {
	int i = 0;
	for (int bit = LG - 1; bit >= 0; --bit) {
		int b = f[i + (1 << bit) - 1];
		if (b < k) {
			k -= b;
			i += 1 << bit;
		}
	}
	return i;
}

struct Fen {

int f[N];
void clear() {
	memset(f, 0, sizeof f);
}
void add(int i, int x) {
	for (; i < N; i |= i + 1) {
		f[i] += x;
	}
}
int get(int i) {
	int ans = 0;
	for (; i >= 0; i &= i + 1, --i) {
		ans += f[i];
	}
	return ans;
}
int get(int l, int r) {
	return get(r) - get(l - 1);
}

};

vi co;

struct Dist {

Fen cnt, sum;
void clear() {
	cnt.clear();
	sum.clear();
}
void add(int i) {
	cnt.add(i, 1);
	sum.add(i, co[i]);
}
int left(int i) {
	return co[i] * cnt.get(i) - sum.get(i);
}
int right(int i) {
	return sum.get(i, N - 1) - co[i] * cnt.get(i, N - 1);
}

} L, R;	

int pref[N], suf[N];

signed main() {
    #ifdef LOCAL
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    int k, n;
    cin >> k >> n;
    int AD = 0;
    V <ii> a;

    FOR (i, n) {
    	char t1, t2;
    	int l, r;
    	cin >> t1 >> l >> t2 >> r;
    	if (r < l) {
    		swap(l, r);
    	}
    	AD += r - l;
    	if (t1 != t2) {
    		a.app(mp(l, r));
    		co.app(l);
    		co.app(r);
    		AD++;
    	}
    }

    auto comp = [&] (ii a, ii b) {
    	return a.x + a.y < b.x + b.y;
    };
    sort(all(a), comp);
    sort(all(co));

   	if (k == 2) {
	    auto Pos = [&] (int x) {
	    	return lower_bound(all(co), x) - co.begin();
	    };

	    int sz = a.size();

	    FOR (l, sz) {
	    	add(Pos(a[l].x));
	    	add(Pos(a[l].y));
	    	L.add(Pos(a[l].x));
	    	R.add(Pos(a[l].y));
	    	int x = get(l + 1);
	    	pref[l + 1] = R.left(x) + L.right(x);
	    }
	    clear();
	    L.clear();
	    R.clear();
	    for (int r = sz - 1; r >= 0; --r) {
	    	add(Pos(a[r].x));
	    	add(Pos(a[r].y));
	    	L.add(Pos(a[r].x));
	    	R.add(Pos(a[r].y));
	    	int x = get(sz - r);
	    	suf[r] = R.left(x) + L.right(x);
	    }

	    int ans = 1e18;
	    FOR (i, sz + 1) {
	    	ckmin(ans, pref[i] + suf[i]);
	    }

	    cout << 2 * ans + AD << endl;   		
   	}
   	else {

   		vi c;
   		trav (e, a) {
   			c.app(e.x);
   			c.app(e.y);
   		}
   		sort(all(c));

	    int sz = a.size();
	 
	    int ans = 0;
	    if (sz) {
	    	int X = c[sz - 1];
		    trav (e, a) {
		    	if (e.x > X) {
		    		ans += e.x - X;
		    	}
		    	else if (e.y < X) {
		    		ans += X - e.y;
		    	}
		    }    	
	    }
	 
		cout << ans * 2 + AD << endl;
   	}

}
#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...