이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize
const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 100001;
const ld PI = 4*atan((ld)1);
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
template<class A, class B> A operator+=(A& l, const B& r) { return l = l+r; }
template<class A, class B> A operator-=(A& l, const B& r) { return l = l-r; }
template<class A, class B> A operator*=(A& l, const B& r) { return l = l*r; }
template<class A, class B> A operator/=(A& l, const B& r) { return l = l/r; }
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
re(first); re(rest...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
using namespace input;
namespace output {
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T, size_t SZ> void pr(const array<T,SZ>& x);
template<class T> void pr(const vector<T>& x);
template<class T> void pr(const set<T>& x);
template<class T1, class T2> void pr(const map<T1,T2>& x);
template<class T> void pr(const T& x) { cout << x; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
pr(first); pr(rest...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void prContain(const T& x) {
pr("{");
bool fst = 1; trav(a,x) pr(!fst?", ":"",a), fst = 0;
pr("}");
}
template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
template<class T> void pr(const vector<T>& x) { prContain(x); }
template<class T> void pr(const set<T>& x) { prContain(x); }
template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
void ps() { pr("\n"); }
template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
pr(first," "); ps(rest...); // print w/ spaces
}
}
using namespace output;
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
}
using namespace io;
struct Line {
int x; pi y;
Line(int _x, pi _y) {
x = _x; y = _y;
}
Line() : Line(0,{1,-1}) {}
friend bool operator<(const Line& l, const Line& r) {
if (l.y != r.y) return l.y < r.y;
return l.x < r.x;
}
friend bool operator>(const Line& l, const Line& r) {
if (l.y != r.y) return l.y > r.y;
return l.x > r.x;
}
friend bool operator==(const Line& l, const Line& r) {
return l.x == r.x && l.y == r.y;
}
friend bool operator>=(const Line& l, const Line& r) {
return l > r || l == r;
}
friend void pr(const Line& l) {
pr(mp(l.x,l.y));
}
};
const int TMP = 10000000;
Line val[TMP];
int best[TMP];
int c[TMP][2], pri[TMP];
int tot = 0;
namespace treap {
int pt(Line _val) {
pri[tot] = (rand()+(rand()<<15));
val[tot] = _val; best[tot] = tot;
c[tot][0] = c[tot][1] = -1;
return tot++;
}
int getBest(int x) {
if (x == -1) return -1;
return best[x];
}
int bet(int l, int r) {
if (min(l,r) == -1) return max(l,r);
if (val[l].y.s > val[r].y.s) return l;
return r;
}
int recalc(int x) {
best[x] = bet(x,bet(getBest(c[x][0]),getBest(c[x][1])));
return x;
}
pi split(int t, const Line& v) { // >= v goes to the right
if (t == -1) return {t,t};
if (val[t] >= v) {
auto p = split(c[t][0], v); c[t][0] = p.s;
return {p.f, recalc(t)};
} else {
auto p = split(c[t][1], v); c[t][1] = p.f;
return {recalc(t), p.s};
}
}
int merge(int l, int r) {
if (min(l,r) == -1) return max(l,r);
int t;
if (pri[l] > pri[r]) c[l][1] = merge(c[l][1],r), t = l;
else c[r][0] = merge(l,c[r][0]), t = r;
return recalc(t);
}
int ins(int x, const Line& v) { // insert v
auto a = split(x,v);
return merge(a.f,merge(pt(v),a.s));
}
int remFst(int x) {
if (c[x][0] != -1) {
c[x][0] = remFst(c[x][0]);
return recalc(x);
}
return c[x][1];
}
int del(int x, const Line& v) { // delete v
auto a = split(x,v);
return merge(a.f,remFst(a.s));
}
Line queryBest(int x, int y) {
auto a = split(x,Line({-MOD,{y+1,-MOD}}));
int L = getBest(a.f);
x = merge(a.f,a.s);
if (L == -1) return Line();
return val[L];
}
}
using namespace treap;
int ret = 0;
template<int SZ> struct Seg { // 0: store vertical lines
int seg[2*SZ];
Seg() {
F0R(i,2*SZ) seg[i] = -1;
}
void add(const Line& L, int p, int ind = 1, int l = 0, int r = 200010) { // L.x, L.y.f, L.y.s;
ret ++;
if (p == 1) seg[ind] = ins(seg[ind],L);
else seg[ind] = del(seg[ind],L);
if (l == r) return;
int m = (l+r)/2;
if (L.x <= m) add(L,p,2*ind,l,m);
else add(L,p,2*ind+1,m+1,r);
}
void query(vector<Line>& v, const Line& L, int ind = 1, int l = 0, int r = 200010) {
if (r < L.y.f || L.y.s < l) return;
if (L.y.f <= l && r <= L.y.s) {
while (1) {
auto a = queryBest(seg[ind],L.x);
if (a.y.s < L.x) break;
v.pb(a); add(a,-1);
}
return;
}
int m = (l+r)/2;
query(v,L,2*ind,l,m); query(v,L,2*ind+1,m+1,r);
}
};
Seg<1<<18> stor[2];
int N;
pi S,E;
vector<pair<pi,pi>> v;
vi x, y;
void getInd(vi& a, int& b) {
b = lb(all(a),b)-a.begin()+1;
}
void compressX() {
sort(all(x)); x.erase(unique(all(x)),x.end());
getInd(x,S.f);
getInd(x,E.f);
trav(t,v) {
getInd(x,t.f.f);
getInd(x,t.f.s);
}
}
void compressY() {
sort(all(y)); y.erase(unique(all(y)),y.end());
getInd(y,S.s);
getInd(y,E.s);
trav(t,v) {
getInd(y,t.s.f);
getInd(y,t.s.s);
}
}
void init() {
setIO(); re(S,E);
x.pb(S.f), x.pb(E.f), y.pb(S.s), y.pb(E.s);
re(N);
F0R(i,N) {
int a,b,c,d; re(a,b,c,d);
v.pb({{a,b},{c,d}});
x.pb(a), x.pb(b), y.pb(c), y.pb(d);
}
compressX();
compressY();
}
queue<pair<pi,Line>> q;
bool finished(pair<pi,Line> a) {
if (a.f.s == 0) return a.s.x == E.f && a.s.y.f <= E.s && a.s.y.s >= E.s;
return a.s.x == E.s && a.s.y.f <= E.f && a.s.y.s >= E.f;
}
map<int,int> m;
map<pi,int> M;
void ins(pi y, int x) {
if (y.f > y.s) return;
while (1) {
auto it = M.lb({y.s+1,-MOD}); if (it == M.begin()) break;
it = prev(it); if (it->f.s < y.f) break;
auto t = *it; M.erase(it);
if (t.f.f < y.f) M[{t.f.f,y.f-1}] = t.s;
if (y.s < t.f.s) M[{y.s+1,t.f.s}] = t.s;
}
M[y] = x;
}
int getMax(int y) {
return prev(M.ub({y,MOD}))->s;
}
vector<Line> hseg, vseg;
vector<Line> tmp;
void genSeg() {
m.clear(), M.clear(); tmp.clear();
sort(all(v));
ins({1,sz(y)},1);
trav(t,v) {
auto it = m.ub(t.s.f);
while (it != m.end() && it->f < t.s.s) {
tmp.pb(Line(it->f,{it->s,t.f.f}));
it = next(it);
m.erase(prev(it));
}
if (!m.count(t.s.f)) m[t.s.f] = getMax(t.s.f);
if (!m.count(t.s.s)) m[t.s.s] = getMax(t.s.s);
// ps("??",t,sz(y),getMax(t.s.f),getMax(t.s.s));
ins({t.s.f+1,t.s.s-1},t.f.s);
}
trav(t,m) tmp.pb(Line(t.f,{t.s,sz(x)}));
}
void swapXY() {
trav(z,v) swap(z.f,z.s);
swap(x,y);
}
int main() {
clock_t st = clock();
init();
v.pb({{S.f,S.f},{S.s,S.s}});
v.pb({{E.f,E.f},{E.s,E.s}});
genSeg();
swap(hseg,tmp);
swapXY();
genSeg();
swap(vseg,tmp);
swapXY();
// ps(ret,double(clock()-st)/CLOCKS_PER_SEC);
trav(t,vseg) {
stor[0].add(t,1);
if (t.x == S.f && t.y.f <= S.s && S.s <= t.y.s) {
q.push({{1,0},t});
// ps("AA",t);
}
}
trav(t,hseg) {
stor[1].add(t,1);
if (t.x == S.s && t.y.f <= S.f && S.f <= t.y.s) {
q.push({{1,1},t});
// ps("BB",t);
}
}
// ps(ret,double(clock()-st)/CLOCKS_PER_SEC);
while (sz(q)) {
auto a = q.front(); q.pop();
if (finished(a)) {
// ps(ret,double(clock()-st)/CLOCKS_PER_SEC);
ps(a.f.f);
exit(0);
}
vector<Line> V; stor[a.f.s^1].query(V,a.s);
trav(t,V) q.push({{a.f.f+1,a.f.s^1},t});
}
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do smth instead of nothing and stay organized
*/
컴파일 시 표준 에러 (stderr) 메시지
golf.cpp: In function 'int main()':
golf.cpp:358:13: warning: unused variable 'st' [-Wunused-variable]
clock_t st = clock();
^~
golf.cpp: In function 'void io::setIn(std::__cxx11::string)':
golf.cpp:117:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
golf.cpp: In function 'void io::setOut(std::__cxx11::string)':
golf.cpp:118:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |