Submission #298325

#TimeUsernameProblemLanguageResultExecution timeMemory
298325shayan_pSky Walking (IOI19_walk)C++17
15 / 100
232 ms17696 KiB
#include<bits/stdc++.h>
#include "walk.h"

#define F first
#define S second
#define PB push_back
#define sz(s) (int(s.size()))
#define bit(n, k) (((n)>>(k))&1)

#pragma GCC optimize("Ofast")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int maxn = 1e5 + 10;
const ll INF = 2e18;

vector<int> tdo[maxn][2];
ll tmp[maxn];

ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int src, int snk){
    int n = sz(x), m = sz(y);
    assert(src == 0 && snk == n-1);
    set< pair<int, ll> > st;
    ll lazy = 0;
    
    st.insert({0, 0});
    tdo[0][1].PB(0);
    for(int i = 0; i < m; i++){
	tdo[l[i]][0].PB(y[i]);
	if(r[i] != n-1)
	    tdo[r[i]][1].PB(y[i]);
    }
    for(int i = 0; i < n; i++){
	for(int j = 0; j < sz(tdo[i][0]); j++){
	    int H = tdo[i][0][j];
	    auto it = st.lower_bound({H, -1});
	    tmp[j] = INF;
	    if(it != st.end() && (it->F) <= h[i])
		tmp[j] = min(tmp[j], (it->S) + abs(H - (it->F)));
	    if(it != st.begin())
		it = prev(it), tmp[j] = min(tmp[j], (it->S) + abs(H - (it->F)));
	}
	for(int H : tdo[i][1]){
	    auto it = st.lower_bound({H, -1});
	    if(it != st.end())
		assert(it->F == H), st.erase(st.lower_bound({H, -1}));
	}
	for(int j = 0; j < sz(tdo[i][0]); j++){
	    if(tmp[j] != INF)
		st.insert({tdo[i][0][j], tmp[j]});
	}
	if(i > 0)
	    lazy+= x[i] - x[i-1];
    }
    auto it = st.begin();
    if(st.empty())
	return -1;
    return lazy + (it->F) + (it->S);
}
#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...