We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Can somebody please, provide an Editorial for this challenge ? I tried using the ray crossing algorithm, but only the sample test cases pass and the others Time Out ! Is there any other technique which can be applied ? Or can my code be optimised ?
#include<iostream>#include<bits/stdc++.h>#define ll long longusingnamespacestd;#define x first#define y second#define Point pair<int,int>#define INF 500000boolonSegment(Pointp,Pointq,Pointr){if(q.x<=max(p.x,r.x)&&q.x>=min(p.x,r.x)&&q.y<=max(p.y,r.y)&&q.y>=min(p.y,r.y))returntrue;returnfalse;}intorientation(Pointp,Pointq,Pointr){into=(q.y-p.y)*(r.x-q.x)-(q.x-p.x)*(r.y-q.y);if(o==0)return0;return(o>0)?1:-1;}booldoesIntersect(Pointp1,Pointq1,Pointp2,Pointq2){into1=orientation(p1,q1,p2);into2=orientation(p1,q1,q2);into3=orientation(p2,q2,p1);into4=orientation(p2,q2,q1);if(o1!=o2&&o3!=o4)returntrue;if(o1==0&&onSegment(p1,p2,q1))returntrue;if(o2==0&&onSegment(p1,q2,q1))returntrue;if(o3==0&&onSegment(p2,p1,q2))returntrue;if(o4==0&&onSegment(p2,q1,q2))returntrue;returnfalse;}boolisInside(PointPolygon[],intn,Pointp){//cout<<"Flag"<<endl;if(n<3)returnfalse;inti=0,next;llcount=0;Pointextream={INF,p.y};do{next=(i+1)%n;//cout<<(Polygon[i]==p)<<endl;if(Polygon[i]==p)returntrue;//cout<<Polygon[i].x<<";"<<Polygon[i].y<<" "<<Polygon[next].x<<";"<<Polygon[next].y<<endl;//cout<<Polygon[i].x<<";"<<Polygon[i].y<<" "<<p.x<<";"<<p.y<<endl;if(doesIntersect(Polygon[i],Polygon[next],p,extream)){if(orientation(Polygon[i],Polygon[next],p)==0){if(onSegment(Polygon[i],p,Polygon[next]))returntrue;}elsecount++;}i=next;}while(i!=0);return(count%2!=0);}llsolve(Pointset[],Pointpolygon[],intn,intm){llcount=0;for(inti=0;i<n;i++){if(isInside(polygon,m,set[i])){count++;//cout<<set[i].x<<" "<<set[i].y<<endl;}}returncount;}intmain(){intn,q,m;cin>>n>>q;Pointset[n];for(inti=0;i<n;i++)cin>>set[i].x>>set[i].y;Pointpolygon[20];while(q--){/* code */cin>>m;for(inti=0;i<m;i++)cin>>polygon[i].x>>polygon[i].y;cout<<solve(set,polygon,n,m)<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Polygon
You are viewing a single comment's thread. Return to all comments →
Can somebody please, provide an Editorial for this challenge ? I tried using the ray crossing algorithm, but only the sample test cases pass and the others Time Out ! Is there any other technique which can be applied ? Or can my code be optimised ?