From 0957a020c4f16983518aea3f59b304c40622e77e Mon Sep 17 00:00:00 2001 From: Horace He Date: Thu, 23 Apr 2020 13:01:39 -0700 Subject: [PATCH] Removed trav (and some other test/CI fixes) (#173) * checkpoint * another checkpoint * final checkpoint * converted tests and fixed some tests * fix some tests * updated descriptions * responded to comments * fix topo sort * added space before for * final spacing nits --- content/combinatorial/IntPerm.h | 2 +- content/contest/template.cpp | 1 - content/data-structures/FenwickTree2d.h | 2 +- content/geometry/3dHull.h | 2 +- content/geometry/ClosestPair.h | 2 +- content/geometry/ConvexHull.h | 4 +- content/geometry/DelaunayTriangulation.h | 4 +- content/geometry/ManhattanMST.h | 5 +- content/geometry/PolygonUnion.h | 5 +- content/geometry/PolyhedronVolume.h | 2 +- content/graph/2sat.h | 2 +- content/graph/BellmanFord.h | 4 +- content/graph/BiconnectedComponents.h | 2 +- content/graph/DFSMatching.h | 4 +- content/graph/Dinic.h | 2 +- content/graph/DirectedMST.h | 2 +- content/graph/EdmondsKarp.h | 2 +- content/graph/EulerWalk.h | 2 +- content/graph/GeneralMatching.h | 2 +- content/graph/GomoryHu.h | 2 +- content/graph/HLD.h | 4 +- content/graph/LCA.h | 2 +- content/graph/MaximumClique.h | 10 +- content/graph/MinCostMaxFlow.h | 6 +- content/graph/MinimumVertexCover.h | 4 +- content/graph/PushRelabel.h | 4 +- content/graph/SCC.h | 2 +- content/graph/TopoSort.h | 4 +- content/graph/hopcroftKarp.h | 8 +- content/number-theory/MillerRabin.h | 3 +- content/number-theory/phiFunction.h | 6 +- content/numerical/BerlekampMassey.h | 2 +- content/numerical/FastFourierTransform.h | 2 +- content/numerical/FastSubsetTransform.h | 2 +- content/numerical/Polynomial.h | 2 +- content/strings/AhoCorasick.h | 4 +- content/strings/Hashing-codeforces.h | 2 +- content/strings/Hashing.h | 2 +- doc/example.h | 2 +- doc/scripts/run-all.sh | 2 +- old-unit-tests/UnitTest.h | 2 +- old-unit-tests/UnitTestManager.cpp | 6 +- old-unit-tests/geometry/test_convexHull.cpp | 2 +- .../geometry/test_insidePolygon.cpp | 6 +- old-unit-tests/geometry/test_polygonCut.cpp | 2 +- old-unit-tests/global.h | 1 - .../data-structures/FenwickTree2d.cpp | 4 +- .../data-structures/LineContainer.cpp | 2 +- stress-tests/geometry/ConvexHull.cpp | 2 +- .../geometry/DelaunayTriangulation.cpp | 2 +- stress-tests/geometry/FastDelaunay.cpp | 2 +- .../geometry/LineHullIntersection.cpp | 2 +- .../geometry/MinimumEnclosingCircle.cpp | 4 +- stress-tests/geometry/PolygonCut.cpp | 2 +- stress-tests/geometry/PolygonUnion.cpp | 77 +++---- stress-tests/geometry/PolygonUnionArea.cpp | 211 ------------------ stress-tests/geometry/insidePolygon.cpp | 1 - stress-tests/graph/2sat.cpp | 4 +- stress-tests/graph/DirectedMST.cpp | 6 +- stress-tests/graph/EulerWalk.cpp | 14 +- stress-tests/graph/LCA.cpp | 2 +- stress-tests/graph/LinkCutTree.cpp | 2 +- stress-tests/graph/MaximalCliques.cpp | 2 +- stress-tests/graph/MinCostMaxFlow2.h | 2 +- stress-tests/graph/MinimumVertexCover.cpp | 12 +- stress-tests/graph/SCC.cpp | 12 +- stress-tests/graph/maxflow.cpp | 8 +- stress-tests/graph/oldHLD.h | 4 +- stress-tests/number-theory/Eratosthenes.cpp | 2 +- stress-tests/number-theory/MillerRabin.cpp | 2 +- stress-tests/numerical/BerlekampMassey.cpp | 2 +- .../numerical/FastFourierTransform.cpp | 4 +- .../numerical/FastFourierTransformMod.cpp | 6 +- .../numerical/FastSubsetTransform.cpp | 4 +- stress-tests/numerical/LinearRecurrence.cpp | 2 +- .../numerical/NumberTheoreticTransform.cpp | 10 +- stress-tests/numerical/SolveLinear.cpp | 2 +- stress-tests/strings/KMP.cpp | 2 +- stress-tests/strings/SuffixArray.cpp | 2 +- stress-tests/utilities/genPolygon.h | 6 +- stress-tests/utilities/template.h | 1 - stress-tests/various/IntervalContainer.cpp | 2 +- stress-tests/various/LIS.cpp | 4 +- 83 files changed, 172 insertions(+), 401 deletions(-) delete mode 100644 stress-tests/geometry/PolygonUnionArea.cpp diff --git a/content/combinatorial/IntPerm.h b/content/combinatorial/IntPerm.h index 009bea78d..43cfc9a87 100644 --- a/content/combinatorial/IntPerm.h +++ b/content/combinatorial/IntPerm.h @@ -10,7 +10,7 @@ int permToInt(vi& v) { int use = 0, i = 0, r = 0; - trav(x, v) r = r * ++i + __builtin_popcount(use & -(1 << x)), + for(int x:v) r = r * ++i + __builtin_popcount(use & -(1< hull3d(const vector& A) { C(a, b, c); C(a, c, b); C(b, c, a); } } - trav(it, FS) if ((A[it.b] - A[it.a]).cross( + for (F& it : FS) if ((A[it.b] - A[it.a]).cross( A[it.c] - A[it.a]).dot(it.q) <= 0) swap(it.c, it.b); return FS; }; diff --git a/content/geometry/ClosestPair.h b/content/geometry/ClosestPair.h index e092de39b..bd5adcb01 100644 --- a/content/geometry/ClosestPair.h +++ b/content/geometry/ClosestPair.h @@ -18,7 +18,7 @@ pair closest(vector

v) { sort(all(v), [](P a, P b) { return a.y < b.y; }); pair> ret{LLONG_MAX, {P(), P()}}; int j = 0; - trav(p, v) { + for (P p : v) { P d{1 + (ll)sqrt(ret.first), 0}; while (v[j].y <= p.y - d.x) S.erase(v[j++]); auto lo = S.lower_bound(p - d), hi = S.upper_bound(p + d); diff --git a/content/geometry/ConvexHull.h b/content/geometry/ConvexHull.h index 7c6d9252a..efd30925e 100644 --- a/content/geometry/ConvexHull.h +++ b/content/geometry/ConvexHull.h @@ -13,7 +13,7 @@ Points on the edge of the hull between two other points are not considered part \includegraphics[width=\textwidth]{content/geometry/ConvexHull} \vspace{-6mm} \end{minipage} - * Status: tested with Kattis problems convexhull + * Status: stress-tested, tested with Kattis problems convexhull * Time: O(n \log n) */ #pragma once @@ -27,7 +27,7 @@ vector

convexHull(vector

pts) { vector

h(sz(pts)+1); int s = 0, t = 0; for (int it = 2; it--; s = --t, reverse(all(pts))) - trav(p, pts) { + for (P p : pts) { while (t >= s + 2 && h[t-2].cross(h[t-1], p) <= 0) t--; h[t++] = p; } diff --git a/content/geometry/DelaunayTriangulation.h b/content/geometry/DelaunayTriangulation.h index 6e3a525dd..cf3a0aad0 100644 --- a/content/geometry/DelaunayTriangulation.h +++ b/content/geometry/DelaunayTriangulation.h @@ -18,8 +18,8 @@ void delaunay(vector

& ps, F trifun) { if (sz(ps) == 3) { int d = (ps[0].cross(ps[1], ps[2]) < 0); trifun(0,1+d,2-d); } vector p3; - trav(p, ps) p3.emplace_back(p.x, p.y, p.dist2()); - if (sz(ps) > 3) trav(t, hull3d(p3)) if ((p3[t.b]-p3[t.a]). + for (P p : ps) p3.emplace_back(p.x, p.y, p.dist2()); + if (sz(ps) > 3) for(auto t:hull3d(p3)) if ((p3[t.b]-p3[t.a]). cross(p3[t.c]-p3[t.a]).dot(P3(0,0,1)) < 0) trifun(t.a, t.c, t.b); } diff --git a/content/geometry/ManhattanMST.h b/content/geometry/ManhattanMST.h index 16d794178..6c54d6d2e 100644 --- a/content/geometry/ManhattanMST.h +++ b/content/geometry/ManhattanMST.h @@ -22,7 +22,7 @@ vector> manhattanMST(vector

ps) { sort(all(id), [&](int i, int j) { return (ps[i]-ps[j]).x < (ps[j]-ps[i]).y;}); map sweep; - trav(i,id) { + for (int i : id) { for (auto it = sweep.lower_bound(-ps[i].y); it != sweep.end(); sweep.erase(it++)) { int j = it->second; @@ -32,8 +32,7 @@ vector> manhattanMST(vector

ps) { } sweep[-ps[i].y] = i; } - if (k & 1) trav(p,ps) p.x = -p.x; - else trav(p,ps) swap(p.x, p.y); + for (P& p : ps) if (k & 1) p.x = -p.x; else swap(p.x, p.y); } return edges; } diff --git a/content/geometry/PolygonUnion.h b/content/geometry/PolygonUnion.h index c69e1b102..a0c45f857 100644 --- a/content/geometry/PolygonUnion.h +++ b/content/geometry/PolygonUnion.h @@ -6,9 +6,8 @@ * Description: Calculates the area of the union of $n$ polygons (not necessarily * convex). The points within each polygon must be given in CCW order. * (Epsilon checks may optionally be added to sideOf/sgn, but shouldn't be needed.) - * Status: Submitted on ECNA 2017 Problem A, fuzz-tested + * Status: stress-tested, Submitted on ECNA 2017 Problem A * Time: $O(N^2)$, where $N$ is the total number of points - * Usage: */ #pragma once @@ -37,7 +36,7 @@ double polyUnion(vector>& poly) { } } sort(all(segs)); - trav(s,segs) s.first = min(max(s.first, 0.0), 1.0); + for (auto& s : segs) s.first = min(max(s.first, 0.0), 1.0); double sum = 0; int cnt = segs[0].second; rep(j,1,sz(segs)) { diff --git a/content/geometry/PolyhedronVolume.h b/content/geometry/PolyhedronVolume.h index faf20b35d..6ced76c20 100644 --- a/content/geometry/PolyhedronVolume.h +++ b/content/geometry/PolyhedronVolume.h @@ -9,6 +9,6 @@ template double signedPolyVolume(const V& p, const L& trilist) { double v = 0; - trav(i, trilist) v += p[i.a].cross(p[i.b]).dot(p[i.c]); + for (auto i : trilist) v += p[i.a].cross(p[i.b]).dot(p[i.c]); return v / 6; } diff --git a/content/graph/2sat.h b/content/graph/2sat.h index 2b349860d..a8aa97c83 100644 --- a/content/graph/2sat.h +++ b/content/graph/2sat.h @@ -54,7 +54,7 @@ struct TwoSat { vi val, comp, z; int time = 0; int dfs(int i) { int low = val[i] = ++time, x; z.push_back(i); - trav(e, gr[i]) if (!comp[e]) + for(int e : gr[i]) if (!comp[e]) low = min(low, val[e] ?: dfs(e)); if (low == val[i]) do { x = z.back(); z.pop_back(); diff --git a/content/graph/BellmanFord.h b/content/graph/BellmanFord.h index ddbae64bb..4daae31a8 100644 --- a/content/graph/BellmanFord.h +++ b/content/graph/BellmanFord.h @@ -20,7 +20,7 @@ void bellmanFord(vector& nodes, vector& eds, int s) { sort(all(eds), [](Ed a, Ed b) { return a.s() < b.s(); }); int lim = sz(nodes) / 2 + 2; // /3+100 with shuffled vertices - rep(i,0,lim) trav(ed, eds) { + rep(i,0,lim) for (Ed ed : eds) { Node cur = nodes[ed.a], &dest = nodes[ed.b]; if (abs(cur.dist) == inf) continue; ll d = cur.dist + ed.w; @@ -29,7 +29,7 @@ void bellmanFord(vector& nodes, vector& eds, int s) { dest.dist = (i < lim-1 ? d : -inf); } } - rep(i,0,lim) trav(e, eds) { + rep(i,0,lim) for (Ed e : eds) { if (nodes[e.a].dist == -inf) nodes[e.b].dist = -inf; } diff --git a/content/graph/BiconnectedComponents.h b/content/graph/BiconnectedComponents.h index 68d9e1277..f0c4a9ac5 100644 --- a/content/graph/BiconnectedComponents.h +++ b/content/graph/BiconnectedComponents.h @@ -25,7 +25,7 @@ int Time; template int dfs(int at, int par, F& f) { int me = num[at] = ++Time, e, y, top = me; - trav(pa, ed[at]) if (pa.second != par) { + for (auto pa : ed[at]) if (pa.second != par) { tie(y, e) = pa; if (num[y]) { top = min(top, num[y]); diff --git a/content/graph/DFSMatching.h b/content/graph/DFSMatching.h index e002269f7..8abf0094a 100644 --- a/content/graph/DFSMatching.h +++ b/content/graph/DFSMatching.h @@ -17,7 +17,7 @@ bool find(int j, vector& g, vi& btoa, vi& vis) { if (btoa[j] == -1) return 1; vis[j] = 1; int di = btoa[j]; - trav(e, g[di]) + for (int e : g[di]) if (!vis[e] && find(e, g, btoa, vis)) { btoa[e] = di; return 1; @@ -28,7 +28,7 @@ int dfsMatching(vector& g, vi& btoa) { vi vis; rep(i,0,sz(g)) { vis.assign(sz(btoa), 0); - trav(j,g[i]) + for (int j : g[i]) if (find(j, g, btoa, vis)) { btoa[j] = i; break; diff --git a/content/graph/Dinic.h b/content/graph/Dinic.h index a983ddeaa..b0023273e 100644 --- a/content/graph/Dinic.h +++ b/content/graph/Dinic.h @@ -41,7 +41,7 @@ struct Dinic { int qi = 0, qe = lvl[s] = 1; while (qi < qe && !lvl[t]) { int v = q[qi++]; - trav(e, adj[v]) + for (Edge e : adj[v]) if (!lvl[e.to] && e.c >> (30 - L)) q[qe++] = e.to, lvl[e.to] = lvl[v] + 1; } diff --git a/content/graph/DirectedMST.h b/content/graph/DirectedMST.h index aef78ab82..71e9d5aca 100644 --- a/content/graph/DirectedMST.h +++ b/content/graph/DirectedMST.h @@ -37,7 +37,7 @@ void pop(Node*& a) { a->prop(); a = merge(a->l, a->r); } ll dmst(int n, int r, vector& g) { UF uf(n); vector heap(n); - trav(e, g) heap[e.b] = merge(heap[e.b], new Node{e}); + for (Edge e : g) heap[e.b] = merge(heap[e.b], new Node{e}); ll res = 0; vi seen(n, -1), path(n); seen[r] = r; diff --git a/content/graph/EdmondsKarp.h b/content/graph/EdmondsKarp.h index 754c25dd3..317000228 100644 --- a/content/graph/EdmondsKarp.h +++ b/content/graph/EdmondsKarp.h @@ -22,7 +22,7 @@ template T edmondsKarp(vector>& graph, int source rep(i,0,ptr) { int x = q[i]; - trav(e, graph[x]) { + for (auto e : graph[x]) { if (par[e.first] == -1 && e.second > 0) { par[e.first] = x; q[ptr++] = e.first; diff --git a/content/graph/EulerWalk.h b/content/graph/EulerWalk.h index 9ae07517c..213132499 100644 --- a/content/graph/EulerWalk.h +++ b/content/graph/EulerWalk.h @@ -26,6 +26,6 @@ vi eulerWalk(vector>& gr, int nedges, int src=0) { D[x]--, D[y]++; eu[e] = 1; s.push_back(y); }} - trav(x, D) if (x < 0 || sz(ret) != nedges+1) return {}; + for (int x : D) if (x < 0 || sz(ret) != nedges+1) return {}; return {ret.rbegin(), ret.rend()}; } diff --git a/content/graph/GeneralMatching.h b/content/graph/GeneralMatching.h index 460d9f771..aa2701aef 100644 --- a/content/graph/GeneralMatching.h +++ b/content/graph/GeneralMatching.h @@ -14,7 +14,7 @@ vector generalMatching(int N, vector& ed) { vector> mat(N, vector(N)), A; - trav(pa, ed) { + for (pii pa : ed) { int a = pa.first, b = pa.second, r = rand() % mod; mat[a][b] = r, mat[b][a] = (mod - r) % mod; } diff --git a/content/graph/GomoryHu.h b/content/graph/GomoryHu.h index da1a2c376..2ca3c8bb6 100644 --- a/content/graph/GomoryHu.h +++ b/content/graph/GomoryHu.h @@ -24,7 +24,7 @@ vector gomoryHu(int N, vector ed) { vi par(N); rep(i,1,N) { PushRelabel D(N); // Dinic also works - trav(t,ed) D.addEdge(t[0], t[1], t[2], t[2]); + for (Edge t : ed) D.addEdge(t[0], t[1], t[2], t[2]); tree.push_back({i, par[i], D.calc(i, par[i])}); rep(j,i+1,N) if (par[j] == par[i] && D.leftOfMinCut(j)) par[j] = i; diff --git a/content/graph/HLD.h b/content/graph/HLD.h index 8f9febebe..f4aafa884 100644 --- a/content/graph/HLD.h +++ b/content/graph/HLD.h @@ -27,7 +27,7 @@ template struct HLD { rt(N),pos(N),tree(new Node(0, N)){ dfsSz(),dfsHld();} void dfsSz(int v = 0) { if (par[v] != -1) adj[v].erase(find(all(adj[v]), par[v])); - trav(u, adj[v]) { + for (int& u : adj[v]) { par[u] = v, depth[u] = depth[v] + 1; dfsSz(u); siz[v] += siz[u]; @@ -36,7 +36,7 @@ template struct HLD { } void dfsHld(int v = 0) { pos[v] = tim++; - trav(u, adj[v]) { + for (int u : adj[v]) { rt[u] = (u == adj[v][0] ? rt[v] : u); dfsHld(u); } diff --git a/content/graph/LCA.h b/content/graph/LCA.h index 3fe2ef31d..a84fa6b78 100644 --- a/content/graph/LCA.h +++ b/content/graph/LCA.h @@ -21,7 +21,7 @@ struct LCA { LCA(vector& C) : time(sz(C)), rmq((dfs(C,0,-1), ret)) {} void dfs(vector& C, int v, int par) { time[v] = T++; - trav(y, C[v]) if (y != par) { + for (int y : C[v]) if (y != par) { path.push_back(v), ret.push_back(time[v]); dfs(C, y, v); } diff --git a/content/graph/MaximumClique.h b/content/graph/MaximumClique.h index 70403ad76..a579387f5 100644 --- a/content/graph/MaximumClique.h +++ b/content/graph/MaximumClique.h @@ -20,8 +20,8 @@ struct Maxclique { vector C; vi qmax, q, S, old; void init(vv& r) { - trav(v,r) v.d = 0; - trav(v, r) trav(j, r) v.d += e[v.i][j.i]; + for (auto& v : r) v.d = 0; + for (auto& v : r) for (auto j : r) v.d += e[v.i][j.i]; sort(all(r), [](auto a, auto b) { return a.d > b.d; }); int mxD = r[0].d; rep(i,0,sz(r)) r[i].d = min(i, mxD) + 1; @@ -33,12 +33,12 @@ struct Maxclique { if (sz(q) + R.back().d <= sz(qmax)) return; q.push_back(R.back().i); vv T; - trav(v,R) if (e[R.back().i][v.i]) T.push_back({v.i}); + for(auto v:R) if (e[R.back().i][v.i]) T.push_back({v.i}); if (sz(T)) { if (S[lev]++ / ++pk < limit) init(T); int j = 0, mxk = 1, mnk = max(sz(qmax) - sz(q) + 1, 1); C[1].clear(), C[2].clear(); - trav(v, T) { + for (auto v : T) { int k = 1; auto f = [&](int i) { return e[v.i][i]; }; while (any_of(all(C[k]), f)) k++; @@ -47,7 +47,7 @@ struct Maxclique { C[k].push_back(v.i); } if (j > 0) T[j - 1].d = 0; - rep(k,mnk,mxk + 1) trav(i, C[k]) + rep(k,mnk,mxk + 1) for (int i : C[k]) T[j].i = i, T[j++].d = k; expand(T, lev + 1); } else if (sz(q) > sz(qmax)) qmax = q; diff --git a/content/graph/MinCostMaxFlow.h b/content/graph/MinCostMaxFlow.h index 5234903e8..4bfdb2a12 100644 --- a/content/graph/MinCostMaxFlow.h +++ b/content/graph/MinCostMaxFlow.h @@ -56,9 +56,9 @@ struct MCMF { while (!q.empty()) { s = q.top().second; q.pop(); seen[s] = 1; di = dist[s] + pi[s]; - trav(i, ed[s]) if (!seen[i]) + for (int i : ed[s]) if (!seen[i]) relax(i, cap[s][i] - flow[s][i], cost[s][i], 1); - trav(i, red[s]) if (!seen[i]) + for (int i : red[s]) if (!seen[i]) relax(i, flow[i][s], -cost[i][s], 0); } rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF); @@ -85,7 +85,7 @@ struct MCMF { int it = N, ch = 1; ll v; while (ch-- && it--) rep(i,0,N) if (pi[i] != INF) - trav(to, ed[i]) if (cap[i][to]) + for (int to : ed[i]) if (cap[i][to]) if ((v = pi[i] + cost[i][to]) < pi[to]) pi[to] = v, ch = 1; assert(it >= 0); // negative cost cycle diff --git a/content/graph/MinimumVertexCover.h b/content/graph/MinimumVertexCover.h index 08d8d1fe0..05694fe4b 100644 --- a/content/graph/MinimumVertexCover.h +++ b/content/graph/MinimumVertexCover.h @@ -15,13 +15,13 @@ vi cover(vector& g, int n, int m) { vi match(m, -1); int res = dfsMatching(g, match); vector lfound(n, true), seen(m); - trav(it, match) if (it != -1) lfound[it] = false; + for (int it : match) if (it != -1) lfound[it] = false; vi q, cover; rep(i,0,n) if (lfound[i]) q.push_back(i); while (!q.empty()) { int i = q.back(); q.pop_back(); lfound[i] = 1; - trav(e, g[i]) if (!seen[e] && match[e] != -1) { + for (int e : g[i]) if (!seen[e] && match[e] != -1) { seen[e] = true; q.push_back(match[e]); } diff --git a/content/graph/PushRelabel.h b/content/graph/PushRelabel.h index d3b50e212..5528cbb36 100644 --- a/content/graph/PushRelabel.h +++ b/content/graph/PushRelabel.h @@ -38,7 +38,7 @@ struct PushRelabel { int v = sz(g); H[s] = v; ec[t] = 1; vi co(2*v); co[0] = v-1; rep(i,0,v) cur[i] = g[i].data(); - trav(e, g[s]) addFlow(e, e.c); + for (Edge& e : g[s]) addFlow(e, e.c); for (int hi = 0;;) { while (hs[hi].empty()) if (!hi--) return -ec[s]; @@ -46,7 +46,7 @@ struct PushRelabel { while (ec[u] > 0) // discharge u if (cur[u] == g[u].data() + sz(g[u])) { H[u] = 1e9; - trav(e, g[u]) if (e.c && H[u] > H[e.dest]+1) + for (Edge& e : g[u]) if (e.c && H[u] > H[e.dest]+1) H[u] = H[e.dest]+1, cur[u] = &e; if (++co[H[u]], !--co[hi] && hi < v) rep(i,0,v) if (hi < H[i] && H[i] < v) diff --git a/content/graph/SCC.h b/content/graph/SCC.h index 1897f4cb3..7e72f04ee 100644 --- a/content/graph/SCC.h +++ b/content/graph/SCC.h @@ -19,7 +19,7 @@ vi val, comp, z, cont; int Time, ncomps; template int dfs(int j, G& g, F& f) { int low = val[j] = ++Time, x; z.push_back(j); - trav(e,g[j]) if (comp[e] < 0) + for (auto e : g[j]) if (comp[e] < 0) low = min(low, val[e] ?: dfs(e,g,f)); if (low == val[j]) { diff --git a/content/graph/TopoSort.h b/content/graph/TopoSort.h index bdc545435..0cdf397fd 100644 --- a/content/graph/TopoSort.h +++ b/content/graph/TopoSort.h @@ -12,14 +12,14 @@ vi topoSort(const vector& gr) { vi indeg(sz(gr)), ret; - trav(li, gr) trav(x, li) indeg[x]++; + for (auto& li : gr) for (int x : li) indeg[x]++; queue q; // use priority queue for lexic. smallest ans. rep(i,0,sz(gr)) if (indeg[i] == 0) q.push(-i); while (!q.empty()) { int i = -q.front(); // top() for priority queue ret.push_back(i); q.pop(); - trav(x, gr[i]) + for (int x : gr[i]) if (--indeg[x] == 0) q.push(-x); } return ret; diff --git a/content/graph/hopcroftKarp.h b/content/graph/hopcroftKarp.h index a56afee68..83c9847a9 100644 --- a/content/graph/hopcroftKarp.h +++ b/content/graph/hopcroftKarp.h @@ -17,7 +17,7 @@ bool dfs(int a, int L, vector& g, vi& btoa, vi& A, vi& B) { if (A[a] != L) return 0; A[a] = -1; - trav(b, g[a]) if (B[b] == L + 1) { + for (int b : g[a]) if (B[b] == L + 1) { B[b] = 0; if (btoa[b] == -1 || dfs(btoa[b], L + 1, g, btoa, A, B)) return btoa[b] = a, 1; @@ -33,13 +33,13 @@ int hopcroftKarp(vector& g, vi& btoa) { fill(all(B), 0); /// Find the starting nodes for BFS (i.e. layer 0). cur.clear(); - trav(a, btoa) if(a != -1) A[a] = -1; + for (int a : btoa) if(a != -1) A[a] = -1; rep(a,0,sz(g)) if(A[a] == 0) cur.push_back(a); /// Find all layers using bfs. for (int lay = 1;; lay++) { bool islast = 0; next.clear(); - trav(a, cur) trav(b, g[a]) { + for (int a : cur) for (int b : g[a]) { if (btoa[b] == -1) { B[b] = lay; islast = 1; @@ -51,7 +51,7 @@ int hopcroftKarp(vector& g, vi& btoa) { } if (islast) break; if (next.empty()) return res; - trav(a, next) A[a] = lay; + for (int a : next) A[a] = lay; cur.swap(next); } /// Use DFS to scan for augmenting paths. diff --git a/content/number-theory/MillerRabin.h b/content/number-theory/MillerRabin.h index 8a3aff0b5..7f6ce5248 100644 --- a/content/number-theory/MillerRabin.h +++ b/content/number-theory/MillerRabin.h @@ -6,6 +6,7 @@ * Description: Deterministic Miller-Rabin primality test. * Guaranteed to work for numbers up to $2^{63}$; for larger numbers, extend A randomly. * Time: 7 times the complexity of $a^b \mod c$. + * Status: Stress-tested */ #pragma once @@ -15,7 +16,7 @@ bool isPrime(ull n) { if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3; ull A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}, s = __builtin_ctzll(n-1), d = n >> s; - trav(a, A) { // ^ count trailing zeroes + for (ull a : A) { // ^ count trailing zeroes ull p = modpow(a%n, d, n), i = s; while (p != 1 && p != n - 1 && a % n && i--) p = modmul(p, p, n); diff --git a/content/number-theory/phiFunction.h b/content/number-theory/phiFunction.h index 7724c3e81..b30ef7eec 100644 --- a/content/number-theory/phiFunction.h +++ b/content/number-theory/phiFunction.h @@ -9,7 +9,7 @@ * $\phi(n)=n \cdot \prod_{p|n}(1-1/p)$. * * $\sum_{d|n} \phi(d) = n$, $\sum_{1\leq k \leq n, \gcd(k,n)=1} k = n \phi(n)/2, n>1$ - * + * * \textbf{Euler's thm}: $a,n$ coprime $\Rightarrow a^{\phi(n)} \equiv 1 \pmod{n}$. * * \textbf{Fermat's little thm}: $p$ prime $\Rightarrow a^{p-1} \equiv 1 \pmod{p}$ $\forall a$. @@ -22,6 +22,6 @@ int phi[LIM]; void calculatePhi() { rep(i,0,LIM) phi[i] = i&1 ? i : i/2; - for(int i = 3; i < LIM; i += 2) if(phi[i] == i) - for(int j = i; j < LIM; j += i) phi[j] -= phi[j] / i; + for (int i = 3; i < LIM; i += 2) if(phi[i] == i) + for (int j = i; j < LIM; j += i) phi[j] -= phi[j] / i; } diff --git a/content/numerical/BerlekampMassey.h b/content/numerical/BerlekampMassey.h index 5ee1a0efa..96b018d49 100644 --- a/content/numerical/BerlekampMassey.h +++ b/content/numerical/BerlekampMassey.h @@ -33,6 +33,6 @@ vector berlekampMassey(vector s) { } C.resize(L + 1); C.erase(C.begin()); - trav(x, C) x = (mod - x) % mod; + for (ll& x : C) x = (mod - x) % mod; return C; } diff --git a/content/numerical/FastFourierTransform.h b/content/numerical/FastFourierTransform.h index b8b18fb26..7da5b5108 100644 --- a/content/numerical/FastFourierTransform.h +++ b/content/numerical/FastFourierTransform.h @@ -47,7 +47,7 @@ vd conv(const vd& a, const vd& b) { copy(all(a), begin(in)); rep(i,0,sz(b)) in[i].imag(b[i]); fft(in); - trav(x, in) x *= x; + for (C& x : in) x *= x; rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]); fft(out); rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n); diff --git a/content/numerical/FastSubsetTransform.h b/content/numerical/FastSubsetTransform.h index a748562d1..2be5d1e9c 100644 --- a/content/numerical/FastSubsetTransform.h +++ b/content/numerical/FastSubsetTransform.h @@ -20,7 +20,7 @@ void FST(vi& a, bool inv) { // pii(u + v, u - v); // XOR /// include-line } } - // if (inv) trav(x, a) x /= sz(a); // XOR only /// include-line + // if (inv) for (int& x : a) x /= sz(a); // XOR only /// include-line } vi conv(vi a, vi b) { FST(a, 0); FST(b, 0); diff --git a/content/numerical/Polynomial.h b/content/numerical/Polynomial.h index f0569eb65..714dec7f9 100644 --- a/content/numerical/Polynomial.h +++ b/content/numerical/Polynomial.h @@ -9,7 +9,7 @@ struct Poly { vector a; double operator()(double x) const { double val = 0; - for(int i = sz(a); i--;) (val *= x) += a[i]; + for (int i = sz(a); i--;) (val *= x) += a[i]; return val; } void diff() { diff --git a/content/strings/AhoCorasick.h b/content/strings/AhoCorasick.h index d7567079d..82126939e 100644 --- a/content/strings/AhoCorasick.h +++ b/content/strings/AhoCorasick.h @@ -27,7 +27,7 @@ struct AhoCorasick { void insert(string& s, int j) { assert(!s.empty()); int n = 0; - trav(c, s) { + for (char c : s) { int& m = N[n].next[c - first]; if (m == -1) { n = m = sz(N); N.emplace_back(-1); } else n = m; @@ -62,7 +62,7 @@ struct AhoCorasick { vi find(string word) { int n = 0; vi res; // ll count = 0; - trav(c, word) { + for (char c : word) { n = N[n].next[c - first]; res.push_back(N[n].end); // count += N[n].nmatches; diff --git a/content/strings/Hashing-codeforces.h b/content/strings/Hashing-codeforces.h index 319dcd8f3..b8b4f1421 100644 --- a/content/strings/Hashing-codeforces.h +++ b/content/strings/Hashing-codeforces.h @@ -50,7 +50,7 @@ vector getHashes(string& str, int length) { return ret; } -H hashString(string& s) { H h{}; trav(c,s) h=h*C+c; return h; } +H hashString(string& s){H h{}; for(char c:s) h=h*C+c;return h;} #include int main() { diff --git a/content/strings/Hashing.h b/content/strings/Hashing.h index f947f0271..c91746d06 100644 --- a/content/strings/Hashing.h +++ b/content/strings/Hashing.h @@ -51,4 +51,4 @@ vector getHashes(string& str, int length) { return ret; } -H hashString(string& s) { H h{}; trav(c,s) h=h*C+c; return h; } +H hashString(string& s){H h{}; for(char c:s) h=h*C+c;return h;} diff --git a/doc/example.h b/doc/example.h index aaba09ade..02fa1f071 100644 --- a/doc/example.h +++ b/doc/example.h @@ -30,7 +30,7 @@ int exampleFunction1(vi mice, int* cats, int catsCount) { rep(i, 1, catsCount) { // This comment will be kept! - trav(it, mice) { + for (auto it : mice) { /// This will be removed. // And this kept. sum += cats[i] * it; diff --git a/doc/scripts/run-all.sh b/doc/scripts/run-all.sh index 312d52e9b..789cc23fa 100755 --- a/doc/scripts/run-all.sh +++ b/doc/scripts/run-all.sh @@ -9,7 +9,6 @@ for test in $tests; do echo "$(basename $test): " start=`date +%s.%N` g++ -std=c++14 -O2 $test && ./a.out - end=`date +%s.%N` retCode=$? if (($retCode != 0)); then echo "Failed with $retCode" @@ -18,6 +17,7 @@ for test in $tests; do else pass+=1 fi + end=`date +%s.%N` runtime=$( echo "$end - $start" | bc -l ) echo "Took $runtime seconds" rm -f a.out diff --git a/old-unit-tests/UnitTest.h b/old-unit-tests/UnitTest.h index 684221111..5c1b9ecaa 100644 --- a/old-unit-tests/UnitTest.h +++ b/old-unit-tests/UnitTest.h @@ -70,7 +70,7 @@ class UnitTest ostringstream oss; oss << "{ "; - trav(it, data) + for(auto &it: data) oss << convertToString(it) << " "; oss << "}"; diff --git a/old-unit-tests/UnitTestManager.cpp b/old-unit-tests/UnitTestManager.cpp index 90188e97f..78afc174a 100644 --- a/old-unit-tests/UnitTestManager.cpp +++ b/old-unit-tests/UnitTestManager.cpp @@ -14,7 +14,7 @@ UnitTestManager::UnitTestManager(): UnitTestManager::~UnitTestManager() { - trav(it, m_unitTestWrappers) + for(auto &it: m_unitTestWrappers) delete it.second; m_unitTestWrappers.clear(); @@ -58,7 +58,7 @@ void UnitTestManager::registerWrapper(UnitTestWrapper* unitTestWrapper) void UnitTestManager::runAll() { - trav(it, m_unitTestWrappers) + for(auto &it: m_unitTestWrappers) runTest(it.second); } @@ -97,7 +97,7 @@ void UnitTestManager::runTest(UnitTestWrapper* unitTestWrapper) fprintf(stderr, "Running test \"%s\" <%d, %d>... ", name.c_str(), i, unitTest->getCount()); fflush(stderr); - + try { unitTest->run(i); diff --git a/old-unit-tests/geometry/test_convexHull.cpp b/old-unit-tests/geometry/test_convexHull.cpp index 87debc5e5..2babc9b52 100644 --- a/old-unit-tests/geometry/test_convexHull.cpp +++ b/old-unit-tests/geometry/test_convexHull.cpp @@ -7,7 +7,7 @@ typedef Point P; template ostream & operator<<(ostream & os, const vector p) { os << "[ "; - trav(it,p) os << it << " "; + for(auto &it:p) os << it << " "; os << "]"; return os; } diff --git a/old-unit-tests/geometry/test_insidePolygon.cpp b/old-unit-tests/geometry/test_insidePolygon.cpp index b66457554..6c61b93ed 100644 --- a/old-unit-tests/geometry/test_insidePolygon.cpp +++ b/old-unit-tests/geometry/test_insidePolygon.cpp @@ -55,15 +55,15 @@ class test_insidePolygon : stringstream ss; ss << strings[c] << " : " << points[c]; check(a, (bool)answers[c], ss.str()); - } else { + } else { vector > poly(2+rand()%20); - trav(i,poly) i = Point(rand()%100000,rand()%100000); + for(auto &i:poly) i = Point(rand()%100000,rand()%100000); Point p(rand()%100000,rand()%100000); bool a = insidePolygon(poly.begin(),poly.end(),p,true); for (int i = 0; i < 10; ++i) { double alpha = rand()%1000/1000.0; Point D(rand()%1000,rand()%1000); - trav(i,poly) i = i.rotate(alpha)+D; + for(auto &i:poly) i = i.rotate(alpha)+D; p = p.rotate(alpha)+D; check(insidePolygon(poly.begin(),poly.end(),p,true),a); } diff --git a/old-unit-tests/geometry/test_polygonCut.cpp b/old-unit-tests/geometry/test_polygonCut.cpp index bb534a9c4..159d751d5 100644 --- a/old-unit-tests/geometry/test_polygonCut.cpp +++ b/old-unit-tests/geometry/test_polygonCut.cpp @@ -15,7 +15,7 @@ class test_polygonCut : string str(vector

p, int n) { stringstream ss; ss << n << " : "; - trav(i,p) ss << i; + for(auto &i:p) ss << i; return ss.str(); } diff --git a/old-unit-tests/global.h b/old-unit-tests/global.h index bc4aa163b..b754442bc 100644 --- a/old-unit-tests/global.h +++ b/old-unit-tests/global.h @@ -7,7 +7,6 @@ using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) -#define trav(it, v) for(auto& it : v) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() diff --git a/stress-tests/data-structures/FenwickTree2d.cpp b/stress-tests/data-structures/FenwickTree2d.cpp index a2d19e033..2dd6552ba 100644 --- a/stress-tests/data-structures/FenwickTree2d.cpp +++ b/stress-tests/data-structures/FenwickTree2d.cpp @@ -12,10 +12,10 @@ int main() { } vector grid(12, vi(12)), sumto(13, vi(13)); - trav(pa, upd) + for(auto &pa: upd) ft.fakeUpdate(get<0>(pa), get<1>(pa)); ft.init(); - trav(pa, upd) { + for(auto &pa: upd) { grid[get<0>(pa)][get<1>(pa)] += get<2>(pa); ft.update(get<0>(pa), get<1>(pa), get<2>(pa)); } diff --git a/stress-tests/data-structures/LineContainer.cpp b/stress-tests/data-structures/LineContainer.cpp index 018fe8687..1199349a6 100644 --- a/stress-tests/data-structures/LineContainer.cpp +++ b/stress-tests/data-structures/LineContainer.cpp @@ -59,7 +59,7 @@ int test2() { rep(j,0,K) v[j] = max(v[j], k*x[j] + m); } // cerr << mh.eval(x) << ' ' << v << endl; -// trav(li, mh) cerr << li.k << ' ' << li.m << ' ' << li.p << endl; +// for(auto &li: mh) cerr << li.k << ' ' << li.m << ' ' << li.p << endl; rep(j,0,K) assert(mh.query(x[j]) == v[j]); } diff --git a/stress-tests/geometry/ConvexHull.cpp b/stress-tests/geometry/ConvexHull.cpp index 782a78137..3d94b6824 100644 --- a/stress-tests/geometry/ConvexHull.cpp +++ b/stress-tests/geometry/ConvexHull.cpp @@ -8,7 +8,7 @@ pair ulHull(const vector

& S) { vi Q(sz(S)), U, L; iota(all(Q), 0); sort(all(Q), [&S](int a, int b){ return S[a] < S[b]; }); - trav(it, Q) { + for(auto &it: Q) { #define ADDP(C, cmp) while (sz(C) > 1 && S[C[sz(C)-2]].cross(\ S[it], S[C.back()]) cmp 0) C.pop_back(); C.push_back(it); ADDP(U, <=); ADDP(L, >=); diff --git a/stress-tests/geometry/DelaunayTriangulation.cpp b/stress-tests/geometry/DelaunayTriangulation.cpp index e73f959d9..cd72a1a2f 100644 --- a/stress-tests/geometry/DelaunayTriangulation.cpp +++ b/stress-tests/geometry/DelaunayTriangulation.cpp @@ -37,7 +37,7 @@ int main() { auto fail = [&]() { cout << "Points:" << endl; - trav(p, ps) { + for(auto &p: ps) { cout << p.x << ' ' << p.y << endl; } diff --git a/stress-tests/geometry/FastDelaunay.cpp b/stress-tests/geometry/FastDelaunay.cpp index 782cf77d0..e31f0466f 100644 --- a/stress-tests/geometry/FastDelaunay.cpp +++ b/stress-tests/geometry/FastDelaunay.cpp @@ -93,7 +93,7 @@ int main1() { auto fail = [&]() { cout << "Points:" << endl; - trav(p, ps) { + for(auto &p: ps) { cout << p.x << ' ' << p.y << endl; } diff --git a/stress-tests/geometry/LineHullIntersection.cpp b/stress-tests/geometry/LineHullIntersection.cpp index d80f61eaa..a7e8d0ed2 100644 --- a/stress-tests/geometry/LineHullIntersection.cpp +++ b/stress-tests/geometry/LineHullIntersection.cpp @@ -87,7 +87,7 @@ int main() { auto fail = [&](int line) { cerr << sz(ps) << endl; - trav(p, ps) cout << p<<' '; + for(auto &p: ps) cout << p<<' '; cout< " << r.first << ' ' << r.second << endl; diff --git a/stress-tests/geometry/MinimumEnclosingCircle.cpp b/stress-tests/geometry/MinimumEnclosingCircle.cpp index a85cf066a..9cab53b6f 100644 --- a/stress-tests/geometry/MinimumEnclosingCircle.cpp +++ b/stress-tests/geometry/MinimumEnclosingCircle.cpp @@ -16,7 +16,7 @@ int main() { P mid = pa.first; double rad = pa.second; double maxDist = 0; - trav(p, ps) { + for(auto &p: ps) { maxDist = max(maxDist, (p - mid).dist()); } @@ -24,7 +24,7 @@ int main() { rep(it2,0,50) { P q2 = mid - P(0, 1e-6).rotate(it2); - trav(p, ps) { + for(auto &p: ps) { if((p - q2).dist() > rad - 1e-7) goto fail; } assert(0); diff --git a/stress-tests/geometry/PolygonCut.cpp b/stress-tests/geometry/PolygonCut.cpp index 95d895d48..7d5b37dd9 100644 --- a/stress-tests/geometry/PolygonCut.cpp +++ b/stress-tests/geometry/PolygonCut.cpp @@ -45,7 +45,7 @@ int main() { // cout << N << ' ' << realArea << '\t' << approxArea << '\t' << realArea - approxArea << endl; // cerr << N << endl; - // trav(x, ps) { + // for(auto &x: ps) { // cout << x.x << ' ' << x.y << endl; // } } diff --git a/stress-tests/geometry/PolygonUnion.cpp b/stress-tests/geometry/PolygonUnion.cpp index d4ace841a..23205b75c 100644 --- a/stress-tests/geometry/PolygonUnion.cpp +++ b/stress-tests/geometry/PolygonUnion.cpp @@ -5,7 +5,6 @@ typedef long long ll; using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) -#define trav(a, x) for (auto &a : x) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; @@ -87,8 +86,8 @@ namespace approximate { double polygonUnion(vector> &polygons, int lim) { int cnt = 0; int total = 0; - for (double y = -lim + 1e-5; y < lim; y += lim / 200.0) { - for (double x = -lim + 1.1e-5; x < lim; x += lim / 200.0) { + for (double y = -lim + 1e-5; y < lim; y += lim / 500.0) { + for (double x = -lim + 1.1e-5; x < lim; x += lim / 500.0) { total++; for (auto &i : polygons) { if (inPolygon(i, P(x, y))) { @@ -163,19 +162,13 @@ db polygon_union(vector py[], int n) { } } // namespace lovelive -P randPt(int lim) { - return P(randRange(-lim, lim), randRange(-lim, lim)); -} +P randPt(int lim) { return P(randRange(-lim, lim), randRange(-lim, lim)); } -P rndUlp(int lim, long long ulps = 5) { - return P(randNearIntUlps(lim, ulps), randNearIntUlps(lim, ulps)); -} +P rndUlp(int lim, long long ulps = 5) { return P(randNearIntUlps(lim, ulps), randNearIntUlps(lim, ulps)); } -P rndEps(int lim, double eps) { - return P(randNearIntEps(lim, eps), randNearIntEps(lim, eps)); -} +P rndEps(int lim, double eps) { return P(randNearIntEps(lim, eps), randNearIntEps(lim, eps)); } -void testRandom(int n, int numPts = 10, int lim = 5) { +void testRandom(int n, int numPts = 10, int lim = 5, bool brute = false) { vector> polygons; for (int i = 0; i < n; i++) { vector

pts; @@ -189,53 +182,45 @@ void testRandom(int n, int numPts = 10, int lim = 5) { } } auto val1 = polyUnion(polygons); - auto val2 = approximate::polygonUnion(polygons, lim); - if (abs(val1 - val2) / max(val1, val2) > 0.1) { - vector> polygons2; - for (auto i : polygons) { - vector t; - for (auto j : i) - t.push_back({j.x, j.y}); - polygons2.push_back(t); - } - vector> polygons3; - for (auto i : polygons) { - vector t; - for (auto j : i) - t.push_back({j.x, j.y}); - polygons3.push_back(t); - } - auto val3 = blackhorse::polygon_union(polygons2.data(), sz(polygons2)); - auto val4 = lovelive::polygon_union(polygons3.data(), sz(polygons3)); - - cout << endl; - cout << val3 <<' ' << val4 << endl; - cout << val1 << ' ' << val2 << ' ' << endl; - if (abs(val1 - val2) > 1e-8) { - rep(i,0,n) { - trav(x, polygons[i]) { - cout << x << ' '; - } - cout << endl; + vector> polygons2; + for (auto i : polygons) { + vector t; + for (auto j : i) + t.push_back({j.x, j.y}); + polygons2.push_back(t); + } + vector> polygons3; + for (auto i : polygons) { + vector t; + for (auto j : i) + t.push_back({j.x, j.y}); + polygons3.push_back(t); + } + auto val3 = blackhorse::polygon_union(polygons2.data(), sz(polygons2)); + auto val4 = lovelive::polygon_union(polygons3.data(), sz(polygons3)); + if (abs(val1 - val3) > 1e-8 || abs(val1 - val4) > 1e-8) { + rep(i, 0, n) { + for (auto &x : polygons[i]) { + cout << x << ' '; } - abort(); + cout << endl; } + abort(); } } int main() { - int s = (int)time(0); - // s = 1572890368; + // int s = (int)time(0); + int s = 1; // cout << "seed " << s << endl; srand(s); for (int i = 0; i < 100; i++) { - // cerr << i << ' '; testRandom(2, 5, 5); } for (int i = 0; i < 100; i++) { testRandom(2, 10, 2); } - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 50; i++) { testRandom(5, 100, 5); } cout << "Tests passed!" << endl; diff --git a/stress-tests/geometry/PolygonUnionArea.cpp b/stress-tests/geometry/PolygonUnionArea.cpp deleted file mode 100644 index a71699337..000000000 --- a/stress-tests/geometry/PolygonUnionArea.cpp +++ /dev/null @@ -1,211 +0,0 @@ -#include - -#define all(x) begin(x), end(x) -typedef long long ll; -using namespace std; - -#define rep(i, a, b) for (int i = a; i < (b); ++i) -#define trav(a, x) for (auto &a : x) -#define all(x) begin(x), end(x) -#define sz(x) (int)(x).size() -typedef long long ll; -typedef pair pii; -typedef vector vi; - -#include "../../content/geometry/Point.h" -#include "../../content/geometry/sideOf.h" -#include "../../content/geometry/PolygonArea.h" -#include "../../content/geometry/PolygonUnionArea.h" -#include "../utilities/genPolygon.h" - -namespace blackhorse { - -using db = double; -const db eps = 1e-8; - -struct pt { - db x, y; - pt(db x = 0, db y = 0) : x(x), y(y) {} -}; - -inline int sgn(db x) { return (x > eps) - (x < -eps); } - -pt operator-(pt p1, pt p2) { return pt(p1.x - p2.x, p1.y - p2.y); } - -db vect(pt p1, pt p2) { return p1.x * p2.y - p1.y * p2.x; } - -db scal(pt p1, pt p2) { return p1.x * p2.x + p1.y * p2.y; } - -db polygon_union(vector poly[], int n) { - auto ratio = [](pt A, pt B, pt O) { - return !sgn(A.x - B.x) ? (O.y - A.y) / (B.y - A.y) : (O.x - A.x) / (B.x - A.x); - }; - db ret = 0; - for (int i = 0; i < n; ++i) { - for (int v = 0; v < poly[i].size(); ++v) { - pt A = poly[i][v], B = poly[i][(v + 1) % poly[i].size()]; - vector> segs; - segs.emplace_back(0, 0), segs.emplace_back(1, 0); - for (int j = 0; j < n; ++j) - if (i != j) { - for (size_t u = 0; u < poly[j].size(); ++u) { - pt C = poly[j][u], D = poly[j][(u + 1) % poly[j].size()]; - int sc = sgn(vect(B - A, C - A)), sd = sgn(vect(B - A, D - A)); - if (!sc && !sd) { - if (sgn(scal(B - A, D - C)) > 0 && i > j) { - segs.emplace_back(ratio(A, B, C), 1), segs.emplace_back(ratio(A, B, D), -1); - } - } else { - db sa = vect(D - C, A - C), sb = vect(D - C, B - C); - if (sc >= 0 && sd < 0) - segs.emplace_back(sa / (sa - sb), 1); - else if (sc < 0 && sd >= 0) - segs.emplace_back(sa / (sa - sb), -1); - } - } - } - sort(segs.begin(), segs.end()); - db pre = min(max(segs[0].first, 0.0), 1.0), now, sum = 0; - int cnt = segs[0].second; - for (int j = 1; j < segs.size(); ++j) { - now = min(max(segs[j].first, 0.0), 1.0); - if (!cnt) - sum += now - pre; - cnt += segs[j].second; - pre = now; - } - ret += vect(A, B) * sum; - } - } - return ret / 2; -} -} // namespace blackhorse -namespace approximate { -#include "../../content/geometry/InsidePolygon.h" -double polygonUnion(vector> &polygons, int lim) { - int cnt = 0; - int total = 0; - for (double y = -lim; y < lim; y += lim / 200.0) { - for (double x = -lim; x < lim; x += lim / 200.0) { - total++; - for (auto &i : polygons) { - if (inPolygon(i, P(x, y))) { - cnt++; - break; - } - } - } - } - return lim * lim * 4 * cnt / double(total); -} -} // namespace approximate -namespace lovelive { -#define re real -#define im imag -#define pb push_back -#define fir first -#define sec second -typedef double db; -const db pi = acos(db(-1)); -inline int sgn(db x) { return (x > 1e-8) - (x < -1e-8); } - -typedef complex cpoi; -db polygon_union(vector py[], int n) { - auto ratio = [](cpoi &a, cpoi &b, cpoi &c) { - cpoi x = b - a, y = c - a; - if (sgn(re(x)) == 0) - return im(y) / im(x); - return re(y) / re(x); - }; - db ret = 0; - for (int i = 0; i < n; ++i) - for (int v = 0; v < py[i].size(); ++v) { - cpoi a = py[i][v], b = py[i][(v + 1) % py[i].size()]; - vector> segs = {{0, 0}, {1, 0}}; - for (int j = 0; j < n; ++j) - if (i != j) - for (int u = 0; u < py[j].size(); ++u) { - cpoi c = py[j][u], d = py[j][(u + 1) % py[j].size()]; - int sc = sgn(im(conj(b - a) * (c - a))); - int sd = sgn(im(conj(b - a) * (d - a))); - if (!sc && !sd) { - if (sgn(re(conj(b - a) * (d - c))) > 0 && i > j) { - segs.pb({ratio(a, b, c), +1}); - segs.pb({ratio(a, b, d), -1}); - } - } else { - db sa = im(conj(d - c) * (a - c)); - db sb = im(conj(d - c) * (b - c)); - if (sc >= 0 && sd < 0) - segs.pb({sa / (sa - sb), 1}); - else if (sc < 0 && sd >= 0) - segs.pb({sa / (sa - sb), -1}); - } - } - sort(segs.begin(), segs.end()); - db pre = min(max(segs[0].fir, 0.0), 1.0); - db cur, sum = 0; - int cnt = segs[0].sec; - for (int j = 1; j < segs.size(); ++j) { - cur = min(max(segs[j].fir, 0.0), 1.0); - if (!cnt) - sum += cur - pre; - cnt += segs[j].sec; - pre = cur; - } - ret += im(conj(a) * b) * sum; - } - ret = abs(ret) * 0.5; - return ret; -} -} // namespace lovelive - -void testRandom(int n, int numPts = 10, int lim = 5) { - vector> polygons; - for (int i = 0; i < n; i++) { - vector

pts; - for (int j = 0; j < numPts; j++) { - pts.push_back(P((rand() % (2 * lim)) - lim, (rand() % (2 * lim)) - lim)); - } - polygons.push_back(genPolygon(pts)); - if (polygonArea2(polygons.back()) < 0) { - reverse(all(polygons.back())); - } - } - vector> polygons2; - for (auto i : polygons) { - vector t; - for (auto j : i) - t.push_back({j.x, j.y}); - polygons2.push_back(t); - } - vector> polygons3; - for (auto i : polygons) { - vector t; - for (auto j : i) - t.push_back({j.x, j.y}); - polygons3.push_back(t); - } - auto val1 = polyUnion(polygons); - auto val2 = approximate::polygonUnion(polygons, lim); - auto val3 = blackhorse::polygon_union(polygons2.data(), polygons2.size()); - auto val4 = lovelive::polygon_union(polygons3.data(), polygons3.size()); - if (abs(val1 - val2) / max(val1, val2) > 0.1) { - cout << val3 <<' '<= 0)); + for(auto &x: r) co += (ts.values[max(x, ~x)] == (x >= 0)); assert(co <= 1); } } diff --git a/stress-tests/graph/DirectedMST.cpp b/stress-tests/graph/DirectedMST.cpp index d698ff39f..47a2f2405 100644 --- a/stress-tests/graph/DirectedMST.cpp +++ b/stress-tests/graph/DirectedMST.cpp @@ -127,9 +127,9 @@ int main() { vi par = pa.second; if (debug) { cout << "r = " << r << endl; - trav(x, par) cout << x << ' '; + for(auto &x: par) cout << x << ' '; cout << endl; - trav(e, edges) { + for(auto &e: edges) { cout << e.a << ' ' << e.b << ' ' << e.w << endl; } } @@ -148,7 +148,7 @@ int main() { rep(qi,0,sz(q)) { int s = q[qi]; if (!seen[s]++) - trav(x, ch[s]) q.push_back(x); + for(auto &x: ch[s]) q.push_back(x); } assert(count(all(seen), 0) == 0); } diff --git a/stress-tests/graph/EulerWalk.cpp b/stress-tests/graph/EulerWalk.cpp index 7e1069348..437828eea 100644 --- a/stress-tests/graph/EulerWalk.cpp +++ b/stress-tests/graph/EulerWalk.cpp @@ -22,14 +22,14 @@ bool hasEulerWalk(vector>& ed, int start, bool undir, bool cycle) { bool anyEdges = false; vi nins(n); rep(i,0,n) { - trav(x, ed[i]) nins[x.first]++; + for(auto &x: ed[i]) nins[x.first]++; } rep(i,0,n) { if (!ed[i].empty()) anyEdges = true; if (undir) { assert(sz(ed[i]) == nins[i]); int nout = 0; - trav(x, ed[i]) if (x.first != i) nout++; + for(auto &x: ed[i]) if (x.first != i) nout++; if (i != start && nout % 2) odd++; } else { @@ -42,7 +42,7 @@ bool hasEulerWalk(vector>& ed, int start, bool undir, bool cycle) { if (odd > !cycle) { return false; } if (ed[start].empty() && anyEdges) { return false; } UF uf(n); - rep(i,0,n) trav(x, ed[i]) uf.join(i, x.first); + rep(i,0,n) for(auto &x: ed[i]) uf.join(i, x.first); int comp = 0; rep(i,0,n) if (uf.find(i) == i) { if (ed[i].empty()) continue; @@ -63,7 +63,7 @@ vi eulerCycle(vector>& gr, int nedges, int src=0) { D[x]--, D[y]++; eu[e] = 1; s.push_back(y); }} - trav(x, D) if (x < 0 || sz(ret) != nedges+1) return {}; + for(auto &x: D) if (x < 0 || sz(ret) != nedges+1) return {}; return {ret.rbegin(), ret.rend()}; } @@ -92,11 +92,11 @@ int main() { if (0) { cout << n << ' ' << m << ' ' << start << ' ' << undir << ' ' << cycle << endl; rep(i,0,n) { - trav(x, ed[i]) cout << x.first << ' '; + for(auto &x: ed[i]) cout << x.first << ' '; cout << endl; } cout << "returned" << endl; - trav(x, res) cout << x << ' '; + for(auto &x: res) cout << x << ' '; cout << endl; cout << "of length " << sz(res) << endl; } @@ -112,7 +112,7 @@ int main() { vi seenEdge(m); rep(i,1,sz(res)) { int x = res[i]; - trav(eid, allEds[pii(cur, x)]) { + for(auto &eid: allEds[pii(cur, x)]) { if (!seenEdge[eid]) { seenEdge[eid] = 1; goto ok; diff --git a/stress-tests/graph/LCA.cpp b/stress-tests/graph/LCA.cpp index 23a869cba..bef2136c8 100644 --- a/stress-tests/graph/LCA.cpp +++ b/stress-tests/graph/LCA.cpp @@ -26,7 +26,7 @@ struct LCA { if (d) ret.emplace_back(d, p); time[v] = T++; dist[v] = di; - trav(e, C[v]) if (e.first != p) + for(auto &e: C[v]) if (e.first != p) q.emplace_back(e.first, v, d+1, di + e.second); } return ret; diff --git a/stress-tests/graph/LinkCutTree.cpp b/stress-tests/graph/LinkCutTree.cpp index 7241d6538..ea197aeab 100644 --- a/stress-tests/graph/LinkCutTree.cpp +++ b/stress-tests/graph/LinkCutTree.cpp @@ -26,7 +26,7 @@ int main() { int a = (rand() >> 4) % N; int b = (rand() >> 4) % N; uf.e.assign(N, -1); - trav(ed, edges) uf.join(ed.first, ed.second); + for(auto &ed: edges) uf.join(ed.first, ed.second); bool c = uf.sameSet(a, b); if (!c && v != 1) { lc.link(a, b); diff --git a/stress-tests/graph/MaximalCliques.cpp b/stress-tests/graph/MaximalCliques.cpp index 8e93dfb78..b1aa3bf4f 100644 --- a/stress-tests/graph/MaximalCliques.cpp +++ b/stress-tests/graph/MaximalCliques.cpp @@ -45,7 +45,7 @@ int main1() { } return true; }; - trav(c, cl) { + for(auto &c: cl) { assert(isClique(c)); } diff --git a/stress-tests/graph/MinCostMaxFlow2.h b/stress-tests/graph/MinCostMaxFlow2.h index 05c14846c..eb67ed9cf 100644 --- a/stress-tests/graph/MinCostMaxFlow2.h +++ b/stress-tests/graph/MinCostMaxFlow2.h @@ -34,7 +34,7 @@ pair aug(G &g, int s, int t) { bool changed = true; mindist[s] = 0; for (int i = 1; !(changed = !changed); ++i) for (int v = 0; v < n; ++v) if (mindist[v] != inf) - trav(e, g[v]) { + for(auto &e: g[v]) { Flow dist = mindist[v] + (e.f<0 ? -e.cost : e.cost); if (e.r() > 0 && dist < mindist[e.dest]) { if (i >= n) assert(0);// negative cycle! shouldn't be diff --git a/stress-tests/graph/MinimumVertexCover.cpp b/stress-tests/graph/MinimumVertexCover.cpp index 297556a82..40ed409a8 100644 --- a/stress-tests/graph/MinimumVertexCover.cpp +++ b/stress-tests/graph/MinimumVertexCover.cpp @@ -7,13 +7,13 @@ vi coverHK(vector& g, int n, int m) { vi match(m, -1); int res = hopcroftKarp(g, match); vector lfound(n, true), seen(m); - trav(it, match) if (it != -1) lfound[it] = false; + for(auto &it: match) if (it != -1) lfound[it] = false; vi q, cover; rep(i,0,n) if (lfound[i]) q.push_back(i); while (!q.empty()) { int i = q.back(); q.pop_back(); lfound[i] = 1; - trav(e, g[i]) if (!seen[e] && match[e] != -1) { + for(auto &e: g[i]) if (!seen[e] && match[e] != -1) { seen[e] = true; q.push_back(match[e]); } @@ -34,17 +34,17 @@ int main() { gr[i].push_back(j); } auto verify = [&](vi& cover) { - trav(x, cover) { + for(auto &x: cover) { if (x < N) left[x] = 1; else right[x - N] = 1; } - rep(i,0,N) if (!left[i]) trav(j,gr[i]) { + rep(i,0,N) if (!left[i]) for(auto &j:gr[i]) { assert(right[j]); /* if (!right[j]) { cout << N << ' ' << M << endl; - rep(i,0,N) trav(j, gr[i]) cout << i << " - " << j << endl; + rep(i,0,N) for(auto &j: gr[i]) cout << i << " - " << j << endl; cout << "yields " << sz(cover) << endl; - trav(x, cover) cout << x << endl; + for(auto &x: cover) cout << x << endl; abort(); } */ } diff --git a/stress-tests/graph/SCC.cpp b/stress-tests/graph/SCC.cpp index dd4ad024b..30f19e0be 100644 --- a/stress-tests/graph/SCC.cpp +++ b/stress-tests/graph/SCC.cpp @@ -8,7 +8,7 @@ int no_vertices, no_components; template void dfs(int j, G &g) { low[j] = orig[j] = no_vertices++; comp[j] = -2; z.push_back(j); - trav(e,g[j]) + for(auto &e:g[j]) if (comp[e] == -1) { dfs(e, g); low[j] = min(low[j], low[e]); @@ -37,7 +37,7 @@ template vi scc(G &g) { int main() { unsigned r = 1; - for (int N = 0; N <= 5; N++) { + for (int N = 0; N <= 4; N++) { // cout << "N = " << N << endl; vector mat(N, vi(N)), adj(N); vi compsize(N), seen(N); @@ -61,17 +61,17 @@ int main() { compsize[ncomps] = sz(v); }); if (comp != comp2) { - trav(x, comp) cout << x << ' '; + for(auto &x: comp) cout << x << ' '; cout << endl; - trav(x, comp2) cout << x << ' '; + for(auto &x: comp2) cout << x << ' '; cout << endl; } rep(i,0,N) assert(comp[i] >= 0 && comp[i] < ncomps); - rep(i,0,N) trav(j, adj[i]) assert(comp[j] <= comp[i]); + rep(i,0,N) for(auto &j: adj[i]) assert(comp[j] <= comp[i]); rep(i,0,N) { seen.assign(N, 0); seen[i] = 1; rep(it,0,N) { - rep(j,0,N) if (seen[j]) trav(k, adj[j]) seen[k] = 1; + rep(j,0,N) if (seen[j]) for(auto &k: adj[j]) seen[k] = 1; } rep(j,0,N) { if (seen[j]) assert(comp[j] <= comp[i]); diff --git a/stress-tests/graph/maxflow.cpp b/stress-tests/graph/maxflow.cpp index 914763ba5..4b9f3f5c0 100644 --- a/stress-tests/graph/maxflow.cpp +++ b/stress-tests/graph/maxflow.cpp @@ -49,7 +49,7 @@ int main() { // Conservation of flow for PushRelabel vector flows(n); - rep(i,0,n) trav(e, pr.g[i]) if (e.f > 0) { + rep(i,0,n) for(auto &e: pr.g[i]) if (e.f > 0) { assert(e.c >= 0); flows[i] += e.f; flows[e.dest] -= e.f; @@ -60,7 +60,7 @@ int main() { // Conservation of flow for Dinic vector dinicFlows(n); - rep(i,0,n) trav(e, dinic.adj[i]) { + rep(i,0,n) for(auto &e: dinic.adj[i]) { assert(e.flow() <= e.oc); dinicFlows[i] += e.flow(); dinicFlows[e.to] -= e.flow(); @@ -69,7 +69,7 @@ int main() { // Conservation of flow for EdmondsKarp vector ekFlows(n); - rep(i,0,n) trav(e, origEk[i]) { + rep(i,0,n) for(auto &e: origEk[i]) { int nc = ek[i][e.first]; assert(nc >= 0); int flow = e.second - nc; @@ -84,7 +84,7 @@ int main() { ll acrossCut = 0; assert(pr.leftOfMinCut(s)); assert(!pr.leftOfMinCut(t)); - rep(i,0,n) trav(e, pr.g[i]) { + rep(i,0,n) for(auto &e: pr.g[i]) { if (pr.leftOfMinCut(i) && !pr.leftOfMinCut(e.dest)) { assert(e.f >= 0); assert(e.c == 0); diff --git a/stress-tests/graph/oldHLD.h b/stress-tests/graph/oldHLD.h index cb34e74bd..e633a2a0a 100644 --- a/stress-tests/graph/oldHLD.h +++ b/stress-tests/graph/oldHLD.h @@ -23,7 +23,7 @@ struct HLD { HLD(vector& g) : V(sz(g)) { dfs(0, -1, g, 0); - trav(c, C) { + for(auto &c: C) { c.tree = {sz(c.nodes), 0}; for (int ni : c.nodes) c.tree.update(V[ni].pos, V[ni].val); @@ -77,7 +77,7 @@ struct HLD { V[at].d = d; V[at].par = par; int sum = 1, ch, nod, sz; tuple mx(-1,-1,-1); - trav(e, g[at]){ + for(auto &e: g[at]){ if (e.first == par) continue; tie(sz, ch) = dfs(e.first, at, g, d+1); V[e.first].val = e.second; diff --git a/stress-tests/number-theory/Eratosthenes.cpp b/stress-tests/number-theory/Eratosthenes.cpp index f10fb95c2..313ca1d49 100644 --- a/stress-tests/number-theory/Eratosthenes.cpp +++ b/stress-tests/number-theory/Eratosthenes.cpp @@ -47,7 +47,7 @@ int main(int argc, char** argv) { prime_sieve ps(MAX_PR); rep(i,0,ps.primes) s += ps.prime[i]; vi r = eratosthenesSieve(MAX_PR); - trav(x, r) s2 += x; + for(auto &x: r) s2 += x; assert(s==s2); cout<<"Tests passed!"< v{0, 1, 1, 3, 5, 11}; auto v2 = berlekampMassey(v); - trav(x, v2) cout << x << ' '; + for(auto &x: v2) cout << x << ' '; cout << endl; return 0; } diff --git a/stress-tests/numerical/FastFourierTransform.cpp b/stress-tests/numerical/FastFourierTransform.cpp index 0909297c6..8146fcbf3 100644 --- a/stress-tests/numerical/FastFourierTransform.cpp +++ b/stress-tests/numerical/FastFourierTransform.cpp @@ -19,8 +19,8 @@ int main() { } vd A(4), B(6); - trav(x, A) x = rand() / (RAND_MAX + 1.0) * 10 - 5; - trav(x, B) x = rand() / (RAND_MAX + 1.0) * 10 - 5; + for(auto &x: A) x = rand() / (RAND_MAX + 1.0) * 10 - 5; + for(auto &x: B) x = rand() / (RAND_MAX + 1.0) * 10 - 5; vd C = conv(A, B); rep(i,0,sz(A) + sz(B) - 1) { double sum = 0; diff --git a/stress-tests/numerical/FastFourierTransformMod.cpp b/stress-tests/numerical/FastFourierTransformMod.cpp index 3dd8795f3..03ec4cf7d 100644 --- a/stress-tests/numerical/FastFourierTransformMod.cpp +++ b/stress-tests/numerical/FastFourierTransformMod.cpp @@ -10,7 +10,7 @@ vl simpleConv(vl a, vl b) { vl c(s); rep(i,0,sz(a)) rep(j,0,sz(b)) c[i+j] = (c[i+j] + (ll)a[i] * b[j]) % mod; - trav(x, c) if (x < 0) x += mod; + for(auto &x: c) if (x < 0) x += mod; return c; } @@ -27,8 +27,8 @@ int main() { rep(it,0,6000) { a.resize(ra() % 100); b.resize(ra() % 100); - trav(x, a) x = ra() % mod; - trav(x, b) x = ra() % mod; + for(auto &x: a) x = ra() % mod; + for(auto &x: b) x = ra() % mod; auto v1 = simpleConv(a, b); auto v2 = convMod(a, b); assert(v1 == v2); diff --git a/stress-tests/numerical/FastSubsetTransform.cpp b/stress-tests/numerical/FastSubsetTransform.cpp index 8e5592552..9f57f474f 100644 --- a/stress-tests/numerical/FastSubsetTransform.cpp +++ b/stress-tests/numerical/FastSubsetTransform.cpp @@ -5,8 +5,8 @@ int main() { rep(k,0,10) { vi a(1 << k), b = a, c = a, target = a; - trav(x, a) x = rand() % 6 - 2; - trav(x, b) x = rand() % 6 - 2; + for(auto &x: a) x = rand() % 6 - 2; + for(auto &x: b) x = rand() % 6 - 2; rep(i,0,1 << k) rep(j,0,1 << k) target[i & j] += a[i] * b[j]; // rep(i,0,1 << k) cout << a[i] << ' '; cout << endl; // rep(i,0,1 << k) cout << b[i] << ' '; cout << endl; diff --git a/stress-tests/numerical/LinearRecurrence.cpp b/stress-tests/numerical/LinearRecurrence.cpp index 9ee3ed8e6..63ea8c4b8 100644 --- a/stress-tests/numerical/LinearRecurrence.cpp +++ b/stress-tests/numerical/LinearRecurrence.cpp @@ -21,7 +21,7 @@ int main() { vector full(size); gen(start,0,[&]() { gen(coef,0,[&]() { - trav(x,full) x = 0; + for(auto &x:full) x = 0; rep(i,0,n) full[i] = start[i]; rep(i,n,size) rep(j,0,n) full[i] = (full[i] + coef[j] * full[i-1 - j]) % mod; // rep(i,0,size) cerr << full[i] << ' '; cerr << endl; diff --git a/stress-tests/numerical/NumberTheoreticTransform.cpp b/stress-tests/numerical/NumberTheoreticTransform.cpp index bc976215b..8605183d4 100644 --- a/stress-tests/numerical/NumberTheoreticTransform.cpp +++ b/stress-tests/numerical/NumberTheoreticTransform.cpp @@ -19,7 +19,7 @@ vl simpleConv(vl a, vl b) { vl c(s); rep(i,0,sz(a)) rep(j,0,sz(b)) c[i+j] = (c[i+j] + (ll)a[i] * b[j]) % mod; - trav(x, c) if (x < 0) x += mod; + for(auto &x: c) if (x < 0) x += mod; return c; } @@ -38,10 +38,10 @@ int main() { rep(it,0,6000) { a.resize(ra() % 10); b.resize(ra() % 10); - trav(x, a) x = (ra() % 100 - 50+mod)%mod; - trav(x, b) x = (ra() % 100 - 50+mod)%mod; - trav(x, simpleConv(a, b)) res += (ll)x * ind++ % mod; - trav(x, conv(a, b)) res2 += (ll)x * ind2++ % mod; + for(auto &x: a) x = (ra() % 100 - 50+mod)%mod; + for(auto &x: b) x = (ra() % 100 - 50+mod)%mod; + for(auto &x: simpleConv(a, b)) res += (ll)x * ind++ % mod; + for(auto &x: conv(a, b)) res2 += (ll)x * ind2++ % mod; } assert(res==res2); cout<<"Tests passed!"<> str; trav(x, pi(str)) cout << x; cout << endl; + // string str; cin >> str; for(auto &x: pi(str)) cout << x; cout << endl; // test ~3^12 strings rep(n,0,13) { string s(n, 'x'); diff --git a/stress-tests/strings/SuffixArray.cpp b/stress-tests/strings/SuffixArray.cpp index 666499c2d..d4a9ee521 100644 --- a/stress-tests/strings/SuffixArray.cpp +++ b/stress-tests/strings/SuffixArray.cpp @@ -35,7 +35,7 @@ struct VecSuffixArray { string display(const string& s) { string ret; - trav(x, s) ret += (char)('a' + x - 1); + for(auto &x: s) ret += (char)('a' + x - 1); return ret; } diff --git a/stress-tests/utilities/genPolygon.h b/stress-tests/utilities/genPolygon.h index 8bd662b5e..ee81c93f3 100644 --- a/stress-tests/utilities/genPolygon.h +++ b/stress-tests/utilities/genPolygon.h @@ -3,6 +3,7 @@ // Translated from Python code posted here: https://codeforces.com/blog/entry/63058?#comment-472788 // May generate polygons with colinear points. Won't generate polygons with duplicate points. No guarantee of the direction of the polygon. #include "../../content/geometry/Point.h" +#include "../../content/geometry/PolygonArea.h" #include "random.h" template pair> conquer(vector

pts, int depth) { @@ -41,7 +42,8 @@ template pair> conquer(vector

pts, int depth) { pa.second.insert(pa.second.end(), all(pb.second)); return pa; } -template vector

genPolygon(vector

pts) { +template vector

genPolygon(vector

pts, int depth=0) { + if (depth>100) return {P(0,0), P(1,0), P(0,1)}; sort(all(pts)); pts.resize(unique(all(pts)) - pts.begin()); shuffle_vec(pts); @@ -60,7 +62,7 @@ template vector

genPolygon(vector

pts) { auto tb = conquer(S[1],0); auto pa=ta.second, pb=tb.second; if (!ta.first || !tb.first) { - return genPolygon(pts); + return genPolygon(pts, depth+1); } pa.erase(pa.begin()); pb.erase(pb.begin()); diff --git a/stress-tests/utilities/template.h b/stress-tests/utilities/template.h index ac359df12..eb14ea010 100644 --- a/stress-tests/utilities/template.h +++ b/stress-tests/utilities/template.h @@ -2,7 +2,6 @@ using namespace std; #define rep(i, from, to) for (int i = from; i < (to); ++i) -#define trav(a, x) for (auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; diff --git a/stress-tests/various/IntervalContainer.cpp b/stress-tests/various/IntervalContainer.cpp index cb40161ff..7fcc54183 100644 --- a/stress-tests/various/IntervalContainer.cpp +++ b/stress-tests/various/IntervalContainer.cpp @@ -44,7 +44,7 @@ int main() { } int last = -1; int ct = 0; - trav(pa, iv) { + for(auto &pa: iv) { assert(0 <= pa.first && pa.second <= N); assert(pa.first < pa.second); assert(last < pa.first); diff --git a/stress-tests/various/LIS.cpp b/stress-tests/various/LIS.cpp index eef17f26a..a75ce4cdb 100644 --- a/stress-tests/various/LIS.cpp +++ b/stress-tests/various/LIS.cpp @@ -26,7 +26,7 @@ int main() { rep(it,0,1000000) { int n = rand() % 7; vi v(n); - trav(x, v) x = rand() % 4; + for(auto &x: v) x = rand() % 4; vi inds = weak ? lisWeak(v) : lis(v); rep(i,0,sz(inds)-1) { assert(lt(v[inds[i]], v[inds[i+1]])); @@ -40,7 +40,7 @@ int main() { prev = v[i]; } cout << "exists lis of size " << si << " but found only " << sz(inds) << endl; - trav(x, v) cout << x << ' '; + for(auto &x: v) cout << x << ' '; cout << endl; abort(); next:;