Submission #498526

#TimeUsernameProblemLanguageResultExecution timeMemory
498526codebuster_10Walk (CEOI06_walk)C++17
72 / 100
223 ms31660 KiB
#include <bits/stdc++.h> using namespace std; #define int int64_t //be careful about this #define pr pair #define ar array #define f first #define s second #define vt vector #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define SZ(x) ((int)(x).size()) #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define mem(a,b) memset(a, b, sizeof(a)) template<class A> void rd(vt<A>& v); template<class T> void rd(T& x){ cin >> x;} template<class H, class... T> void rd(H& h, T&... t) { rd(h); rd(t...);} template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a);} template<class T> bool ckmin(T& a, const T b) { return b < a ? a = b, 1 : 0;} template<class T> bool ckmax(T& a, const T b) { return a < b ? a = b, 1 : 0;} template<typename T> void __p(T a) { cout<<a; } template<typename T, typename F> void __p(pair<T, F> a) { cout<<"{"; __p(a.first); cout<<","; __p(a.second); cout<<"}\n"; } template<typename T> void __p(std::vector<T> a) { cout<<"{"; for(auto it=a.begin(); it<a.end(); it++) __p(*it),cout<<",}\n"[it+1==a.end()]; } template<typename T, typename ...Arg> void __p(T a1, Arg ...a) { __p(a1); __p(a...); } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "; __p(arg1); cout<<endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket=0,i=0; for(;; i++) if(names[i]==','&&bracket==0) break; else if(names[i]=='(') bracket++; else if(names[i]==')') bracket--; const char *comma=names+i; cout.write(names,comma-names)<<" : "; __p(arg1); cout<<" | "; __f(comma+1,args...); } #define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__) void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); cout << fixed; if(SZ(s)){ freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } } /*/--------------------------------------------------look below-----------------------------------------------------------------------------------/*/ const int inf = 1e9; struct R{ int X1,Y1,X2,Y2,id; }; signed main(){ setIO(); int FX,FY; rd(FX,FY); int n; rd(n); vt<R> rect(n); for(int i = 0; i < n; ++i) rect[i].id=i,rd(rect[i].X1,rect[i].Y1,rect[i].X2,rect[i].Y2); rect.pb({FX,FY,FX,FY,n}); vt<int> nxtu(n+1,-1),nxtl(n+1,-1); vt<ar<int,3>> events; for(int i = 0; i <= n; ++i){ events.pb({rect[i].Y1-1,+1,rect[i].id}); events.pb({rect[i].Y2+1,-1,rect[i].id}); events.pb({rect[i].Y1,+2,rect[i].id});//activate events.pb({rect[i].Y2,-2,rect[i].id});//deactivate. } sort(all(events),[&](auto l,auto r){ if(l[0] != r[0]) return l[0] < r[0]; return l[1] > r[1]; }); auto cmp = [&](int i,int j){ return rect[i].X2 < rect[j].X2; }; set<int,decltype(cmp)> s(cmp); for(auto [y,t,id] : events){ if(t == -1 || t == +1){ if(s.empty()) continue; auto itr = s.lb(id); if(itr != s.begin()) --itr; else continue; if(t == -1) nxtu[id] = *itr; else nxtl[id] = *itr; } if(t == +2){ s.insert(id); } if(t == -2){ s.erase(s.find(id)); } } vt<int> id(n+1); iota(all(id),0); sort(all(id),[&](auto l,auto r){ return rect[l].X2 < rect[r].X2; }); int ans = inf; vt<int> disu(n+1,inf),disl(n+1,inf); map<pr<int,int>,pr<int,int>> par; for(auto i : id){ if(rect[i].id == n){ if(nxtu[rect[i].id] < 0) ckmin(ans,abs(rect[i].X2) + abs(rect[i].Y2)),par[{FX,FY}] = {0,0}; else{ int nxt = nxtu[rect[i].id]; if(ckmin(ans,disu[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y2-(rect[nxt].Y2+1)))) par[{FX,FY}] = {rect[nxt].X2,rect[nxt].Y2+1}; if(ckmin(ans,disl[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y2-(rect[nxt].Y1-1)))) par[{FX,FY}] = {rect[nxt].X2,rect[nxt].Y1-1}; } if(nxtl[rect[i].id] < 0) ckmin(ans,abs(rect[i].X2) + abs(rect[i].Y2)); else{ int nxt = nxtl[rect[i].id]; if(ckmin(ans,disu[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y2-(rect[nxt].Y2+1)))) par[{FX,FY}] = {rect[nxt].X2,rect[nxt].Y2+1}; if(ckmin(ans,disl[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y2-(rect[nxt].Y1-1)))) par[{FX,FY}] = {rect[nxt].X2,rect[nxt].Y1-1}; } vt<pr<int,int>> out; while(FX != 0 || FY != 0){ auto p = par[{FX,FY}]; out.eb(0,FY-p.s); out.eb(FX-p.f,0); FX=p.f,FY=p.s; } cout << ans << "\n"; reverse(all(out)); cout << SZ(out) << "\n"; for(auto [x,y] : out) cout << x << " " << y << "\n"; return 0; } if(nxtu[rect[i].id] < 0) disu[rect[i].id] = abs(rect[i].X2) + abs(rect[i].Y2+1), par[{rect[i].X2,rect[i].Y2+1}] = {0,0}; else{ int nxt = nxtu[rect[i].id]; if(ckmin(disu[rect[i].id],disu[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y2-rect[nxt].Y2))) par[{rect[i].X2,rect[i].Y2+1}] = {rect[nxt].X2,rect[nxt].Y2+1}; if(ckmin(disu[rect[i].id],disl[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y2+1-(rect[nxt].Y1-1)))) par[{rect[i].X2,rect[i].Y2+1}] = {rect[nxt].X2,rect[nxt].Y1-1}; } if(nxtl[rect[i].id] < 0) disl[rect[i].id] = abs(rect[i].X2) + abs(rect[i].Y1-1), par[{rect[i].X2,rect[i].Y1-1}] = {0,0}; else{ int nxt = nxtl[rect[i].id]; if(ckmin(disl[rect[i].id],disu[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y1-1-(rect[nxt].Y2+1)))) par[{rect[i].X2,rect[i].Y1-1}] = {rect[nxt].X2,rect[nxt].Y2+1}; if(ckmin(disl[rect[i].id],disl[nxt] + abs(rect[i].X2-rect[nxt].X2) + abs(rect[i].Y1-rect[nxt].Y1))) par[{rect[i].X2,rect[i].Y1-1}] = {rect[nxt].X2,rect[nxt].Y1-1}; } } return 0; } // tips to avoid bugs. /* * be careful of whats happening you dont want a continue statement to miss imp line of code. * be careful when to update what since it might be needed in next segment of code. * dont get stuck on one idea. * dont use too much space bar so that code is written quickly as possible. */

Compilation message (stderr)

walk.cpp: In function 'void setIO(std::string)':
walk.cpp:81:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |   freopen((s+".in").c_str(),"r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
walk.cpp:82:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |   freopen((s+".out").c_str(),"w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...