Submission #1196584

#TimeUsernameProblemLanguageResultExecution timeMemory
1196584Zbyszek99Roads (CEOI20_roads)C++20
30 / 100
152 ms11288 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

struct seg
{
    ll x1,y1,x2,y2;
    int ind;
    bool operator<(const seg& other) const
    {
        if(x1 > other.x1)
        {
            if((y1) * (other.x1 - other.x2) == (x1 * (other.y1 - other.y2) - other.y1 * other.x2 + other.y2 * other.x1)) return y1 < other.y1;
            return (y1) * (other.x1 - other.x2) < (x1 * (other.y1 - other.y2) - other.y1 * other.x2 + other.y2 * other.x1);
        }
        else
        {
            if((other.x1 * (y1 - y2) - y1 * x2 + y2 * x1) == (other.y1) * (x1 - x2)) return y1 < other.y1;
            return (other.x1 * (y1 - y2) - y1 * x2 + y2 * x1) < (other.y1) * (x1 - x2);
        }
    }
};

seg segs[100001];
pii most_rup[100001];
pii most_rdown[100001];

int main()
{
   // ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int n;
    cin >> n;
    vector<pair<pii,int>> points;
    rep(i,n)
    {
        int x1,y1,x2,y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if(x1 > x2 || (x1 == x2 && y1 > y2))
        {
            swap(x1,x2);
            swap(y1,y2);
        }
        segs[i] = {x1,y1,x2,y2,i};
        points.pb({{x1,y1},i});
        points.pb({{x2,y2},i});
    }
    set<seg> segs_set;
    sort(all(points));
    pair<pii,int> prev = {{-1,-1},-1};
    forall(it,points)
    {
        int x = it.ff.ff;
        int y = it.ff.ss;
        int ind = it.ss;
        if(x == segs[ind].x2 && y == segs[ind].y2)
        {
            auto it = segs_set.find(segs[ind]);
            auto nxt = it;
            nxt++;
            if(nxt != segs_set.end())
            {
                most_rdown[(*nxt).ind] = {x,y};
            }
            if(it != segs_set.begin())
            {
                auto pre = it;
                pre--;
                most_rup[(*pre).ind] = {x,y};
            }
            segs_set.erase(it);
        }
        else 
        {
            if(siz(segs_set) == 0)
            {
                if(prev.ss == -1)
                {
                    segs_set.insert(segs[ind]);
                    most_rdown[ind] = {x,y};
                    most_rup[ind] = {x,y};
                }
                else
                {
                    cout << x << " " << y << " " << prev.ff.ff << " " << prev.ff.ss << "\n";
                    segs_set.insert(segs[ind]);
                    most_rdown[ind] = {x,y};
                    most_rup[ind] = {x,y};
                }
                continue;
            }
            else
            {
                auto it = segs_set.lower_bound(segs[ind]);
                if(it != segs_set.end())
                {
                    cout << x << " " << y << " " << most_rdown[(*it).ind].ff << " " << most_rdown[(*it).ind].ss << "\n";
                }
                else
                {
                    it--;
                    cout << x << " " << y << " " << most_rup[(*it).ind].ff << " " << most_rup[(*it).ind].ss << "\n";
                }
                it = segs_set.lower_bound(segs[ind]);
                if(it != segs_set.end())
                {
                    most_rdown[(*it).ind] = {x,y};
                }
                if(it != segs_set.begin())
                {
                    it--;
                    most_rup[(*it).ind] = {x,y};
                }
                most_rdown[ind] = {x,y};
                most_rup[ind] = {x,y};
                segs_set.insert(segs[ind]);
            }
        }
        prev = {{x,y},ind};
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...