Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- ll cur_x;
- struct Segment{
- //Use struct to get segment and get each coordinate values for use later on
- //Calculate y vals at each point from x1, x1+1, x1+2... x2
- ll x1, y1, x2, y2, idx;
- double coordinate() const{
- if(x1 == x2) return y1;
- //calculation comes from the position of the x coordinate and then comparing
- //where the y coordinate is of each line underneath the line is at the x-point and using
- //that to sort the values in the set
- return y1+1.0*(y2-y1)*(cur_x-x1)/(x2-x1);
- }
- bool operator<(const Segment &a) const{
- return coordinate() < a.coordinate();
- }
- bool operator==(const Segment &a) const{
- return idx==a.idx;
- }
- };
- struct Event{
- ll x, y, idx;
- bool operator<(const Event &a) const{
- if(x==a.x){
- return y<a.y;
- }
- return x<a.x;
- }
- };
- int n;
- //int ans = 1;
- int main(){
- freopen("hillwalk.in", "r", stdin);
- freopen("hillwalk.out", "w", stdout);
- cin >> n;
- vector<Segment> hill(n);
- vector<Event> event;
- for(int i=0; i<n; i++){
- ll x1, y1, x2, y2;
- cin >> x1 >> y1 >> x2 >> y2;
- hill[i] = {x1, y1, x2, y2, i};
- event.push_back({x1, y1, i});
- event.push_back({x2, y2, i});
- }
- sort(event.begin(), event.end());
- set<Segment> s;
- vector<bool> active(n, false);
- int cur = 0;
- auto it = s.begin();
- int ans = 1;
- for(Event e: event){
- cur_x = e.x;
- //if index is not active then that means it is the left point of the segment
- //but if the index is already active then would need to make it not active anymore
- if(!active[e.idx]){
- s.insert(hill[e.idx]);
- active[e.idx] = true;
- }else{
- if(cur == e.idx){
- //since at the end point of the segment already, have to check if the index is the one bessie
- //is currently on and is about to continue walking on if not then the hill doesn't really matter
- //just still need to make the hill inactive which is on line 84-85
- it = s.find(hill[e.idx]);
- if(it!=s.begin()){
- it--;
- ans++;
- //set.erase(it);
- cur = (*it).idx;
- //cout << (*it).idx << '\n';
- }else{
- break;
- }
- }
- s.erase(hill[e.idx]);
- active[e.idx] = false;
- }
- }
- cout << ans << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement