-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.cpp
208 lines (193 loc) · 4.51 KB
/
3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include<bits/stdc++.h>
using namespace std;
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long
#define fo(i,n) for(ll i = 0; i < n; i++)
#define pb push_back
#define N 2000
#define INF 10000000000
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
typedef vector<pii> vpii;
typedef set<pii> spii;
//trying to solve using convex hull wait... but its concave no?:)
ll a,b;
int in = 0;
pii p0;
vpii points;
vpii hulls[N];
//next poll
pii nextToTop(stack<pii> &S)
{
pii p = S.top();
S.pop();
pii res = S.top();
S.push(p);
return res;
}
// distance between p1 and p2
ll dist(pii p1, pii p2)
{
return (p1.first - p2.first)*(p1.first - p2.first) +
(p1.second - p2.second)*(p1.second - p2.second);
}
ll orientation(pii p, pii q, pii r)
{
ll val = (q.second - p.second) * (r.first - q.first) -
(q.first - p.first) * (r.second - q.second);
if (val == 0) return 0; // colinear
return (val > 0)? 1: 2; // clock or counterclock wise
}
bool cmp(pii p, pii q)
{
ll o = orientation(p0, p, q);
if (o == 0)
return (dist(p0, p)< dist(p0, q));
return (o == 2)? true: false;
}
ll isInside(pii p, pii q, pii r)
{
ll tmp = (r.second - p.second)* (q.first - p.first) - (r.first - p.first)* (q.second - p.second);
return tmp;
}
bool check(vpii v, pii p0)
{
pii p,q;
p.first = v[0].first;
p.second = v[0].second;
ll f;
for(int i = v.size()-1; i >=0; i--)
{
q.first = v[i].first;
q.second = v[i].second;
ll tmp = isInside(p,q,p0);
if(i == v.size()-1)
f = tmp;
else if((f<=0&&tmp>=0)||(f>=0&&tmp<=0))
return false;
p = q;
}
return true;
}
int Hull(vpii polls)
{
ll n = polls.size();
vpii v;
stack<pii> S;
//find minimum y coordinate-
ll ymin = polls[0].second;
ll ind = 0;
fo(i,n)
{
if(i && (ymin>polls[i].second||(ymin==polls[i].second&& polls[i].first< polls[ind].first)))
ymin = polls[i].second, ind = i;
}
// placing the minimum poll at the first position
{
pii temp = polls[0];
polls[0] = polls[ind];
polls[ind]=temp;
}
p0 = polls[0];
// fo(i,n)
//cout<<polls[i].first<<" "<<polls[i].second<<"a\n";
//sorting
sort(polls.begin()+1,polls.end(), cmp);
//fo(i,n)
//cout<<polls[i].first<<" "<<polls[i].second<<"aa\n";
// Initializing the size of modified array
ll m = 1;
for (ll i=1; i<n; i++)
{
//cout<<polls[i].first<<" "<<polls[i].second<<"r\n";
//if angle of i and i+1 is same remove them-
while (i < n-1 && orientation(p0, polls[i], polls[i+1]) == 0)
{
v.push_back(polls[i]);
i++;
}
polls[m] = polls[i];
m++;
}
// convex hull not possible :(
if (m < 3) return 0;
//push polls in the stack
S.push(polls[0]);
S.push(polls[1]);
S.push(polls[2]);
//cout<<polls[0].first<<" "<<polls[1].first<<" "<<polls[2].first<<"aa\n";
//return S;
// Process remaining
for (ll i = 3; i < m; i++)
{
while (!S.empty() && orientation(nextToTop(S), S.top(), polls[i]) != 2)
{
//cout<<"Yes\n";
v.push_back(S.top());
S.pop();
}
S.push(polls[i]);
}
points.clear();
points = v;
//cout<<S.size()<<"q\n";
while(S.size())
{
hulls[in].push_back(S.top());
S.pop();
}
int ind1 = 0;
for(int i = 0; i< points.size(); i++)
{
if(check(hulls[in],points[i]))
points[ind1++]=points[i];
}
points.erase(points.begin()+ind1, points.end());
in++;
return 1;
}
int main()
{
io
//freopen("test.out", "rt", stdin);
//freopen("test.out", "wt", stdout);
ll t;
cin>>t;
while(t--)
{
fo(i,N)
hulls[i].clear();
points.clear();
in = 0;
ll n,q, flag = 0;
cin>>n>>q;
fo(i,n)
{
cin>>a>>b;
points.push_back({a,b});
}
while(!points.empty())
{
int i = Hull(points);
if(!i)
break;
}
// cout<<in<<endl;
//cout<<hulls[1][0].first<<" "<<hulls[1][0].second<<"a\n";
//cout<<hulls[1][1].first<<" "<<hulls[1][1].second<<"a\n";
//cout<<hulls[1][2].first<<" "<<hulls[1][2].second<<"a\n";
//cout<<hulls[1][3].first<<" "<<hulls[1][3].second<<"a\n";
while(q--)
{
int ans = 0;
cin>>a>>b;
for(int i = in-1; i>= 0; i--)
{
if(check(hulls[i],{a,b}))
{ans = i+1;break;}
}
cout<<ans<<"\n";
}
}
return 0;
}