Submission #1379118


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ln length()
#define M 1000000007
ll r,c,n;
vector<pair<pll,pll> >lines;

// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
bool onSegment(pll p, pll q, pll r)
{
    if (q.F <= max(p.F, r.F) && q.F >= min(p.F, r.F) &&
        q.S <= max(p.S, r.S) && q.S >= min(p.S, r.S))
       return true;

    return false;
}

// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
ll orientation(pll p, pll q, pll r)
{
    // See http://www.geeksforgeeks.org/orientation-3-ordered-points/
    // for details of below formula.
    int val = (q.S - p.S) * (r.F - q.F) -
              (q.F - p.F) * (r.S - q.S);

    if (val == 0) return 0;  // colinear

    return (val > 0)? 1: 2; // clock or counterclock wise
}

// The main function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
bool intersect(pll p1, pll q1, pll p2, pll q2)
{
    // Find the four orientations needed for general and
    // special cases
    ll o1 = orientation(p1, q1, p2);
    ll o2 = orientation(p1, q1, q2);
    ll o3 = orientation(p2, q2, p1);
    ll o4 = orientation(p2, q2, q1);

    // General case
    if (o1 != o2 && o3 != o4)
        return true;

    // Special Cases
    // p1, q1 and p2 are colinear and p2 lies on segment p1q1
    if (o1 == 0 && onSegment(p1, p2, q1)) return true;

    // p1, q1 and p2 are colinear and q2 lies on segment p1q1
    if (o2 == 0 && onSegment(p1, q2, q1)) return true;

    // p2, q2 and p1 are colinear and p1 lies on segment p2q2
    if (o3 == 0 && onSegment(p2, p1, q2)) return true;

     // p2, q2 and q1 are colinear and q1 lies on segment p2q2
    if (o4 == 0 && onSegment(p2, q1, q2)) return true;

    return false; // Doesn't fall in any of the above cases
}

bool check(){
    vector<pair<pll,ll> > pts;
    map<pll,pll> getright,getleft;
    for(auto xp:lines) pts.pb(mp(xp.F,1)),pts.pb(mp(xp.S,2)),getright[xp.F]=xp.S,getleft[xp.S]=xp.F;
    sort(pts.begin(),pts.end());
    set<pair<ll,pair<pll,pll> > > active;
    for(auto xp:pts){
        if(xp.S == 1){
            pll xpr = getright[xp.F];
            pair<pll,pll> line = mp(xp.F,xpr);
            ll yc = xp.F.S;
            active.insert(mp(yc,line));

            auto it = active.lower_bound(mp(yc,line));

            it++;
            if(it!=active.end()){
                auto idk = *it;
                pair<pll,pll> line2 = idk.S;
                if(intersect(line2.F,line2.S,line.F,line.S)) return false;
            }
            it--;

            if(it!=active.begin()){
                it--;
                auto idk = *it;
                pair<pll,pll> line2 = idk.S;
                if(intersect(line2.F,line2.S,line.F,line.S)) return false;
            }
        }
        else{
            pll xpl = getleft[xp.F];
            pair<pll,pll> line = mp(xpl,xp.F);
            ll yc = xpl.S;
            active.erase(mp(yc,line));

            auto it = active.lower_bound(mp(yc,line));
            if(it==active.end() || it == active.begin()) continue;

            auto idk = *it;
            pair<pll,pll> line2 = idk.S;

            it--;
            auto idk2 = *it;
            pair<pll,pll> line3 = idk2.S;
            if(intersect(line2.F,line2.S,line3.F,line3.S)) return false;
        }
    }

    return true;
}

int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>r>>c>>n;
    rep(i,1,n){
        ll a1,b1,a2,b2;cin>>a1>>b1>>a2>>b2;
        if ((a1 == 0 || a1 == r || b1 == 0 || b1 == c) && (a2 == 0 || a2 == r || b2 == 0 || b2 == c))
            lines.pb(mp(mp(a1,b1),mp(a2,b2)));
        /*if(a1 == 0 && a2!=0 && (a2 == r || b2 == 0 || b2 == c)) lines.pb(mp(mp(a1,b1),mp(a2,b2)));
        else if(a1 == r && a2!=r && (a2 == 0 || b2 == 0 || b2 == c)) lines.pb(mp(mp(a1,b1),mp(a2,b2)));
        else if(b1 == 0 && b2!=0 && (b2 == c || a2 == 0 || a2 == r)) lines.pb(mp(mp(a1,b1),mp(a2,b2)));
        else if(b1 == c && b2!=c && (b2 == 0 || a2 == 0 || a2 == r)) lines.pb(mp(mp(a1,b1),mp(a2,b2)));*/
    }
    if(lines.size()<=1){
        cout<<"YES"<<endl;
        return 0;
    }
    n=lines.size();
    if(check()) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

Submission Info

Submission Time
Task E - Connected?
User ShivRam
Language C++14 (GCC 5.4.1)
Score 0
Code Size 4767 Byte
Status WA
Exec Time 141 ms
Memory 25192 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 4
AC × 26
WA × 12
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt, s4.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 43.txt, 44.txt, 45.txt, 46.txt, s1.txt, s2.txt, s3.txt, s4.txt
Case Name Status Exec Time Memory
01.txt WA 99 ms 19564 KB
02.txt WA 86 ms 13036 KB
03.txt WA 90 ms 13676 KB
04.txt WA 51 ms 4980 KB
05.txt WA 86 ms 13036 KB
06.txt WA 133 ms 23400 KB
07.txt AC 43 ms 2680 KB
08.txt AC 96 ms 15340 KB
09.txt AC 66 ms 10608 KB
10.txt AC 121 ms 21612 KB
11.txt AC 104 ms 21484 KB
12.txt WA 36 ms 256 KB
13.txt WA 45 ms 3060 KB
14.txt AC 73 ms 10476 KB
15.txt AC 73 ms 10092 KB
16.txt WA 40 ms 1404 KB
17.txt WA 37 ms 640 KB
18.txt WA 36 ms 512 KB
19.txt AC 54 ms 5616 KB
20.txt AC 131 ms 23400 KB
21.txt AC 66 ms 9456 KB
22.txt AC 72 ms 10352 KB
23.txt AC 136 ms 24168 KB
24.txt AC 136 ms 24680 KB
25.txt WA 141 ms 24168 KB
26.txt AC 138 ms 24040 KB
27.txt AC 140 ms 25192 KB
28.txt AC 136 ms 24296 KB
29.txt AC 138 ms 24808 KB
30.txt AC 137 ms 24168 KB
43.txt AC 1 ms 256 KB
44.txt AC 1 ms 256 KB
45.txt AC 1 ms 256 KB
46.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB
s3.txt AC 1 ms 256 KB
s4.txt AC 1 ms 256 KB