-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpp.snippets
More file actions
2616 lines (2186 loc) · 75.8 KB
/
cpp.snippets
File metadata and controls
2616 lines (2186 loc) · 75.8 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# competitive programming template
snippet temp
// ﷽
#include <bits/stdc++.h>
using namespace std;
#ifdef SAWALHY
#include "debug.hpp"
#else
#define debug(...) 0
#define debug_itr(...) 0
#define debug_bits(...) 0
#endif
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vii vector<pii>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define minit(v, x) v = min(v, x)
#define maxit(v, x) v = max(v, x)
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
${0}
return 0;
}
# competitive programming template with multi-tests
snippet tempt
// ﷽
#include <bits/stdc++.h>
using namespace std;
#ifdef SAWALHY
#include "debug.hpp"
#else
#define debug(...) 0
#define debug_itr(...) 0
#define debug_bits(...) 0
#endif
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vii vector<pii>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define minit(v, x) v = min(v, x)
#define maxit(v, x) v = max(v, x)
void solve() {
${0}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T = 1;
cin >> T;
for (int t = 1; t <= T; t++) {
debug("--------", t);
solve();
}
return 0;
}
snippet dbg
#ifdef SAWALHY
#include "debug.hpp"
#else
#define debug(...) 0
#define debug_itr(...) 0
#define debug_bits(...) 0
#endif
# increase the stack memory limit
snippet code_largestack
static void run_with_stack_size(void (*func)(void), size_t stsize) {
char *stack, *send;
stack = (char *)malloc(stsize);
send = stack + stsize - 16;
send = (char *)((uintptr_t)send / 16 * 16);
asm volatile(
"mov %%rsp, (%0)\n"
"mov %0, %%rsp\n"
:
: "r"(send));
func();
asm volatile("mov (%0), %%rsp\n" : : "r"(send));
free(stack);
}
int32_t main() {
run_with_stack_size(main_, 1024 * 1024 * 1024 / 2); // run with a 512 MB stack
return 0;
}
# Read an array of length n from the stdin
snippet arr
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
# Read a tree
snippet tree
int n;
cin >> n;
vector<int> adj[n];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v, u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
# Primality test (Brute force)
snippet code_is_prime
bool is_prime(ll n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (ll i = 3; i * i <= n; i += 2)
if (n % i == 0) return false;
return true;
}
# Primality test (Miller & Rabin probabilistic)
snippet code_miller_rabin_ptest
bool miller_rabin_ptest(unsigned ll n, int k = 3) {
if (n < 2) return false;
if (n == 2) return true;
while (k--) {
unsigned ll a = 1LL * rand() * rand() % (n - 2) + 2;// [2 ... n-1]
unsigned ll r = 1;
for (unsigned ll p = n - 1; p; p >>= 1) {
if (p & 1) r = r * a % n;
a = a * a % n;
}
if (r != 1) return false;
}
return true;// probably
}
# Prime factorization in O(sqrt(n))
snippet code_primefacts
map<ll, ll> primefacts(ll n) {
map<ll, ll> result;
int r = 0;
while (n % 2 == 0) {
r++;
n = n / 2;
}
if (r > 0)
result[2] = r;
int sqn = sqrt(n);
for (int i = 3; i <= sqn; i += 2) {
r = 0;
while (n % i == 0) {
r++;
n = n / i;
}
if (r > 0)
result[i] = r;
}
if (n > 2)
result[n] = 1;
return result;
}
# Euler's totient theorm
snippet code_totient
std::vector<int> phi(${1:n} + 1);
std::iota(phi.begin(), phi.end(), 0);
for (int i = 1; i <= ${2:$1}; i++) {
for (int j = i << 1; j <= ${2:$1}; j += i)
phi[j] -= phi[i];
}
# Sieve's algorithm to mark numbers as primes and composites
snippet code_sieve
void sieve(vector<bool> &is_prime) {
is_prime[1] = false;
is_prime[0] = false;
int s = is_prime.size();
for (int i = 4; i < s; i += 2)
is_prime[i] = false;
for (int i = 3; i * i < s; i += 2) {
if (is_prime[i]) {
for (int j = i * i; j < s; j += i + i)
is_prime[j] = false;
}
}
}
# Fast Sieve's algorithm to calc minimum prime
snippet code_fast_sieve
std::vector<int> minp, primes;
void sieve(int n) {
minp.assign(n + 1, 0), primes.clear();
for (int i = 2; i <= n; i++) {
if (minp[i] == 0) {
minp[i] = i;
primes.push_back(i);
}
for (auto p: primes) {
if (i * p > n) break;
minp[i * p] = p;
if (p == minp[i]) break;
}
}
}
# Dijkstra's algorithm
snippet code_dijkstra
long long dijkstra(int s, int e, vector<vector<pair<int, int>>> &adj) {
int n = adj.size();
vector<int> prev(n + 1);
vector<ll> dist(n + 1, 1e18);
typedef pair<ll, int> item;
priority_queue<item, deque<item>, greater<item>> qu;
qu.push({0, s});
dist[s] = 0;
while (!qu.empty()) {
auto [d, i] = qu.top();
qu.pop();
if (dist[i] < d) continue;
for (auto [j, D]: adj[i]) {
if (dist[j] > D + d) {
prev[j] = i;
dist[j] = D + d;
qu.push({dist[j], j});
}
}
}
// for (int i = e; i != s; i = prev[i]);
return dist[e];
}
# MST (Kruskal's algorithm)
snippet code_mst_kruskal
struct Edge {
int from, to;
long long weight;
Edge(int from, int to, long long weight) : from(from), to(to), weight(weight) {}
bool operator<(Edge &e) { return weight < e.weight; }
};
pair<long long, vector<Edge>> mst_kruskal(vector<Edge> &edges, int n) {
DSU uf(n + 1);
double cost = 0;
vector<Edge> mst_edges;
sort(edges.rbegin(), edges.rend());
while (!edges.empty()) {
auto &e = edges.back();
edges.pop_back();
if (uf.uni(e.from, e.to)) {
cost += e.weight;
mst_edges.push_back(e);
}
};
if (mst_edges.size() != n - 1)
return {1e18, {}};
return {cost, mst_edges};
}
# Geometry stuff for competitive prgramming
snippet code_geo
namespace Geometry
{
using T = long double;
const T EPS = 1e-8;
const double PI = acos(-1.0);
template<typename T, typename V>
int cmp(T a, V b) { return (a -= b) < -EPS ? -1 : (a > EPS ? 1 : 0); }
template<typename T, typename V>
bool iseq(T a, V b) { return cmp(a, b) == 0; }
template<typename T>
bool iseq0(T a) { return cmp(a, 0) == 0; }
template<typename T, typename V>
bool islte(T a, V b) { return cmp(a, b) != 1; }
template<typename T, typename V>
bool isgte(T a, V b) { return cmp(a, b) != -1; }
template<typename T, typename V>
bool islt(T a, V b) { return cmp(a, b) == -1; }
template<typename T, typename V>
bool isgt(T a, V b) { return cmp(a, b) == 1; }
template<typename T>
int sign(T val) { return cmp(val, 0); }
enum PointState { OUT,
IN,
ON };
typedef struct Point {
T x, y;
Point() {}
Point(T _x, T _y) : x(_x), y(_y) {}
Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }
Point operator/(T s) const { return Point(x / s, y / s); }
Point operator*(T s) const { return Point(x * s, y * s); }
T dot(const Point &p) const { return x * p.x + y * p.y; }
T cross(const Point &p) const { return x * p.y - y * p.x; }
T dot(const Point &a, const Point &b) const { return (a - *this).dot(b - *this); }
T cross(const Point &a, const Point &b) const { return (a - *this).cross(b - *this); }
T norm() const { return dot(*this); }
Point scale(long double l) {
auto a = ang();
return Point{l * cos(a), l * sin(a)};
};
long double len() const { return sqrtl(dot(*this)); }
long double ang(bool pos = true) const {
auto a = atan2l(y, x);
if (pos && a < 0) a += PI * 2;
return a;
}
Point rotate(const Point &p, long double a) { return (*this - p).rotate(a) + p; }
Point rotate(long double angle) {
auto l = len(), a = ang();
return Point(l * cos(a + angle), l * sin(a + angle));
}
bool operator==(const Point &p) const { return (*this - p).norm() <= EPS; }
bool operator!=(const Point &p) const { return !(*this == p); }
bool operator<(const Point &p) const { return x < p.x || (x == p.x && y < p.y); }
friend ostream &operator<<(ostream &os, const Point &p) { return os << '(' << p.x << ',' << p.y << ')'; }
friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }
} pt;
int ccw(const pt &a, pt b, pt c) {
if (a == b) return (a == c ? 0 : +3); // same point or different
b = b - a, c = c - a;
if (sign(b.cross(c)) == +1) return +1; // "COUNTER_CLOCKWISE"
if (sign(b.cross(c)) == -1) return -1; // "CLOCKWISE"
if (sign(b.dot(c)) == -1) return +2; // "ON_RAY_b_a)"
if (cmp(b.norm(), c.norm()) == -1) return -2; // "ON_RAY_a_b"
return 0; // "ON_SEGMENT"
}
bool colinear(const pt &a, const pt &b, const pt &c) {
return abs(ccw(a, b, c)) != 1;
}
pt slope(pt a, pt b, bool change_direction = true) {
assert(is_integral_v<T>);
long long dx = a.x - b.x;
long long dy = a.y - b.y;
if (dx == 0 && dy == 0) return pt(0, 0);
long long g = gcd(abs(dy), abs(dy));
dx /= g, dy /= g;
if (change_direction) {
if (dx < 0) dy *= -1, dx *= -1;
if (dx == 0) dy = abs(dy);
}
return pt(dx, dy);
}
struct Segment {
pt a, b;
Segment() {}
Segment(pt a, pt b) : a(a), b(b) {}
bool operator==(const Segment &s) const { return a == s.a ? b == s.b : a == s.b && b == s.a; };
friend istream &operator>>(istream &is, Segment &s) { return is >> s.a >> s.b; }
friend ostream &operator<<(ostream &os, const Segment &s) {
return os << "{" << s.a << ", " << s.b << "}";
}
};
struct Line : public Segment {
Line() {}
Line(pt a, pt b) : Segment(a, b) {}
Line(pt a, long double slope) : Segment(a, a + Point(1, 0).rotate(slope)) {}
bool operator==(const Line &l) const { return iseq0((a - b).cross(l.a - l.b)); };
};
struct Ray : public Segment {
Ray() {}
Ray(pt a, pt b) : Segment(a, b) {}
bool operator==(const Ray &r) const { return a == r.a && slope(a, b, false) == slope(r.a, r.b, false); };
};
struct Polygon {
int n;
vector<pt> verts;
Polygon() = default;
Polygon(int n) : n(n) { verts.resize(n); }
Polygon(vector<pt> &vert) : verts(vert), n(vert.size()) {}
T area2() const {
T a = 0;
for (int i = 2; i < n; i++)
a += verts[0].cross(verts[i], verts[i - 1]);
return abs(a);
}
long double area() const { return area2() / 2.0; };
void no_collinear() {
vector<pt> v;
for (int i = 0; i <= n; i++) {
while (v.size() > 1 && colinear(v.back(), v.end()[-2], verts[i % n]))
v.pop_back();
v.push_back(verts[i % n]);
}
v.pop_back();
n = v.size();
verts = v;
assert(n > 2);
}
void ensure_ccw() {
start_bottom_left();
if (ccw(verts[0], verts[1], verts.back()) == -1)
reverse(verts.begin() + 1, verts.end());
}
void start_bottom_left() {
int pos = 0; // most left-bottom point
for (int i = 1; i < n; i++)
if (verts[i] < verts[pos])
pos = i;
rotate(verts.begin(), verts.begin() + pos, verts.end());
}
};
bool parallel(const Line &a, const Line &b) { return (a.b - a.a).cross(b.b - b.a) == 0; }
bool orthogonal(const Line &a, const Line &b) { return (a.a - a.b).dot(b.a - b.b) == 0; }
bool intersect(const Line &l, const Line &m) { return !parallel(l, m); }
bool intersect(const pt &p, const Segment &s) { return ccw(s.a, s.b, p) == 0; }
bool intersect(const Segment &s, const pt &p) { return intersect(p, s); }
bool intersect(const pt &p, const Line &l) { return abs(ccw(l.a, l.b, p)) != 1; }
bool intersect(const Line &l, const pt &p) { return intersect(p, l); }
bool intersect(const Segment &s, const Line &l) { return ccw(l.a, l.b, s.a) * ccw(l.a, l.b, s.b) != 1; }
bool intersect(const Line &l, const Segment &s) { return intersect(s, l); }
bool intersect(const Segment &s, const Segment &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; }
bool intersect(const Segment &s, const Ray &r) {
auto d1 = (s.a - s.b).cross(r.b - r.a),
d2 = (s.a - r.a).cross(r.b - r.a),
d3 = (s.a - s.b).cross(s.a - r.a);
if (abs(d1) <= EPS)
return r.a.cross(r.b, s.a) == 0 &&
(r.a.dot(r.b, s.a) >= 0 || r.a.dot(r.b, s.b) >= 0); // NOT BACK
return sign(d1) * sign(d2) >= 0 && sign(d1) * sign(d3) >= 0 && abs(d2) <= abs(d1);
}
bool intersect(const Ray &r, const Segment &s) { return intersect(s, r); }
bool intersection(pt a, pt b, pt c, pt d, pt &inter) {
assert(is_floating_point_v<T>);
long double d1 = (a - b).cross(d - c);
long double d2 = (a - c).cross(d - c);
if (fabs(d1) <= EPS) return false;
long double t1 = d2 / d1;
inter = a + (b - a) * t1;
return true;
}
template<typename T, typename V>
bool intersection(const T &l, const V &m, pt &inter) {
if (!intersect(l, m)) return false;
return intersection(l.a, l.b, m.a, m.b, inter);
}
// - NOTE: The polygon shouldn't have collinear points.
// - NOTE: First vertex should be the bottom-left, points in ccw order.
vector<pt> intersection(const Polygon &poly, const Line &line) {
int n = poly.n;
vector<pt> inter;
const vector<pt> &verts = poly.verts;
pt x;
for (int i = 1; i <= n; i++) {
int I = i % n, J = i - 1, K = (i - 2 + n) % n;
if (intersection(line, Segment(verts[I], verts[J]), x)) {
if (x == verts[I]) continue;
if (x != verts[J]) {
inter.push_back(x);
continue;
}
int dir1 = ccw(line.a, line.b, verts[I]);
int dir2 = ccw(line.a, line.b, verts[K]);
if (dir1 * dir2 == -1)
// entering or leaving from a vertex
inter.push_back(verts[J]);
} else if (abs(ccw(line.a, line.b, verts[J])) != 1) {
// side (I, J) is on the line
bool isWideAngleI = islt(ccw(verts[I], verts[((I + 1) % n)], verts[J]), 0);
bool isWideAngleJ = islt(ccw(verts[J], verts[I], verts[K]), 0);
if (isWideAngleI) inter.push_back(verts[I]);
if (isWideAngleJ) inter.push_back(verts[J]);
inter.push_back(verts[I]);
inter.push_back(verts[J]);
}
}
debug(inter);
// sort in one direction, as if you travel on the line
// in this direction and see the points one by one
// NOTE: points may NOT be eaxctly on the line due to precesion errors
sort(all(inter), [&](pt l, pt r) {
return sign((line.b - line.a).dot(r - l)) == 1;
});
assert(inter.size() % 2 == 0);
return inter;
};
struct Circle {
pt c;
T r;
Circle() = default;
Circle(pt c, T r) : c(c), r(r) {}
Circle(const vector<pt> &p) {
if (p.size() == 1) c = p[0], r = 0;
else if (p.size() == 2) {
c = (p[0] + p[1]) / 2;
r = (p[0] - c).len();
} else {
assert(p.size() == 3);
*this = Circle(p[0], p[1], p[2]);
}
}
Circle(pt a, pt b, pt c) {
// if we have a cord in a circle,
// the perpendicular from the center will pass from the center
// so we simply solve for the interection of two lines
auto ABmid = (a + b) / 2.0, BCmid = (b + c) / 2.0;
auto ABnorm = pt((a - b).y, -(a - b).x);
auto BCnorm = pt((b - c).y, -(b - c).x);
bool valid = intersection(
Line(ABmid, ABmid + ABnorm),
Line(BCmid, BCmid + BCnorm), this->c);
assert(valid); // unless at least two points are identical
r = (a - this->c).len();
}
friend ostream &operator<<(ostream &os, const Circle &c) {
return os << "c{" << c.c << ", " << c.r << "}";
}
};
PointState point_in_triangle(pt a, pt b, pt c, pt point) {
int x = ccw(a, b, point), y = ccw(b, c, point), z = ccw(c, a, point);
if (sign(x) == sign(y) && sign(y) == sign(z)) return IN;
if (x * y * z == 0) return ON;
return OUT;
}
PointState point_in_circle(const pt &p, const vector<pt> &cir) {
if (cir.size() == 0) return OUT;
auto c = Circle(cir);
if (iseq((p - c.c).norm(), c.r * c.r)) return ON;
if (islt((p - c.c).norm(), c.r * c.r)) return IN;
return OUT;
}
PointState point_in_polygon(const pt &p, const vector<pt> &polygon) {
int wn = 0, n = polygon.size();
for (int i = 0, j = 1; i < n; i++, j++, j %= n) {
if (ccw(polygon[j], polygon[i], p) == 0) return ON;
if ((p.y < polygon[j].y) != (p.y < polygon[i].y)) {
wn += polygon[j].y > polygon[i].y && ccw(p, polygon[i], polygon[j]) == 1;
wn -= polygon[j].y < polygon[i].y && ccw(p, polygon[j], polygon[i]) == 1;
}
}
return wn == 0 ? OUT : IN;
}
PointState ray_and_polygon(const Ray &r, const Polygon &polygon) {
// NOTE: Should be a good ray (a != b),
// and non-degenerate polygon with no duplicated points
int n = polygon.n;
PointState ans = OUT;
for (int i = 0, j = 1, k = 2; i < n; i++, j++, k++, j %= n, k %= n) {
if (!intersect(Segment(polygon.verts[i], polygon.verts[j]), r)) continue;
auto x = r.a.cross(r.b, polygon.verts[i]);
auto y = r.a.cross(r.b, polygon.verts[j]);
auto z = r.a.cross(r.b, polygon.verts[k]);
if (x == 0) ans = ON; // Maybe tangent
else if (y == 0) {
// (the ray splits an internal angle)
// Entering from a vertex
if (sign(x) * sign(z) == -1) return IN;
} else return IN; // Entering from an edge
}
return ans;
}
vector<pt> &sort_clock(vector<pt> &points, bool cw = false) {
int n = points.size();
// choose the pivot (most bottom-right point)
for (int i = 1; i < n; i++) {
auto &l = points[0], &r = points[i];
int cy = cmp(l.y, r.y), cx = cmp(l.x, r.x);
if (cy == 0 ? cx == -1 : cy == +1) swap(l, r);
}
// sorting with points[0] as pivot
sort(points.begin() + 1, points.end(),
[&](pt l, pt r) {
auto c = ccw(points[0], l, r);
int cx = cmp(l.x, r.x), cy = cmp(l.y, r.y);
// closer to bottom-right comes first
if (abs(c) != 1) return cy == 0 ? cx == 1 : cy == -1;
return cw ? c == -1 : c == 1;
});
return points;
}
// sort a convex polygon cw or ccw with the bottom-right as the pivot
vector<pt> &sort_convex(vector<pt> &points, bool cw = false) {
int n = points.size();
// choose the pivot (most bottom-right point)
for (int i = 1; i < n; i++) {
auto &l = points[0], &r = points[i];
int cy = cmp(l.y, r.y), cx = cmp(l.x, r.x);
if (cy == 0 ? cx == -1 : cy == +1) swap(l, r);
}
// sorting with points[0] as pivot
sort(points.begin() + 1, points.end(),
[&](pt l, pt r) {
auto c = ccw(points[0], l, r);
int cx = cmp(l.x, r.x), cy = cmp(l.y, r.y);
if (abs(c) != 1) { // collinear
if (cw) return cy == 0 ? cx == 1 : cy == 1;
else
return cy == 0 ? cx == -1 : cy == -1;
}
return cw ? c == -1 : c == 1;
});
return points;
}
vector<pt> convexhull(vector<pt> &p, bool strict = false) {
int n = p.size(), k = 0, sgn = strict ? 0 : -1;
if (n <= 2) return p;
vector<pt> ch(2 * n); // CCW
auto cmp = [](pt x, pt y) { return (x.x != y.x ? x.x < y.x : x.y < y.y); };
sort(begin(p), end(p), cmp);
for (int i = 0; i < n; ch[k++] = p[i++]) // lower hull
while (k >= 2 && sign((ch[k - 1] - ch[k - 2]).cross(p[i] - ch[k - 1])) <= sgn) --k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) // upper hull
while (k >= t && sign((ch[k - 1] - ch[k - 2]).cross(p[i] - ch[k - 1])) <= sgn) --k;
ch.resize(k - 1);
return ch;
}
struct PointInConvex {
int n;
vector<pt> seq;
pt translation;
PointInConvex(vector<pt> polygon) { prepare_convex_ccw(polygon); }
void prepare_convex_ccw(vector<pt> &points) {
// NOTE: the polygon should be strictly convex
n = points.size();
int pos = 0; // most left-bottom point
for (int i = 1; i < n; i++)
if (points[i] < points[pos])
pos = i;
rotate(points.begin(), points.begin() + pos, points.end());
seq.resize(n);
for (int i = 0; i < n; i++)
seq[i] = points[(i + 1) % n] - points[0];
translation = points[0];
}
int check(pt point) {
point = point - translation;
if (intersect(point, Segment(pt(0, 0), seq[0]))) return 0;
if (seq.size() <= 2) return -1;
int l = 0, r = n - 1;
while (r - l > 1) {
int mid = (l + r) / 2;
if (sign(seq[mid].cross(point)) != -1)
l = mid;
else
r = mid;
}
int ok = point_in_triangle(seq[l], seq[l + 1], pt(0, 0), point);
if (ok == -1) return -1;
if (intersect(point, Segment(seq[l], seq[l + 1]))) return 0;
return 1;
}
};
bool cut(const Polygon &ploy, const Line &l, double d) {
auto &p = poly.verts;
int n = p.size();
vector<pt> new_polygon;
bool has_intersection = false;
bool active = true;
pt u = ((l.b - l.a) / (l.b - l.a).len()).rotate(PI / 2) * d;
Line newl = Line(l.a + u, l.b + u);
for (int i = 0; i < n; i++) {
pt cur = p[i];
pt next = p[(i + 1) % n];
if (cur.x == next.x && cur.y == next.y) continue;
pt inter;
double v1 = (newl.b - newl.a).cross(cur - newl.a);
double v2 = (newl.b - newl.a).cross(next - newl.a);
if (active) {
new_polygon.push_back(cur);
}
if (v1 * v2 < 0 && intersection(newl.a, newl.b, cur, next, inter)) {
if ((next - cur).cross(newl.b - newl.a) > EPS) {
active = false;
} else if ((next - cur).cross(newl.b - newl.a) < EPS) {
if (!has_intersection) new_polygon.clear();
active = true;
}
new_polygon.push_back(inter);
has_intersection = true;
}
}
bool in_correct_dir = (p[0] - newl.a).dot(u) > -EPS;
if (!has_intersection && !in_correct_dir) return false;
p = new_polygon;
return true;
}
struct Welzl {
vector<pt> points;
Welzl(vector<pt> &_points) : points(_points) {
shuffle(all(points), default_random_engine(time(NULL)));
}
Circle get_circle() { return Circle(go()); }
vector<pt> go(int i = 0, vector<pt> cir = {}) {
if (cir.size() == 3 || i == (int) points.size()) return cir;
auto new_cir = go(i + 1, cir);
if (point_in_circle(points[i], new_cir) != OUT)
return new_cir;
cir.push_back(points[i]);
return go(i + 1, cir);
}
};
}; // namespace Geometry
using namespace Geometry;
# STL policy container (oset, omap)
snippet code_ordered_set
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
# Segment tree (Simple implementation)
snippet code_segtree_simple
// source: https://codeforces.com/blog/entry/18051
struct Segtree {
constexpr static ll DEFAULT = 1e18;
int n = 0;
vector<ll> tree;
Segtree() = default;
Segtree(int n) : n(n) { tree.assign(n * 2, DEFAULT); }
inline ll merge(const ll &a, const ll &b) { return min(a, b); }
void pull(int i) { tree[i] = merge(tree[i << 1], tree[i << 1 | 1]); }
void build() {
for (int i = n - 1; i > 0; i--) pull(i);
}
void update(int i, ll val) {
for (tree[i += n] = val; i > 1; i >>= 1) pull(i >> 1);
}
auto query(int l, int r) {
ll resl = DEFAULT, resr = resl;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) resl = merge(resl, tree[l++]);
if (r & 1) resr = merge(tree[--r], resr);
}
return merge(resl, resr);
}
};
# Segment tree (Sawalhy's implementation)
snippet code_segtree
struct Value;
struct Update;
struct Node;
// Replaceable by primitives (using Value = long long)
struct Value {
long long sum = 0, mn = 1e18, mx = -1e18;
Value() = default;
Value(ll value) { sum = mn = mx = value; }
// make sure that: x + Value() == x
Value &operator+=(const Value &other) {
sum += other.sum;
mn = min(mn, other.mn);
mx = max(mx, other.mx);
return *this;
}
Value operator+(const Value &other) const {
return Value(*this) += other;
}
};
struct Update {
// NOTE: Sometime you need to split the update, in these cases
// you should include the range [a, b] of the update in the struct Update
int value;
enum State {
idle,
relative,
forced
} state = idle;
Update() = default;
Update(int value, State state = forced) : value(value), state(state){};
Update &operator+=(const Update &other) {
if (state == idle || other.state == forced) {
*this = other;
} else {
assert(other.state == relative);
value += other.value;
}
return *this;
}
void apply_on(Value &other, int cnt) const {
if (state == forced) other = value;
// else other += value;
// other.sum += value * (cnt - 1);
}
// updates on all leafs (node.l == node.r) are applicable
// updates are applicable on decendant node values if it is applicable on the parent node
inline bool is_applicable(const Value &v) const { return true; }
Update get(const Node &node) const { return *this; }
};
struct Node {
int l = -1, r = -1; // [l, r]
Update up;
Value value;
Node() = default;
Node(int l, int r, const Value &value) : l(l), r(r), value(value){};
void update(const Update &up) { this->up += up; }
void apply_update() {
up.apply_on(value, r - l + 1);
up.state = Update::idle;
}
};
struct Segtree {
int n;
vector<Node> tree;
Segtree(int n) {
if ((n & (n - 1)) != 0)
n = 1 << (32 - __builtin_clz(n));
this->n = n;
tree.assign(n << 1, Node());
for (int i = n; i < n << 1; i++)
tree[i].l = tree[i].r = i - n;
for (int i = n - 1; i > 0; i--)
tree[i].l = tree[i << 1].l, tree[i].r = tree[i << 1 | 1].r;
}
Segtree(const vector<Value> &values) : Segtree(values.size()) {
for (int i = 0; i < (int) values.size(); i++)
tree[i + n].value = values[i];
build();
}
void build() {
for (int i = n - 1; i > 0; --i) pull(i);
}
inline auto query(int i) { return query(1, i, i); }
inline auto query(int i, int j) { return query(1, i, j); }
inline auto update(int i, int j, const Update &val) { return update(1, i, j, val); }
void pull(int i) {
tree[i].value = tree[i << 1].value + tree[i << 1 | 1].value;
}
void push(int i) {
if (tree[i].up.state != Update::idle) {
if (i < n) {
int l = i << 1, r = i << 1 | 1;
tree[l].update(tree[i].up.get(tree[l]));
tree[r].update(tree[i].up.get(tree[r]));
}
tree[i].apply_update();
}
}
Value query(int i, int l, int r) {
push(i);
if (tree[i].r < l || r < tree[i].l) return Value(); // Identity Value
if (l <= tree[i].l && tree[i].r <= r) return tree[i].value;
return query(i << 1, l, r) + query(i << 1 | 1, l, r);
}
void update(int i, int l, int r, const Update &up) {
push(i);
if (tree[i].r < l || r < tree[i].l) return;
if (l <= tree[i].l && tree[i].r <= r && up.is_applicable(tree[i])) {
tree[i].update(up);
push(i);
return;
}
update(i << 1, l, r, up.get(tree[i << 1]));
update(i << 1 | 1, l, r, up.get(tree[i << 1 | 1]));
pull(i);