# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
668265 | slo | Walk (CEOI06_walk) | C++14 | 58 ms | 3028 KiB |
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 <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
#define forw(i,l,r) for(int i=l; i<r; i++)
#define fore(i,l,r) for(int i=l; i<=r; i++)
#define forb(i,r,l) for(int i=r; i>=l; i--)
#define rep(i,n) forw(i,0,n)
#define Pi acos(-1.0)
#define mp make_pair
#define ins insert
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define sz(a) (a.size())
#define all(a) a.begin(), a.end()
#define numZeroBitStart(x) (__builtin_clz(x))
#define numZeroBitEnd(x) (__builtin_ctz(x))
#define numOneBit(x) (__builtin_popcount(x))
#define parityOfNumOneBit(x) (__builtin_parity(x))
typedef long long ll;
typedef unsigned long long int ull;
typedef pair<int,int> pii;
typedef pair<int,ll> pill;
typedef pair<ll,int> plli;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps=1e-9;
if(x+eps<y){
x=y; return true;
}
return false;
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps=1e-9;
if(x>y+eps){
x=y; return true;
}
return false;
}
/*-----------------MAIN PROGRAM-----------------*/
const int N=1e5+7;
const int INF=1e9+7;
struct Rec{
int x1=0, y1=0, x2=0, y2=0;
};
int n, xf, yf;
Rec rec[N];
void read(){
cin>>xf>>yf>>n;
fore(i,1,n){
cin>>rec[i].x1>>rec[i].y1>>rec[i].x2>>rec[i].y2;
if(rec[i].y1>rec[i].y2) swap(rec[i].y1,rec[i].y2);
}
}
void solve(){
vpii evt;
fore(i,1,n) evt.pb(mp(rec[i].x1,i));
sort(all(evt));
int xPre=0;
set<pill> active;
active.ins(mp(0,0));
rep(i,sz(evt)){
const int xCur=evt[i].fi-1;
if(xCur>=xf) break;
const int& id=evt[i].se;
auto itl=active.lower_bound(mp(rec[id].y1,-INF));
auto itr=active.lower_bound(mp(rec[id].y2+1,-INF));
stack<pill> rem;
for(auto it=itl; it!=itr; it++)
rem.push(*it);
if(rem.empty()) continue;
pill lhs, rhs=rem.top();
while(!rem.empty()){
active.erase(active.find(rem.top()));
if(sz(rem)==1) lhs=rem.top();
rem.pop();
}
lhs.se+=(lhs.fi-(rec[id].y1-1))+xCur-xPre;
lhs.fi=rec[id].y1-1;
rhs.se+=(rec[id].y2+1-rhs.fi)+xCur-xPre;
rhs.fi=rec[id].y2+1;
active.ins(lhs), active.ins(rhs);
xPre=xCur;
}
ll ans=xf-xPre;
auto it=active.lower_bound(mp(yf,-INF));
int add=(it->se)+(it->fi)-yf;
if(it!=active.begin()){
it--;
minimize(add,(it->se)+yf-(it->fi));
}
ans+=add;
cout<<ans;
}
int main(){
fastIO;
read();
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |