Skip to content

Commit dd54a07

Browse files
authored
Class #13
1 parent b435029 commit dd54a07

File tree

2 files changed

+294
-0
lines changed

2 files changed

+294
-0
lines changed

Kattis Supercomputer.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Created by Alon on 30/06/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
12+
using namespace std;
13+
typedef long long ll;
14+
15+
#define MAX_FOR_H 1000001
16+
17+
int BIT[MAX_FOR_H] = {0}, n;
18+
void update(int x, int val) { for (; x <= n; x += x & -x) BIT[x] += val; }
19+
int query(int x) {
20+
int sum = 0;
21+
for (; x > 0; x -= x & -x) sum += BIT[x];
22+
return sum;
23+
}
24+
int main() {
25+
int K;
26+
cin >> n >> K;
27+
while (K--) {
28+
char c;
29+
cin >> c;
30+
if (c == 'F') {
31+
int num;
32+
cin >> num;
33+
int c = (query(num) - query(num-1) == 1) ? -1 : 1;
34+
update(num, c);
35+
} else { // case C
36+
int start, end;
37+
cin >> start >> end;
38+
cout << query(end) - query(start-1) << endl;
39+
}
40+
}
41+
42+
return 0;
43+
}
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
//
2+
// Created by Alon on 30/06/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
12+
using namespace std;
13+
typedef long long ll;
14+
15+
16+
17+
18+
#include <iostream>
19+
#include <cstring>
20+
#include <algorithm>
21+
#include <vector>
22+
#include <queue>
23+
#include <bitset>
24+
#include <cmath>
25+
#include <map>
26+
#include <unordered_map>
27+
#include <set>
28+
#include <numeric>
29+
#include <stack>
30+
31+
#define INF 1e9
32+
#define EPS 1e-7
33+
#define PI acos(-1.0)
34+
35+
#define FIND(ds, x) (find(ds.begin(), ds.end(), x))
36+
#define CONTAINS(ds, x) (FIND(ds, x) != ds.end())
37+
38+
using namespace std;
39+
40+
typedef long long ll;
41+
42+
typedef pair<int, int> ipair;
43+
typedef pair<long long, long long> llpair;
44+
typedef pair<double, double> dpair;
45+
typedef pair<double, double> dpair;
46+
typedef pair<char, char> cpair;
47+
48+
typedef vector<int> ivec;
49+
typedef vector<long long> llvec;
50+
typedef vector<double> dvec;
51+
typedef vector<bool> bvec;
52+
typedef vector<char> cvec;
53+
54+
typedef set<int> iset;
55+
typedef set<long long> llset;
56+
typedef set<double> dset;
57+
typedef set<bool> bset;
58+
typedef set<char> cset;
59+
60+
typedef stack<int> istack;
61+
typedef stack<long long> llstack;
62+
typedef stack<double> dstack;
63+
typedef stack<bool> bstack;
64+
typedef stack<char> cstack;
65+
66+
typedef queue<int> iqueue;
67+
typedef queue<long long> llqueue;
68+
69+
typedef priority_queue<int, vector<int>, greater<int>> ipqueue;
70+
typedef priority_queue<long long, vector<long long>, greater<long long>> llpqueue;
71+
72+
typedef vector<vector<long long>> graph;
73+
typedef vector<vector<pair<long long, long long>>> wgraph;
74+
75+
double DEG_to_RAD(double d) { return d * PI / 180.0; }
76+
77+
double RAD_to_DEG(double r) { return r * 180.0 / PI; }
78+
79+
class point {
80+
public:
81+
double x = 0, y = 0;
82+
83+
point() = default;
84+
85+
point(double x, double y) : x(x), y(y) {}
86+
87+
bool operator<(point other) const {
88+
if (fabs(x - other.x) > EPS) {
89+
return x < other.x;
90+
}
91+
return y < other.y;
92+
}
93+
94+
bool operator==(point other) const {
95+
return fabs(x - other.x) < EPS && fabs(y - other.y) < EPS;
96+
}
97+
98+
point operator+(point other) const {
99+
return {x + other.x, y + other.y};
100+
}
101+
102+
point operator-(point other) const {
103+
return {x - other.x, y - other.y};
104+
}
105+
106+
};
107+
108+
point operator*(double c, point p) {
109+
return {c * p.x, c * p.y};
110+
}
111+
112+
double dot(point p, point q) {
113+
return p.x * q.x + p.y * q.y;
114+
}
115+
116+
double norm2(point p) {
117+
return dot(p, p);
118+
}
119+
120+
double dist2(point p, point q) {
121+
return norm2(p - q);
122+
}
123+
124+
double cross(point p, point q) {
125+
return p.x * q.y - p.y * q.x;
126+
}
127+
128+
point projectPointOntoSegment(point a, point b, point c) {
129+
double r = dist2(a, b);
130+
if (fabs(r) < EPS) return a;
131+
r = dot(c - a, b - a) / r;
132+
if (r < 0) return a;
133+
if (r > 1) return b;
134+
return a + r * (b - a);
135+
}
136+
137+
bool pointOnPolygon(const vector<point>& p, point q) {
138+
for (int i = 0; i < p.size(); i++) {
139+
if (projectPointOntoSegment(p[i], p[(i + 1) % p.size()], q) == q) {
140+
return true;
141+
}
142+
}
143+
return false;
144+
}
145+
146+
bool pointInPolygon(const vector<point>& p, point q) {
147+
bool c = false;
148+
for (int i = 0; i < p.size(); i++) {
149+
int j = (i + 1) % p.size();
150+
if ((p[i].y <= q.y && q.y < p[j].y || p[j].y <= q.y && q.y < p[i].y) &&
151+
q.x < p[i].x + (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y)) {
152+
c = !c;
153+
}
154+
}
155+
return c || pointOnPolygon(p,q);
156+
}
157+
158+
bool ccw(point p, point q, point r) {
159+
return cross((p - q), (p - r)) > 0;
160+
}
161+
162+
vector<point> convex_hull(vector<point> Points) {
163+
164+
//-------------- incremental alg. ---------
165+
// upper hull
166+
sort(Points.begin(), Points.end());
167+
stack<point> stk_up;
168+
stk_up.push(Points[0]);
169+
stk_up.push(Points[1]);
170+
for (int i = 2; i < Points.size(); i++) {
171+
while ((stk_up.size() >= 2)) {
172+
point p2, p3;
173+
p2 = stk_up.top();
174+
stk_up.pop();
175+
p3 = stk_up.top();
176+
if (ccw(Points[i], p2, p3)) {
177+
stk_up.push(p2);
178+
break;
179+
}
180+
}
181+
stk_up.push(Points[i]);
182+
}
183+
// lower hull
184+
for (int i = 0; i < Points.size(); i++) {
185+
Points[i].x = -Points[i].x;
186+
Points[i].y = -Points[i].y;
187+
}
188+
sort(Points.begin(), Points.end());
189+
stack<point> stk_low;
190+
stk_low.push(Points[0]);
191+
stk_low.push(Points[1]);
192+
for (int i = 2; i < Points.size(); i++) {
193+
while ((stk_low.size() >= 2)) {
194+
point p2, p3;
195+
p2 = stk_low.top();
196+
stk_low.pop();
197+
p3 = stk_low.top();
198+
if (ccw(Points[i], p2, p3)) {
199+
stk_low.push(p2);
200+
break;
201+
}
202+
}
203+
stk_low.push(Points[i]);
204+
}
205+
206+
// print convex hull - cw order from leftmost point
207+
vector<point> CH;
208+
stk_low.pop();
209+
point p;
210+
while (!stk_low.empty()) {
211+
p = stk_low.top();
212+
p.x = -p.x;
213+
p.y = -p.y;
214+
CH.push_back(p);
215+
stk_low.pop();
216+
}
217+
stk_up.pop();
218+
while (!stk_up.empty()) {
219+
CH.push_back(stk_up.top());
220+
stk_up.pop();
221+
}
222+
reverse(CH.begin(), CH.end()); // ccw -> cw
223+
return CH;
224+
}
225+
226+
227+
228+
int main() { // Cops and Robbers - Kattis copsandrobbers
229+
int c, r, o, index = 0;
230+
while (cin >> c >> r >> o) {
231+
if (!c && !r && !o) break;
232+
cout << "Data set " << ++index << ":" << endl;
233+
vector<point> cops(c), robbers(r);
234+
for (point &p : cops) cin >> p.x >> p.y;
235+
vector<point> ch_cops = c > 2 ? convex_hull(cops) : vector<point>();
236+
for (point &p : robbers) cin >> p.x >> p.y;
237+
vector<point> ch_robbers = r > 2 ? convex_hull(robbers) : vector<point>();
238+
for (int i = 0; i < o; ++i) {
239+
point p;
240+
cin >>p. x >> p.y;
241+
cout << " Citizen at (" << p.x << "," << p.y << ") is ";
242+
if (pointInPolygon(ch_cops, p)) {
243+
cout << "safe." << endl;
244+
} else {
245+
cout << (pointInPolygon(ch_robbers, p) ? "robbed." : "neither.") << endl;
246+
}
247+
}
248+
cout << endl;
249+
}
250+
return 0;
251+
}

0 commit comments

Comments
 (0)