This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 BIT{
vector<pll> bit;
void init(){
bit.resize(n + 1, mp(MAX, -MAX)); // mn mx
}
void reset(){
fill(iter(bit), mp(MAX, -MAX));
}
void modify(int x, pll v){
x = n - x;
for(; x < bit.size(); x += lowbit(x)){
bit[x].F = min(bit[x].F, v.F);
bit[x].S = max(bit[x].S, v.S);
}
}
pll query(int x){
x = n - x;
pll ans = mp(MAX, -MAX);
for(; x > 0; x -= lowbit(x)){
ans.F = min(ans.F, bit[x].F);
ans.S = max(ans.S, bit[x].S);
}
return ans;
}
};
BIT bit;
struct event{
ll x, y1, y2, ty;
bool operator<(event b){
return x < b.x;
}
};
bool check(ll t){
//cerr << "check " << t << "\n";
//cerr << "tmp ";
//printv(tmp, cerr);
bit.reset();
vector<event> ev;
for(int a = n - 1; a >= 0; a--){
ll X = t - d[a] - c;
//cerr << "OAO " << a << "\n";;
int id = upper_bound(iter(discr), t - d[a] + p[a]) - discr.begin();
//cerr << "test " << a << " " << id << " " << t - d[a] + p[a] << "\n";
if(id < n){
pll owo = bit.query(id);
ll mny = -X + owo.S;
ll mxy = X + owo.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";
ll x1 = mnx - midy;
ll x2 = mxx - midy;
ll y1 = mnx + midy;
ll y2 = mxx + midy;
ev.eb(event({x1, y1, y2, 0}));
ev.eb(event({x2 + 1, y1, y2, 1}));
}
bit.modify(pos[a], mp(-d[a] + p[a], d[a] + p[a]));
}
//cerr << "ok\n";
ll y1 = -MAX, y2 = MAX;
ll x1 = -MAX, x2 = MAX;
lsort(ev);
int sz = ev.size();
int cnt = 0;
//cerr << "ok2 " << sz << "\n";
for(int i = 0; i < sz; ){
ll x = ev[i].x;
int tc = 0;
bool ed = false;
for(; i < sz && ev[i].x == x; i++){
//cerr << "do " << i << "\n";
if(ev[i].ty == 1){
x2 = x - 1;
ed = true;
break;
}
y1 = max(y1, ev[i].y1);
y2 = min(y2, ev[i].y2);
x1 = x;
tc++;
}
if(y1 > y2 || ed) break;
cnt += tc;
}
//cerr << "range " << x1 << " " << x2 << " " << y1 << " " << y2 << "\n";
if(y1 > y2 || cnt != sz / 2) return false;
for(int i = 0; i < n; i++){
ll x = p[i] * 2;
ll mny = max(y1 - x, x - x2);
ll mxy = min(y2 - x, x - x1);
//cerr << "owo " << i << " " << x << " " << mny << " " << mxy << "\n";
auto it = lower_bound(iter(p), iceil(mny, 2));
if(it != p.end() && *it * 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];
}
bit.init();
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;
}
//check(l);
return l;
}
Compilation message (stderr)
shortcut.cpp: In member function 'void BIT::modify(int, pll)':
shortcut.cpp:86:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(; x < bit.size(); x += lowbit(x)){
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |