Submission #941342

#TimeUsernameProblemLanguageResultExecution timeMemory
941342panWiring (IOI17_wiring)C++17
100 / 100
236 ms34536 KiB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, srb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> piii;
typedef pair<pi, pi> pii;

ll const INF = 1e15;
struct node{

    int s, e, m; 
    ll val;
    ll lazy; 
    node *l, *r; 
	node (int S, int E)
	{ 
       s = S, e = E, m = (s+e)/2;
       val = 0; 
       lazy = 0; 
       l=r=nullptr;
 
    }
    void create()
    {
		if(s != e && l==nullptr)
		   { 
			   l = new node(s, m); 
			   r = new node(m+1, e); 
		   }
    }
 
	void propogate()
	{
       if (lazy==0) return; 
       create();
       val+=lazy; 
       if (s != e){ 
           l->lazy+=lazy;
           r->lazy+=lazy;
       }
       lazy=0; 
    }
	void update(int S, int E, ll V)
	{ 
	   propogate();
       if(s==S && e==E) lazy += V; 
       else{ 
           create();
           if(E <= m) l->update(S, E, V); 
           else if (m < S) r->update(S, E, V); 
           else l->update(S, m, V),r->update(m+1, E, V);
           l->propogate(),r->propogate();
		   val = min(l->val, r->val);
          
       }
    }
	ll query(int S, int E)
	{

       propogate(); 
       if(s == S && e == E) return val;
       create();
       if(E <= m) return l->query(S, E); 
       else if(S >= m+1) return r->query(S, E); 
       else return min(l->query(S, m), r->query(m+1, E)); 
   }

} *root;
 



long long min_total_length(std::vector<int> r, std::vector<int> b)
{
	ll n= r.size();
	ll m = b.size();
	root = new node(0, n+m);
	ll dp[n+m+5];
	// merge red and blue into consecutive range
	vector<pi> c;
	for (ll i=0; i<n; ++i) c.pb(mp(r[i], 0));
	for (ll i=0; i<m; ++i) c.pb(mp(b[i], 1));
	sort(c.begin(), c.end());
	
	// deal with (painful) base case 
	ll i = 0;
	while (c[i].s==c[0].s) i++;
	ll ps = 0;
	for (ll j=i-1; j>=1; --j)
	{
		ps+=c[i].f - c[j].f;
		root->update(j, j, INF);
		dp[j] = INF;
	}
	ps+= c[i].f - c[0].f;
	dp[0] = 0;
	root->update(0, 0, ps);
	dp[i] = ps; 
	ll l1 = i, l2 = 0, cur = 1;
	// transition
	while (++i<n+m)
	{
		if (c[i].s==c[l1].s) // segment continue
		{
			cur++;
			root->update(l2, l1-1, c[i].f);
			if (l2<=l1-cur) root->update(l2, l1-cur, -c[l1].f);
			root->update(max(l2, l1-cur+1), l1-1, -c[l1-1].f);
			dp[i] = root->query(l2, l1-1);
			 
		}
		else // new segment
		{
			l2 = l1;
			l1= i;
			cur = 1;
			ll ps = 0;
			for (ll j=l1-1; j>=l2; --j)
			{
				ps += c[l1].f - c[j].f;
				root->update(j, j, min(dp[j], dp[j-1])+ps);
			}
			dp[i] = root->query(l2, l1-1);
		}
	}
	return dp[n+m-1];

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