Submission #603447

#TimeUsernameProblemLanguageResultExecution timeMemory
603447wiwihoShortcut (IOI16_shortcut)C++14
97 / 100
2033 ms97404 KiB
#include "shortcut.h"
 
#include <bits/stdc++.h>
#include <bits/extc++.h>
 
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
 
const ll MOD = 1000000007;
const ll MAX = 1LL << 60;
 
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}
 
ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}
 
ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}
 
int n;
vector<ll> p, d, s;
ll c;
vector<ll> discr;
vector<ll> tmp;
vector<ll> pos;

int lowbit(int x){
    return x & -x;
}
 
struct event{
    ll x, y1, y2, ty;
    bool operator<(event b){
        return x < b.x;
    }
};
 
bool check(ll t){
    //cerr << "check " << t << "\n";
 
    ll y1 = -MAX, y2 = MAX;
    ll x1 = -MAX, x2 = MAX;

    vector<pll> dq;
 
    for(int a = n - 1; a >= 0; a--){
        ll X = t - d[a] - c;
        //cerr << "test " << a << " : " << p[a] - d[a] << " " << p[a] + d[a] << " " << t + p[a] - d[a] << "\n";
        //printv(dq, cerr);
        if(!dq.empty() && dq.front().S > t + p[a] - d[a]){
            auto it = lower_bound(iter(dq), mp(0, t + p[a] - d[a]), [&](pll x, pll y){
                return x.S > y.S;
            } );
            it--;
            //cerr << "tmp " << dq.front().S << " " << it->F << "\n";
            ll mny = -X + dq.front().S;
            ll mxy = X + it->F;
            if(mny > mxy) return false;
            mny *= 2;
            mxy *= 2;
            ll midx = p[a] * 2;
            ll midy = (mny + mxy) / 2;
            ll r = mxy - midy;
            ll mnx = midx - r;
            ll mxx = midx + r;
            //cerr << "rect " << midx << " " << midy << " " << r << "\n";
            
            x1 = max(x1, mnx - midy);
            x2 = min(x2, mxx - midy);
            y1 = max(y1, mnx + midy);
            y2 = min(y2, mxx + midy);
        }
        while(!dq.empty() && dq.back().S <= p[a] + d[a]) dq.pob;
        if(dq.empty() || p[a] - d[a] < dq.back().F) dq.eb(mp(p[a] - d[a], p[a] + d[a]));
    }
    //cerr << "ok\n";
 
    //cerr << "range " << x1 << " " << x2 << " " << y1 << " " << y2 << "\n";
    if(x1 > x2 || y1 > y2) return false;

    int b = n - 1;
    for(int a = 0; a < n; a++){
        ll x = p[a] * 2;
        ll mny = max(y1 - x, x - x2);
        ll mxy = min(y2 - x, x - x1);
        while(b < n - 1 && p[b] * 2 < mny) b++;
        while(b > 0 && p[b - 1] * 2 >= mny) b--;
        if(p[b] * 2 >= mny && p[b] * 2 <= mxy) return true;
    }
    return false;
}
 
ll find_shortcut(int _n, vector<int> len, vector<int> _d, int _c){
    n = _n;
    c = _c;
    
    p.resize(n);
    s.resize(n);
    d.resize(n);
    for(int i = 0; i < n; i++) d[i] = _d[i];
    for(int i = 0; i < n - 1; i++){
        p[i + 1] = p[i] + len[i];
    }
    for(int i = 0; i < n; i++){
        s[i] = p[i] + d[i];
    }
 
    discr = s;
    tmp.resize(n);
    pos.resize(n);
    iota(iter(tmp), 0);
    sort(iter(tmp), [&](int x, int y){ return s[x] < s[y]; });
    for(int i = 0; i < n; i++){
        pos[tmp[i]] = i;
    }
    lsort(discr);
 
    ll l = 1, r = (ll)(1e9 + 5) * n + (ll)2e9 + 5;
    while(l < r){
        ll m = (l + r) / 2;
        if(check(m)) r = m;
        else l = m + 1;
    }
 
    return l;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...