From 8ca4bbf3e4e7be7eb00dc83ed83969c992367a41 Mon Sep 17 00:00:00 2001 From: mzuenni Date: Fri, 3 Feb 2023 19:39:14 +0100 Subject: simplified code --- geometry/polygon.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geometry/polygon.cpp b/geometry/polygon.cpp index 3a633af..6465fdb 100644 --- a/geometry/polygon.cpp +++ b/geometry/polygon.cpp @@ -40,13 +40,13 @@ bool inside(pt p, const vector& poly) { // Change line 43 and 49 >= if border counts as inside bool inside(pt p, const vector& hull) { int l = 0, r = sz(hull) - 1; - if (orientation(hull[0], hull[r], p) > 0) return false; + if (cross(hull[0], hull[r], p) > 0) return false; while (l + 1 < r) { int m = (l + r) / 2; - if (orientation(hull[0], hull[m], p) >= 0) l = m; + if (cross(hull[0], hull[m], p) >= 0) l = m; else r = m; } - return orientation(hull[l], hull[r], p) > 0; + return cross(hull[l], hull[r], p) > 0; } void rotateMin(vector& hull) { -- cgit v1.2.3 From c5fa5456eb8a31c5eb8af129bc6f3dc39c54a894 Mon Sep 17 00:00:00 2001 From: mzuenni Date: Sat, 4 Feb 2023 13:05:14 +0100 Subject: fix --- geometry/polygon.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geometry/polygon.cpp b/geometry/polygon.cpp index 6465fdb..9dd4f66 100644 --- a/geometry/polygon.cpp +++ b/geometry/polygon.cpp @@ -2,7 +2,7 @@ // Punkte gegen den Uhrzeigersinn: positiv, sonst negativ. double area(const vector& poly) { //poly[0] == poly.back() double res = 0; - for (int i = 0; i + 1 < n; i++) + for (int i = 0; i + 1 < sz(poly); i++) res += cross(poly[i], poly[i + 1]); return 0.5 * res; } @@ -27,7 +27,7 @@ bool inside(pt p, const vector& poly) { bool in = false; for (int i = 0; i + 1 < sz(poly); i++) { pt a = poly[i], b = poly[i + 1]; - if (pointOnLineSegment(a, b, b)) return false; + if (pointOnLineSegment(a, b, p)) return false; if (real(a) > real(b)) swap(a,b); if (real(a) <= real(p) && real(p) < real(b) && cross(p, a, b) < 0) { -- cgit v1.2.3 From 0ac199d769e6ff70aaf6a41d965b5e4c8525b570 Mon Sep 17 00:00:00 2001 From: mzuenni Date: Sat, 4 Feb 2023 13:07:19 +0100 Subject: added comment --- geometry/polygon.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/geometry/polygon.cpp b/geometry/polygon.cpp index 9dd4f66..23420a1 100644 --- a/geometry/polygon.cpp +++ b/geometry/polygon.cpp @@ -9,6 +9,8 @@ double area(const vector& poly) { //poly[0] == poly.back() // Anzahl drehungen einer Polyline um einen Punkt // p nicht auf rand und poly[0] == poly.back() +// res != 0 or (res & 1) != 0 um inside zu prüfen bei +// selbstschneidenden polygonen (definitions sache) ll windingNumber(pt p, const vector& poly) { ll res = 0; for (int i = 0; i + 1 < sz(poly); i++) { -- cgit v1.2.3 From 35871798d6864f3130399ba845f43bb724d49595 Mon Sep 17 00:00:00 2001 From: Noobie99 Date: Sat, 4 Feb 2023 15:56:51 +0100 Subject: better treap --- datastructures/datastructures.tex | 4 +- datastructures/treap2.cpp | 78 ++++++++++++++++++++++++++++++++++++++ tcr.pdf | Bin 645307 -> 662433 bytes 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 datastructures/treap2.cpp diff --git a/datastructures/datastructures.tex b/datastructures/datastructures.tex index 8d1cb02..c39a2c6 100644 --- a/datastructures/datastructures.tex +++ b/datastructures/datastructures.tex @@ -58,8 +58,8 @@ \sourcecode{datastructures/firstUnused.cpp} \end{algorithm} -\begin{algorithm}{Treap (Cartesian Tree)} - \sourcecode{datastructures/treap.cpp} +\begin{algorithm}{(Implicit) Treap (Cartesian Tree)} + \sourcecode{datastructures/treap2.cpp} \end{algorithm} \begin{algorithm}[optional]{Range Minimum Query} diff --git a/datastructures/treap2.cpp b/datastructures/treap2.cpp new file mode 100644 index 0000000..e0c32a6 --- /dev/null +++ b/datastructures/treap2.cpp @@ -0,0 +1,78 @@ +mt19937 rng(0xc4bd5dad); +struct Treap { + struct Node { + ll val; + int prio, sz = 1, l = -1, r = -1; + Node (ll x) : val(x), prio(rng()) {} + }; + + vector treap; + int root = -1; + + int getSize(int v) { + return v < 0 ? 0 : treap[v].sz; + } + + void upd(int v) { + if (v < 0) return; + auto *V = &treap[v]; + V->sz = 1 + getSize(V->l) + getSize(V->r); + // Update Node Code + } + + void push(int v) { + if (v < 0) return; + //auto *V = &treap[v]; + //if (V->lazy) { + // Lazy Propagation Code + // if (V->l >= 0) treap[V->l].lazy = true; + // if (V->r >= 0) treap[V->r].lazy = true; + // V->lazy = false; + //} + } + + pair split(int v, int k) { + if (v < 0) return {-1, -1}; + auto *V = &treap[v]; + push(v); + if (getSize(V->l) >= k) { // "V->val >= k" for lower_bound(k) + auto [left, right] = split(V->l, k); + V->l = right; + upd(v); + return {left, v}; + } else { + auto [left, right] = split(V->r, k - getSize(V->l) - 1); // and only "k" + V->r = left; + upd(v); + return {v, right}; + }} + + int merge(int left, int right) { + if (left < 0) return right; + if (right < 0) return left; + if (treap[left].prio < treap[right].prio) { + push(left); + treap[left].r = merge(treap[left].r, right); + upd(left); + return left; + } else { + push(right); + treap[right].l = merge(left, treap[right].l); + upd(right); + return right; + }} + + void insert(int i, ll val) { // and i = val + auto [left, right] = split(root, i); + treap.emplace_back(val); + left = merge(left, sz(treap) - 1); + root = merge(left, right); + } + + void remove(int i, int count = 1) { + auto [left, t_right] = split(root, i); + auto [middle, right] = split(t_right, count); + root = merge(left, right); + } + // for query use remove and read middle BEFORE remerging +}; diff --git a/tcr.pdf b/tcr.pdf index f25ba66..496b1da 100644 Binary files a/tcr.pdf and b/tcr.pdf differ -- cgit v1.2.3 From 63b53894683dc5a9d89ecc4ab45bb25faafd770a Mon Sep 17 00:00:00 2001 From: mzuenni Date: Sat, 4 Feb 2023 16:56:22 +0100 Subject: fix --- geometry/formulars.cpp | 6 +++--- geometry/linesAndSegments.cpp | 4 ++-- geometry/polygon.cpp | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/geometry/formulars.cpp b/geometry/formulars.cpp index 855ef1e..37cc4f4 100644 --- a/geometry/formulars.cpp +++ b/geometry/formulars.cpp @@ -21,9 +21,9 @@ double norm(pt a) {return dot(a, a);} double cross(pt a, pt b) {return imag(conj(a) * b);} double cross(pt p, pt a, pt b) {return cross(a - p, b - p);} -// -1 => gegen den Uhrzeigersinn -// 0 => kolliniear -// 1 => im Uhrzeigersinn. +// 1 => c links von a->b +// 0 => a, b und c kolliniear +// -1 => c rechts von a->b int orientation(pt a, pt b, pt c) { double orien = cross(b - a, c - a); return (orien > EPS) - (orien < -EPS); diff --git a/geometry/linesAndSegments.cpp b/geometry/linesAndSegments.cpp index 3a8ac02..4da35b1 100644 --- a/geometry/linesAndSegments.cpp +++ b/geometry/linesAndSegments.cpp @@ -39,7 +39,7 @@ double distToLine(pt a, pt b, pt p) { // Liegt p auf der Geraden a-b? 2d und 3d bool pointOnLine(pt a, pt b, pt p) { - return orientation(a, b, p) == 0; + return cross(a, b, p) == 0; } // Test auf Linienschnitt zwischen a-b und c-d. @@ -57,7 +57,7 @@ pt lineIntersection(pt p0, pt p1, pt p2, pt p3) { // Liegt p auf der Strecke a-b? bool pointOnLineSegment(pt a, pt b, pt p) { - if (orientation(a, b, p) != 0) return false; + if (cross(a, b, p) != 0) return false; ld dist = norm(a - b); return norm(a - p) <= dist && norm(b - p) <= dist; } diff --git a/geometry/polygon.cpp b/geometry/polygon.cpp index 23420a1..1620d7c 100644 --- a/geometry/polygon.cpp +++ b/geometry/polygon.cpp @@ -24,7 +24,7 @@ ll windingNumber(pt p, const vector& poly) { } // Testet, ob ein Punkt im Polygon liegt (beliebige Polygone). -// Ändere Zeile 31 falls rand zählt, poly[0] == poly.back() +// Ändere Zeile 32 falls rand zählt, poly[0] == poly.back() bool inside(pt p, const vector& poly) { bool in = false; for (int i = 0; i + 1 < sz(poly); i++) { @@ -39,7 +39,7 @@ bool inside(pt p, const vector& poly) { } // convex hull without duplicates, h[0] == h.back() -// Change line 43 and 49 >= if border counts as inside +// Change line 45 and 51 >= if border counts as inside bool inside(pt p, const vector& hull) { int l = 0, r = sz(hull) - 1; if (cross(hull[0], hull[r], p) > 0) return false; -- cgit v1.2.3 From 36b13e20dd5d734fb904ee3a7ba1a1ec50d6c8b1 Mon Sep 17 00:00:00 2001 From: Noobie99 Date: Sat, 4 Feb 2023 17:08:20 +0100 Subject: rename variable sz to size --- datastructures/treap2.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datastructures/treap2.cpp b/datastructures/treap2.cpp index e0c32a6..5d0aefa 100644 --- a/datastructures/treap2.cpp +++ b/datastructures/treap2.cpp @@ -2,7 +2,7 @@ mt19937 rng(0xc4bd5dad); struct Treap { struct Node { ll val; - int prio, sz = 1, l = -1, r = -1; + int prio, size = 1, l = -1, r = -1; Node (ll x) : val(x), prio(rng()) {} }; @@ -10,13 +10,13 @@ struct Treap { int root = -1; int getSize(int v) { - return v < 0 ? 0 : treap[v].sz; + return v < 0 ? 0 : treap[v].size; } void upd(int v) { if (v < 0) return; auto *V = &treap[v]; - V->sz = 1 + getSize(V->l) + getSize(V->r); + V->size = 1 + getSize(V->l) + getSize(V->r); // Update Node Code } -- cgit v1.2.3