Parcourir la source

remove redundant headers + global cleaning

Olivier Marty il y a 8 ans
Parent
commit
a37b814b42

+ 0 - 7
code/BIT.cc

@@ -1,11 +1,6 @@
-#include <iostream>
-using namespace std;
-
 #define LOGSZ 17
-
 int tree[(1<<LOGSZ)+1];
 int N = (1<<LOGSZ);
-
 // add v to value at x
 void set(int x, int v) {
   while(x <= N) {
@@ -13,7 +8,6 @@ void set(int x, int v) {
     x += (x & -x);
   }
 }
-
 // get cumulative sum up to and including x
 int get(int x) {
   int res = 0;
@@ -23,7 +17,6 @@ int get(int x) {
   }
   return res;
 }
-
 // get largest value with cumulative sum less than or equal to x;
 // for smallest, pass x-1 and add 1 to result
 int getind(int x) {

+ 0 - 1
code/BellmanFord.cpp

@@ -11,7 +11,6 @@ typedef struct Edge{
 vector<Edge> edges;
 int dist[10000];
 int nodenum;
-const int INF = 1000000000;
 void relax(int u, int v, int w) {
   if(dist[v] > dist[u] + w) {
     dist[v] = dist[u] + w;

+ 0 - 1
code/ConvexHull.cc

@@ -7,7 +7,6 @@
 //          with bottommost/leftmost point
 #define REMOVE_REDUNDANT
 typedef double T;
-const T EPS = 1e-7;
 struct PT {
   T x, y;
   PT() {}

+ 4 - 16
code/Dates.cc

@@ -2,27 +2,18 @@
 // months are expressed as integers from 1 to 12, days are expressed
 // as integers from 1 to 31, and years are expressed as 4-digit
 // integers.
-
-#include <iostream>
-#include <string>
-
-using namespace std;
-
 string dayOfWeek[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
-
 // converts Gregorian date to integer (Julian day number)
-int dateToInt (int m, int d, int y){  
-  return 
+int dateToInt (int m, int d, int y){
+  return
     1461 * (y + 4800 + (m - 14) / 12) / 4 +
-    367 * (m - 2 - (m - 14) / 12 * 12) / 12 - 
-    3 * ((y + 4900 + (m - 14) / 12) / 100) / 4 + 
+    367 * (m - 2 - (m - 14) / 12 * 12) / 12 -
+    3 * ((y + 4900 + (m - 14) / 12) / 100) / 4 +
     d - 32075;
 }
-
 // converts integer (Julian day number) to Gregorian date: month/day/year
 void intToDate (int jd, int &m, int &d, int &y){
   int x, n, i, j;
-  
   x = jd + 68569;
   n = 4 * x / 146097;
   x -= (146097 * n + 3) / 4;
@@ -34,18 +25,15 @@ void intToDate (int jd, int &m, int &d, int &y){
   m = j + 2 - 12 * x;
   y = 100 * (n - 49) + i + x;
 }
-
 // converts integer (Julian day number) to day of week
 string intToDay (int jd){
   return dayOfWeek[jd % 7];
 }
-
 int main (int argc, char **argv){
   int jd = dateToInt (3, 24, 2004);
   int m, d, y;
   intToDate (jd, m, d, y);
   string day = intToDay (jd);
-  
   // expected output:
   //    2453089
   //    3/24/2004

+ 32 - 46
code/Delaunay.cc

@@ -1,64 +1,50 @@
 // Slow but simple Delaunay triangulation. Does not handle
 // degenerate cases (from O'Rourke, Computational Geometry in C)
-//
 // Running time: O(n^4)
-//
 // INPUT:    x[] = x-coordinates
 //           y[] = y-coordinates
-//
 // OUTPUT:   triples = a vector containing m triples of indices
 //                     corresponding to triangle vertices
-
-#include<vector>
-using namespace std;
-
 typedef double T;
-
 struct triple {
     int i, j, k;
     triple() {}
     triple(int i, int j, int k) : i(i), j(j), k(k) {}
 };
-
 vector<triple> delaunayTriangulation(vector<T>& x, vector<T>& y) {
-	int n = x.size();
-	vector<T> z(n);
-	vector<triple> ret;
-
-	for (int i = 0; i < n; i++)
-	    z[i] = x[i] * x[i] + y[i] * y[i];
-
-	for (int i = 0; i < n-2; i++) {
-	    for (int j = i+1; j < n; j++) {
-		for (int k = i+1; k < n; k++) {
-		    if (j == k) continue;
-		    double xn = (y[j]-y[i])*(z[k]-z[i]) - (y[k]-y[i])*(z[j]-z[i]);
-		    double yn = (x[k]-x[i])*(z[j]-z[i]) - (x[j]-x[i])*(z[k]-z[i]);
-		    double zn = (x[j]-x[i])*(y[k]-y[i]) - (x[k]-x[i])*(y[j]-y[i]);
-		    bool flag = zn < 0;
-		    for (int m = 0; flag && m < n; m++)
-			flag = flag && ((x[m]-x[i])*xn + 
-					(y[m]-y[i])*yn + 
-					(z[m]-z[i])*zn <= 0);
-		    if (flag) ret.push_back(triple(i, j, k));
-		}
-	    }
-	}
-	return ret;
+  int n = x.size();
+  vector<T> z(n);
+  vector<triple> ret;
+  for(int i = 0; i < n; i++)
+    z[i] = x[i] * x[i] + y[i] * y[i];
+  for(int i = 0; i < n-2; i++) {
+    for(int j = i+1; j < n; j++) {
+        for(int k = i+1; k < n; k++) {
+            if(j == k) continue;
+        double xn = (y[j]-y[i])*(z[k]-z[i]) - (y[k]-y[i])*(z[j]-z[i]);
+        double yn = (x[k]-x[i])*(z[j]-z[i]) - (x[j]-x[i])*(z[k]-z[i]);
+        double zn = (x[j]-x[i])*(y[k]-y[i]) - (x[k]-x[i])*(y[j]-y[i]);
+        bool flag = zn < 0;
+        for(int m = 0; flag && m < n; m++)
+          flag = flag && ((x[m]-x[i])*xn +
+            (y[m]-y[i])*yn +
+            (z[m]-z[i])*zn <= 0);
+        if(flag) ret.push_back(triple(i, j, k));
+      }
+    }
+  }
+  return ret;
 }
-
 int main()
 {
-    T xs[]={0, 0, 1, 0.9};
-    T ys[]={0, 1, 0, 0.9};
-    vector<T> x(&xs[0], &xs[4]), y(&ys[0], &ys[4]);
-    vector<triple> tri = delaunayTriangulation(x, y);
-    
-    //expected: 0 1 3
-    //          0 3 2
-    
-    int i;
-    for(i = 0; i < tri.size(); i++)
-        printf("%d %d %d\n", tri[i].i, tri[i].j, tri[i].k);
-    return 0;
+  T xs[]={0, 0, 1, 0.9};
+  T ys[]={0, 1, 0, 0.9};
+  vector<T> x(&xs[0], &xs[4]), y(&ys[0], &ys[4]);
+  vector<triple> tri = delaunayTriangulation(x, y);
+  //expected: 0 1 3
+  //          0 3 2
+  int i;
+  for(i = 0; i < tri.size(); i++)
+    printf("%d %d %d\n", tri[i].i, tri[i].j, tri[i].k);
+  return 0;
 }

+ 0 - 4
code/Dinic.cc

@@ -1,19 +1,15 @@
 // Adjacency list implementation of Dinic's blocking flow algorithm.
 // This is very fast in practice, and only loses to push-relabel flow.
-//
 // Running time:
 //     O(|V|^2 |E|)
-//
 // INPUT:
 //     - oriented graph, constructed using AddEdge()
 //     - source
 //     - sink
-//
 // OUTPUT:
 //     - maximum flow value
 //     - To obtain the actual flow values, look at all edges with
 //       capacity > 0 (zero capacity edges are residual edges).
-
 struct Edge {
   int from, to, cap, flow, index;
   Edge(int from, int to, int cap, int flow, int index) :

+ 3 - 28
code/Euclid.cc

@@ -2,34 +2,22 @@
 // involve modular linear equations.  Note that all of the
 // algorithms described here work on nonnegative integers.
 
-#include <iostream>
-#include <vector>
-#include <algorithm>
-
-using namespace std;
-
-typedef vector<int> VI;
-typedef pair<int,int> PII;
-
 // return a % b (positive value)
 int mod(int a, int b) {
   return ((a%b)+b)%b;
 }
-
 // computes gcd(a,b)
 int gcd(int a, int b) {
   int tmp;
   while(b){a%=b; tmp=a; a=b; b=tmp;}
   return a;
 }
-
 // computes lcm(a,b)
 int lcm(int a, int b) {
   return a/gcd(a,b)*b;
 }
-
 // returns d = gcd(a,b); finds x,y such that d = ax + by
-int extended_euclid(int a, int b, int &x, int &y) {  
+int extended_euclid(int a, int b, int &x, int &y) {
   int xx = y = 0;
   int yy = x = 1;
   while (b) {
@@ -40,7 +28,6 @@ int extended_euclid(int a, int b, int &x, int &y) {
   }
   return a;
 }
-
 // finds all solutions to ax = b (mod n)
 VI modular_linear_equation_solver(int a, int b, int n) {
   int x, y;
@@ -53,7 +40,6 @@ VI modular_linear_equation_solver(int a, int b, int n) {
   }
   return solutions;
 }
-
 // computes b such that ab = 1 (mod n), returns -1 on failure
 int mod_inverse(int a, int n) {
   int x, y;
@@ -61,7 +47,6 @@ int mod_inverse(int a, int n) {
   if (d > 1) return -1;
   return mod(x,n);
 }
-
 // Chinese remainder theorem (special case): find z such that
 // z % x = a, z % y = b.  Here, z is unique modulo M = lcm(x,y).
 // Return (z,M).  On failure, M = -1.
@@ -71,10 +56,9 @@ PII chinese_remainder_theorem(int x, int a, int y, int b) {
   if (a%d != b%d) return make_pair(0, -1);
   return make_pair(mod(s*b*x+t*a*y,x*y)/d, x*y/d);
 }
-
 // Chinese remainder theorem: find z such that
 // z % x[i] = a[i] for all i.  Note that the solution is
-// unique modulo M = lcm_i (x[i]).  Return (z,M).  On 
+// unique modulo M = lcm_i (x[i]).  Return (z,M).  On
 // failure, M = -1.  Note that we do not require the a[i]'s
 // to be relatively prime.
 PII chinese_remainder_theorem(const VI &x, const VI &a) {
@@ -85,7 +69,6 @@ PII chinese_remainder_theorem(const VI &x, const VI &a) {
   }
   return ret;
 }
-
 // computes x and y such that ax + by = c; on failure, x = y =-1
 void linear_diophantine(int a, int b, int c, int &x, int &y) {
   int d = gcd(a,b);
@@ -96,25 +79,19 @@ void linear_diophantine(int a, int b, int c, int &x, int &y) {
     y = (c-a*x)/b;
   }
 }
-
 int main() {
-  
   // expected: 2
   cout << gcd(14, 30) << endl;
-  
   // expected: 2 -2 1
   int x, y;
   int d = extended_euclid(14, 30, x, y);
   cout << d << " " << x << " " << y << endl;
-  
   // expected: 95 45
   VI sols = modular_linear_equation_solver(14, 30, 100);
-  for (int i = 0; i < (int) sols.size(); i++) cout << sols[i] << " "; 
+  for (int i = 0; i < (int) sols.size(); i++) cout << sols[i] << " ";
   cout << endl;
-  
   // expected: 8
   cout << mod_inverse(8, 9) << endl;
-  
   // expected: 23 56
   //           11 12
   int xs[] = {3, 5, 7, 4, 6};
@@ -123,9 +100,7 @@ int main() {
   cout << ret.first << " " << ret.second << endl;
   ret = chinese_remainder_theorem (VI(xs+3, xs+5), VI(as+3, as+5));
   cout << ret.first << " " << ret.second << endl;
-  
   // expected: 5 -15
   linear_diophantine(7, 2, 5, x, y);
   cout << x << " " << y << endl;
-
 }

+ 9 - 38
code/FastDijkstra.cc

@@ -1,54 +1,25 @@
 // Implementation of Dijkstra's algorithm using adjacency lists
 // and priority queue for efficiency.
-//
 // Running time: O(|E| log |V|)
-
-#include <queue>
-#include <stdio.h>
-
-using namespace std;
-const int INF = 2000000000;
-typedef pair<int,int> PII;
-
-int main(){
-  
-  int N, s, t;
-  scanf ("%d%d%d", &N, &s, &t);
-  vector<vector<PII> > edges(N);
-  for (int i = 0; i < N; i++){
-    int M;
-    scanf ("%d", &M);
-    for (int j = 0; j < M; j++){
-      int vertex, dist;
-      scanf ("%d%d", &vertex, &dist);
-      edges[i].push_back (make_pair (dist, vertex)); // note order of arguments here
-    }
-  }
-  
+// USAGE: edges: pair of weight/destination, source, target
+void Dijkstra(VVPII edges, int s, int t) {
   // use priority queue in which top element has the "smallest" priority
-  priority_queue<PII, vector<PII>, greater<PII> > Q;
-  vector<int> dist(N, INF), dad(N, -1);
-  Q.push (make_pair (0, s));
+  priority_queue<PII, VPII, greater<PII> > Q;
+  VI dist(edges.size(), INF), dad(edges.size(), -1);
+  Q.push(make_pair (0, s));
   dist[s] = 0;
-  while (!Q.empty()){
+  while(!Q.empty()){
     PII p = Q.top();
     if (p.second == t) break;
     Q.pop();
-    
     int here = p.second;
-    for (vector<PII>::iterator it=edges[here].begin(); it!=edges[here].end(); it++){
-      if (dist[here] + it->first < dist[it->second]){
+    for(VPII::iterator it=edges[here].begin(); it!=edges[here].end(); it++){
+      if(dist[here] + it->first < dist[it->second]){
         dist[it->second] = dist[here] + it->first;
         dad[it->second] = here;
         Q.push (make_pair (dist[it->second], it->second));
       }
     }
   }
-  
-  printf ("%d\n", dist[t]);
-  if (dist[t] < INF)
-    for(int i=t;i!=-1;i=dad[i])
-      printf ("%d%c", i, (i==s?'\n':' '));
-    
-  return 0;
+  // dist contains distances
 }

+ 2 - 26
code/GaussJordan.cc

@@ -1,38 +1,22 @@
 // Gauss-Jordan elimination with full pivoting.
-//
 // Uses:
 //   (1) solving systems of linear equations (AX=B)
 //   (2) inverting matrices (AX=I)
 //   (3) computing determinants of square matrices
-//
 // Running time: O(n^3)
-//
 // INPUT:    a[][] = an nxn matrix
 //           b[][] = an nxm matrix
-//
 // OUTPUT:   X      = an nxm matrix (stored in b[][])
 //           A^{-1} = an nxn matrix (stored in a[][])
 //           returns determinant of a[][]
-
-#include <iostream>
-#include <vector>
-#include <cmath>
-
-using namespace std;
-
-const double EPS = 1e-10;
-
-typedef vector<int> VI;
 typedef double T;
 typedef vector<T> VT;
 typedef vector<VT> VVT;
-
 T GaussJordan(VVT &a, VVT &b) {
   const int n = a.size();
   const int m = b[0].size();
   VI irow(n), icol(n), ipiv(n);
   T det = 1;
-
   for (int i = 0; i < n; i++) {
     int pj = -1, pk = -1;
     for (int j = 0; j < n; j++) if (!ipiv[j])
@@ -45,7 +29,6 @@ T GaussJordan(VVT &a, VVT &b) {
     if (pj != pk) det *= -1;
     irow[i] = pj;
     icol[i] = pk;
-
     T c = 1.0 / a[pk][pk];
     det *= a[pk][pk];
     a[pk][pk] = 1.0;
@@ -55,17 +38,14 @@ T GaussJordan(VVT &a, VVT &b) {
       c = a[p][pk];
       a[p][pk] = 0;
       for (int q = 0; q < n; q++) a[p][q] -= a[pk][q] * c;
-      for (int q = 0; q < m; q++) b[p][q] -= b[pk][q] * c;      
+      for (int q = 0; q < m; q++) b[p][q] -= b[pk][q] * c;
     }
   }
-
   for (int p = n-1; p >= 0; p--) if (irow[p] != icol[p]) {
     for (int k = 0; k < n; k++) swap(a[k][irow[p]], a[k][icol[p]]);
   }
-
   return det;
 }
-
 int main() {
   const int n = 4;
   const int m = 2;
@@ -76,12 +56,9 @@ int main() {
     a[i] = VT(A[i], A[i] + n);
     b[i] = VT(B[i], B[i] + m);
   }
-  
   double det = GaussJordan(a, b);
-  
-  // expected: 60  
+  // expected: 60
   cout << "Determinant: " << det << endl;
-
   // expected: -0.233333 0.166667 0.133333 0.0666667
   //           0.166667 0.166667 0.333333 -0.333333
   //           0.233333 0.833333 -0.133333 -0.0666667
@@ -92,7 +69,6 @@ int main() {
       cout << a[i][j] << ' ';
     cout << endl;
   }
-  
   // expected: 1.63333 1.3
   //           -0.166667 0.5
   //           2.36667 1.7

+ 0 - 9
code/Geometry.cc

@@ -1,15 +1,6 @@
 // C++ routines for computational geometry.
-
-#include <iostream>
-#include <vector>
-#include <cmath>
-#include <cassert>
-
-using namespace std;
-
 double INF = 1e100;
 double EPS = 1e-12;
-
 struct PT {
   double x, y;
   PT() {}

+ 0 - 1
code/Inversion.cpp

@@ -4,7 +4,6 @@
 // COMPLEXITY: n log n
 // pair.first: valeur de la case
 // pair.second: indice dans le tableau initialement (rep est rempli en fonction)
-typedef pair<int, int> PII;
 PII tmp[100001]; // tableau temporaire
 int rep[100001]; // réponse (doit être initialisé à 0)
 void fusion(PII *s1, PII *e1, PII *s2, PII *e2) {

+ 122 - 171
code/KDTree.cc

@@ -1,205 +1,156 @@
-// --------------------------------------------------------------------------
 // A straightforward, but probably sub-optimal KD-tree implmentation that's
 // probably good enough for most things (current it's a 2D-tree)
-//
 //  - constructs from n points in O(n lg^2 n) time
 //  - handles nearest-neighbor query in O(lg n) if points are well distributed
 //  - worst case for nearest-neighbor may be linear in pathological case
-//
 // Sonny Chan, Stanford University, April 2009
-// --------------------------------------------------------------------------
-
-#include <iostream>
-#include <vector>
-#include <limits>
-#include <cstdlib>
-
-using namespace std;
 
 // number type for coordinates, and its maximum value
 typedef long long ntype;
 const ntype sentry = numeric_limits<ntype>::max();
-
 // point structure for 2D-tree, can be extended to 3D
 struct point {
-    ntype x, y;
-    point(ntype xx = 0, ntype yy = 0) : x(xx), y(yy) {}
+  ntype x, y;
+  point(ntype xx = 0, ntype yy = 0) : x(xx), y(yy) {}
 };
-
-bool operator==(const point &a, const point &b)
-{
-    return a.x == b.x && a.y == b.y;
+bool operator==(const point &a, const point &b) {
+  return a.x == b.x && a.y == b.y;
 }
-
 // sorts points on x-coordinate
-bool on_x(const point &a, const point &b)
-{
-    return a.x < b.x;
+bool on_x(const point &a, const point &b) {
+  return a.x < b.x;
 }
-
 // sorts points on y-coordinate
-bool on_y(const point &a, const point &b)
-{
-    return a.y < b.y;
+bool on_y(const point &a, const point &b) {
+  return a.y < b.y;
 }
-
 // squared distance between points
-ntype pdist2(const point &a, const point &b)
-{
-    ntype dx = a.x-b.x, dy = a.y-b.y;
-    return dx*dx + dy*dy;
+ntype pdist2(const point &a, const point &b) {
+  ntype dx = a.x-b.x, dy = a.y-b.y;
+  return dx*dx + dy*dy;
 }
-
 // bounding box for a set of points
-struct bbox
-{
-    ntype x0, x1, y0, y1;
-    
-    bbox() : x0(sentry), x1(-sentry), y0(sentry), y1(-sentry) {}
-    
-    // computes bounding box from a bunch of points
-    void compute(const vector<point> &v) {
-        for (int i = 0; i < v.size(); ++i) {
-            x0 = min(x0, v[i].x);   x1 = max(x1, v[i].x);
-            y0 = min(y0, v[i].y);   y1 = max(y1, v[i].y);
-        }
-    }
-    
-    // squared distance between a point and this bbox, 0 if inside
-    ntype distance(const point &p) {
-        if (p.x < x0) {
-            if (p.y < y0)       return pdist2(point(x0, y0), p);
-            else if (p.y > y1)  return pdist2(point(x0, y1), p);
-            else                return pdist2(point(x0, p.y), p);
-        }
-        else if (p.x > x1) {
-            if (p.y < y0)       return pdist2(point(x1, y0), p);
-            else if (p.y > y1)  return pdist2(point(x1, y1), p);
-            else                return pdist2(point(x1, p.y), p);
-        }
-        else {
-            if (p.y < y0)       return pdist2(point(p.x, y0), p);
-            else if (p.y > y1)  return pdist2(point(p.x, y1), p);
-            else                return 0;
-        }
+struct bbox {
+  ntype x0, x1, y0, y1;
+  bbox() : x0(sentry), x1(-sentry), y0(sentry), y1(-sentry) {}
+  // computes bounding box from a bunch of points
+  void compute(const vector<point> &v) {
+      for (int i = 0; i < v.size(); ++i) {
+          x0 = min(x0, v[i].x);   x1 = max(x1, v[i].x);
+          y0 = min(y0, v[i].y);   y1 = max(y1, v[i].y);
+      }
+  }
+  // squared distance between a point and this bbox, 0 if inside
+  ntype distance(const point &p) {
+  if (p.x < x0) {
+    if (p.y < y0)       return pdist2(point(x0, y0), p);
+    else if (p.y > y1)  return pdist2(point(x0, y1), p);
+    else                return pdist2(point(x0, p.y), p);
+  }
+  else if (p.x > x1) {
+    if (p.y < y0)       return pdist2(point(x1, y0), p);
+    else if (p.y > y1)  return pdist2(point(x1, y1), p);
+    else                return pdist2(point(x1, p.y), p);
+  }
+  else {
+    if (p.y < y0)       return pdist2(point(p.x, y0), p);
+    else if (p.y > y1)  return pdist2(point(p.x, y1), p);
+    else                return 0;
     }
+  }
 };
-
 // stores a single node of the kd-tree, either internal or leaf
-struct kdnode 
-{
-    bool leaf;      // true if this is a leaf node (has one point)
-    point pt;       // the single point of this is a leaf
-    bbox bound;     // bounding box for set of points in children
-    
-    kdnode *first, *second; // two children of this kd-node
-    
-    kdnode() : leaf(false), first(0), second(0) {}
-    ~kdnode() { if (first) delete first; if (second) delete second; }
-    
-    // intersect a point with this node (returns squared distance)
-    ntype intersect(const point &p) {
-        return bound.distance(p);
+struct kdnode {
+  bool leaf;      // true if this is a leaf node (has one point)
+  point pt;       // the single point of this is a leaf
+  bbox bound;     // bounding box for set of points in children
+  kdnode *first, *second; // two children of this kd-node
+  kdnode() : leaf(false), first(0), second(0) {}
+  ~kdnode() { if (first) delete first; if (second) delete second; }
+  // intersect a point with this node (returns squared distance)
+  ntype intersect(const point &p) {
+    return bound.distance(p);
+  }
+  // recursively builds a kd-tree from a given cloud of points
+  void construct(vector<point> &vp) {
+    // compute bounding box for points at this node
+    bound.compute(vp);
+    // if we're down to one point, then we're a leaf node
+    if (vp.size() == 1) {
+      leaf = true;
+      pt = vp[0];
     }
-    
-    // recursively builds a kd-tree from a given cloud of points
-    void construct(vector<point> &vp)
-    {
-        // compute bounding box for points at this node
-        bound.compute(vp);
-        
-        // if we're down to one point, then we're a leaf node
-        if (vp.size() == 1) {
-            leaf = true;
-            pt = vp[0];
-        }
-        else {
-            // split on x if the bbox is wider than high (not best heuristic...)
-            if (bound.x1-bound.x0 >= bound.y1-bound.y0)
-                sort(vp.begin(), vp.end(), on_x);
-            // otherwise split on y-coordinate
-            else
-                sort(vp.begin(), vp.end(), on_y);
-            
-            // divide by taking half the array for each child
-            // (not best performance if many duplicates in the middle)
-            int half = vp.size()/2;
-            vector<point> vl(vp.begin(), vp.begin()+half);
-            vector<point> vr(vp.begin()+half, vp.end());
-            first = new kdnode();   first->construct(vl);
-            second = new kdnode();  second->construct(vr);            
-        }
+    else {
+      // split on x if the bbox is wider than high (not best heuristic...)
+      if (bound.x1-bound.x0 >= bound.y1-bound.y0)
+        sort(vp.begin(), vp.end(), on_x);
+      // otherwise split on y-coordinate
+      else
+        sort(vp.begin(), vp.end(), on_y);
+      // divide by taking half the array for each child
+      // (not best performance if many duplicates in the middle)
+      int half = vp.size()/2;
+      vector<point> vl(vp.begin(), vp.begin()+half);
+      vector<point> vr(vp.begin()+half, vp.end());
+      first = new kdnode();   first->construct(vl);
+      second = new kdnode();  second->construct(vr);
     }
+  }
 };
-
 // simple kd-tree class to hold the tree and handle queries
-struct kdtree
-{
-    kdnode *root;
-    
-    // constructs a kd-tree from a points (copied here, as it sorts them)
-    kdtree(const vector<point> &vp) {
-        vector<point> v(vp.begin(), vp.end());
-        root = new kdnode();
-        root->construct(v);
+struct kdtree {
+  kdnode *root;
+  // constructs a kd-tree from a points (copied here, as it sorts them)
+  kdtree(const vector<point> &vp) {
+    vector<point> v(vp.begin(), vp.end());
+    root = new kdnode();
+    root->construct(v);
+  }
+  ~kdtree() { delete root; }
+  // recursive search method returns squared distance to nearest point
+  ntype search(kdnode *node, const point &p) {
+    if (node->leaf) {
+      // commented special case tells a point not to find itself
+//      if (p == node->pt) return sentry;
+//      else
+      return pdist2(p, node->pt);
     }
-    ~kdtree() { delete root; }
-    
-    // recursive search method returns squared distance to nearest point
-    ntype search(kdnode *node, const point &p)
-    {
-        if (node->leaf) {
-            // commented special case tells a point not to find itself
-//            if (p == node->pt) return sentry;
-//            else               
-                return pdist2(p, node->pt);
-        }
-        
-        ntype bfirst = node->first->intersect(p);
-        ntype bsecond = node->second->intersect(p);
-        
-        // choose the side with the closest bounding box to search first
-        // (note that the other side is also searched if needed)
-        if (bfirst < bsecond) {
-            ntype best = search(node->first, p);
-            if (bsecond < best)
-                best = min(best, search(node->second, p));
-            return best;
-        }
-        else {
-            ntype best = search(node->second, p);
-            if (bfirst < best)
-                best = min(best, search(node->first, p));
-            return best;
-        }
+
+    ntype bfirst = node->first->intersect(p);
+    ntype bsecond = node->second->intersect(p);
+    // choose the side with the closest bounding box to search first
+    // (note that the other side is also searched if needed)
+    if (bfirst < bsecond) {
+      ntype best = search(node->first, p);
+      if (bsecond < best)
+        best = min(best, search(node->second, p));
+      return best;
     }
-    
-    // squared distance to the nearest 
-    ntype nearest(const point &p) {
-        return search(root, p);
+    else {
+      ntype best = search(node->second, p);
+      if (bfirst < best)
+        best = min(best, search(node->first, p));
+      return best;
     }
+  }
+  // squared distance to the nearest
+  ntype nearest(const point &p) {
+    return search(root, p);
+  }
 };
-
-// --------------------------------------------------------------------------
 // some basic test code here
-
-int main()
-{
-    // generate some random points for a kd-tree
-    vector<point> vp;
-    for (int i = 0; i < 100000; ++i) {
-        vp.push_back(point(rand()%100000, rand()%100000));
-    }
-    kdtree tree(vp);
-    
-    // query some points
-    for (int i = 0; i < 10; ++i) {
-        point q(rand()%100000, rand()%100000);
-        cout << "Closest squared distance to (" << q.x << ", " << q.y << ")"
-             << " is " << tree.nearest(q) << endl;
-    }    
-
-    return 0;
+int main() {
+  // generate some random points for a kd-tree
+  vector<point> vp;
+  for (int i = 0; i < 100000; ++i) {
+    vp.push_back(point(rand()%100000, rand()%100000));
+  }
+  kdtree tree(vp);
+  // query some points
+  for (int i = 0; i < 10; ++i) {
+    point q(rand()%100000, rand()%100000);
+    cout << "Closest squared distance to (" << q.x << ", " << q.y << ")"
+         << " is " << tree.nearest(q) << endl;
+  }
+  return 0;
 }
-
-// --------------------------------------------------------------------------

+ 0 - 14
code/LongestIncreasingSubsequence.cc

@@ -5,23 +5,10 @@
 //
 //   INPUT: a vector of integers
 //   OUTPUT: a vector containing the longest increasing subsequence
-
-#include <iostream>
-#include <vector>
-#include <algorithm>
-
-using namespace std;
-
-typedef vector<int> VI;
-typedef pair<int,int> PII;
-typedef vector<PII> VPII;
-
 #define STRICTLY_INCREASNG // buggué
-
 VI LongestIncreasingSubsequence(VI v) {
   VPII best;
   VI dad(v.size(), -1);
-
   for (int i = 0; i < v.size(); i++) {
 #ifdef STRICTLY_INCREASNG
     PII item = make_pair(v[i], 0);
@@ -39,7 +26,6 @@ VI LongestIncreasingSubsequence(VI v) {
       *it = item;
     }
   }
-
   VI ret;
   for (int i = best.back().second; i >= 0; i = dad[i])
     ret.push_back(v[i]);

+ 0 - 10
code/MaxBipartiteMatching.cc

@@ -6,14 +6,6 @@
 //   OUTPUT: mr[i] = assignment for row node i, -1 if unassigned
 //           mc[j] = assignment for column node j, -1 if unassigned
 //           function returns number of matches made
-
-#include <vector>
-
-using namespace std;
-
-typedef vector<int> VI;
-typedef vector<VI> VVI;
-
 bool FindMatch(int i, const VVI &w, VI &mr, VI &mc, VI &seen) {
   for (int j = 0; j < w[i].size(); j++) {
     if (w[i][j] && !seen[j]) {
@@ -27,11 +19,9 @@ bool FindMatch(int i, const VVI &w, VI &mr, VI &mc, VI &seen) {
   }
   return false;
 }
-
 int BipartiteMatching(const VVI &w, VI &mr, VI &mc) {
   mr = VI(w.size(), -1);
   mc = VI(w[0].size(), -1);
-  
   int ct = 0;
   for (int i = 0; i < w.size(); i++) {
     VI seen(w[0].size());

+ 0 - 1
code/MaxBipartiteMatching_sqrt.cc

@@ -1,6 +1,5 @@
 // O(sqrt(n)*E)
 // Use : set G (adjacency list), set uN, then m = MaxMatch() and the result is in Mx and My
-
 const int MAXN = 1000; const int INF = 0x3f3f3f3f; vector<int>G [MAXN];
 int uN;
 int Mx[MAXN],My[MAXN]; int dx[MAXN],dy[MAXN]; int dis;

+ 0 - 5
code/MinCostMatching.cc

@@ -11,11 +11,6 @@
 //
 // The values in cost[i][j] may be positive or negative.  To perform
 // maximization, simply negate the cost[][] matrix.
-
-typedef vector<double> VD;
-typedef vector<VD> VVD;
-typedef vector<int> VI;
-
 double MinCostMatching(const VVD &cost, VI &Lmate, VI &Rmate) {
   int n = int(cost.size());
   // construct dual feasible solution

+ 0 - 14
code/MinCostMaxFlow.cc

@@ -2,30 +2,16 @@
 // matrix (Edmonds and Karp 1972).  This implementation keeps track of
 // forward and reverse edges separately (so you can set cap[i][j] !=
 // cap[j][i]).  For a regular max flow, set all edge costs to 0.
-//
 // Running time, O(|V|^2) cost per augmentation
 //     max flow:           O(|V|^3) augmentations
 //     min cost max flow:  O(|V|^4 * MAX_EDGE_COST) augmentations
-//
 // INPUT:
 //     - graph, constructed using AddEdge()
 //     - source
 //     - sink
-//
 // OUTPUT:
 //     - (maximum flow value, minimum cost value)
 //     - To obtain the actual flow, look at positive values only.
-
-typedef vector<int> VI;
-typedef vector<VI> VVI;
-typedef long long L;
-typedef vector<L> VL;
-typedef vector<VL> VVL;
-typedef pair<int, int> PII;
-typedef vector<PII> VPII;
-
-const L INF = numeric_limits<L>::max() / 4;
-
 struct MinCostMaxFlow {
   int N;
   VVL cap, flow, cost;

+ 13 - 29
code/MinCut.cc

@@ -1,30 +1,14 @@
 // Adjacency matrix implementation of Stoer-Wagner min cut algorithm.
-//
 // Running time:
 //     O(|V|^3)
-//
-// INPUT: 
+// INPUT:
 //     - graph, constructed using AddEdge()
-//
 // OUTPUT:
 //     - (min cut value, nodes in half of min cut)
-
-#include <cmath>
-#include <vector>
-#include <iostream>
-
-using namespace std;
-
-typedef vector<int> VI;
-typedef vector<VI> VVI;
-
-const int INF = 1000000000;
-
 pair<int, VI> GetMinCut(VVI &weights) {
   int N = weights.size();
   VI used(N), cut, best_cut;
   int best_weight = -1;
-  
   for (int phase = N-1; phase >= 0; phase--) {
     VI w = weights[0];
     VI added = used;
@@ -33,20 +17,20 @@ pair<int, VI> GetMinCut(VVI &weights) {
       prev = last;
       last = -1;
       for (int j = 1; j < N; j++)
-	if (!added[j] && (last == -1 || w[j] > w[last])) last = j;
+      if (!added[j] && (last == -1 || w[j] > w[last])) last = j;
       if (i == phase-1) {
-	for (int j = 0; j < N; j++) weights[prev][j] += weights[last][j];
-	for (int j = 0; j < N; j++) weights[j][prev] = weights[prev][j];
-	used[last] = true;
-	cut.push_back(last);
-	if (best_weight == -1 || w[last] < best_weight) {
-	  best_cut = cut;
-	  best_weight = w[last];
-	}
+        for (int j = 0; j < N; j++) weights[prev][j] += weights[last][j];
+        for (int j = 0; j < N; j++) weights[j][prev] = weights[prev][j];
+        used[last] = true;
+        cut.push_back(last);
+        if (best_weight == -1 || w[last] < best_weight) {
+          best_cut = cut;
+          best_weight = w[last];
+        }
       } else {
-	for (int j = 0; j < N; j++)
-	  w[j] += weights[last][j];
-	added[last] = true;
+        for (int j = 0; j < N; j++)
+          w[j] += weights[last][j];
+        added[last] = true;
       }
     }
   }

+ 0 - 6
code/PushRelabel.cc

@@ -4,22 +4,16 @@
 // random problems with 10000 vertices and 1000000 edges in a few
 // seconds, though it is possible to construct test cases that
 // achieve the worst-case.
-//
 // Running time:
 //     O(|V|^3)
-//
 // INPUT:
 //     - graph, constructed using AddEdge()
 //     - source
 //     - sink
-//
 // OUTPUT:
 //     - maximum flow value
 //     - To obtain the actual flow values, look at all edges with
 //       capacity > 0 (zero capacity edges are residual edges).
-
-typedef long long LL;
-
 struct Edge {
   int from, to, cap, flow, index;
   Edge(int from, int to, int cap, int flow, int index) :

+ 0 - 1
code/RabinKarp.cpp

@@ -1,7 +1,6 @@
 // recherche de motif 2D
 // input : text n*m matrix; pattern x*y matrix
 // compléxité : linéaire
-#define uint unsigned int
 const int A=2001, B = 2001; // maximum sizes
 const uint E=27;
 char text[A][A], patt[B][B]; // [x][y]

+ 9 - 27
code/ReducedRowEchelonForm.cc

@@ -1,37 +1,24 @@
-// Reduced row echelon form via Gauss-Jordan elimination 
+// Reduced row echelon form via Gauss-Jordan elimination
 // with partial pivoting.  This can be used for computing
 // the rank of a matrix.
-//
 // Running time: O(n^3)
-//
 // INPUT:    a[][] = an nxn matrix
-//
 // OUTPUT:   rref[][] = an nxm matrix (stored in a[][])
 //           returns rank of a[][]
-
-#include <iostream>
-#include <vector>
-#include <cmath>
-
-using namespace std;
-
-const double EPSILON = 1e-10;
-
 typedef double T;
 typedef vector<T> VT;
 typedef vector<VT> VVT;
-
 int rref(VVT &a) {
   int n = a.size();
   int m = a[0].size();
   int r = 0;
   for (int c = 0; c < m; c++) {
     int j = r;
-    for (int i = r+1; i < n; i++) 
+    for (int i = r+1; i < n; i++)
       if (fabs(a[i][c]) > fabs(a[j][c])) j = i;
-    if (fabs(a[j][c]) < EPSILON) continue;
+    if (fabs(a[j][c]) < EPS) continue;
     swap(a[j], a[r]);
-   
+
     T s = 1.0 / a[r][c];
     for (int j = 0; j < m; j++) a[r][j] *= s;
     for (int i = 0; i < n; i++) if (i != r) {
@@ -42,24 +29,20 @@ int rref(VVT &a) {
   }
   return r;
 }
-
-int main(){
+int main() {
   const int n = 5;
   const int m = 4;
   double A[n][m] = { {16,2,3,13},{5,11,10,8},{9,7,6,12},{4,14,15,1},{13,21,21,13} };
   VVT a(n);
   for (int i = 0; i < n; i++)
     a[i] = VT(A[i], A[i] + n);
-  
   int rank = rref (a);
-  
   // expected: 4
   cout << "Rank: " << rank << endl;
-  
-  // expected: 1 0 0 1 
-  //           0 1 0 3 
-  //           0 0 1 -3 
-  //           0 0 0 2.78206e-15 
+  // expected: 1 0 0 1
+  //           0 1 0 3
+  //           0 0 1 -3
+  //           0 0 0 2.78206e-15
   //           0 0 0 3.22398e-15
   cout << "rref: " << endl;
   for (int i = 0; i < 5; i++){
@@ -67,5 +50,4 @@ int main(){
       cout << a[i][j] << ' ';
     cout << endl;
   }
-  
 }

+ 25 - 20
code/SCC.cc

@@ -1,36 +1,41 @@
-#include<memory.h>
-struct edge{int e, nxt;};
+struct edge { int e, nxt; };
 int V, E;
 edge e[MAXE], er[MAXE];
 int sp[MAXV], spr[MAXV];
 int group_cnt, group_num[MAXV];
 bool v[MAXV];
 int stk[MAXV];
-void fill_forward(int x)
-{
+void fill_forward(int x) {
   int i;
   v[x]=true;
-  for(i=sp[x];i;i=e[i].nxt) if(!v[e[i].e]) fill_forward(e[i].e);
-  stk[++stk[0]]=x;
+  for(i = sp[x]; i; i = e[i].nxt)
+    if(!v[e[i].e])
+      fill_forward(e[i].e);
+  stk[++stk[0]] = x;
 }
-void fill_backward(int x)
-{
+void fill_backward(int x) {
   int i;
   v[x]=false;
-  group_num[x]=group_cnt;
-  for(i=spr[x];i;i=er[i].nxt) if(v[er[i].e]) fill_backward(er[i].e);
+  group_num[x] = group_cnt;
+  for(i = spr[x]; i; i = er[i].nxt)
+    if(v[er[i].e])
+      fill_backward(er[i].e);
 }
-void add_edge(int v1, int v2) //add edge v1->v2
-{
-  e [++E].e=v2; e [E].nxt=sp [v1]; sp [v1]=E;
-  er[  E].e=v1; er[E].nxt=spr[v2]; spr[v2]=E;
+void add_edge(int v1, int v2) { //add edge v1->v2
+  e [++E].e = v2; e [E].nxt = sp [v1]; sp [v1] = E;
+  er[  E].e = v1; er[E].nxt = spr[v2]; spr[v2] = E;
 }
-void SCC()
-{
+void SCC() {
   int i;
-  stk[0]=0;
+  stk[0] = 0;
   memset(v, false, sizeof(v));
-  for(i=1;i<=V;i++) if(!v[i]) fill_forward(i);
-  group_cnt=0;
-  for(i=stk[0];i>=1;i--) if(v[stk[i]]){group_cnt++; fill_backward(stk[i]);}
+  for(i = 1; i <= V; i++)
+    if(!v[i])
+      fill_forward(i);
+  group_cnt = 0;
+  for(i = stk[0]; i >= 1; i--)
+    if(v[stk[i]]) {
+      group_cnt++;
+      fill_backward(stk[i]);
+    }
 }

+ 2 - 3
code/SegmentTree.cpp

@@ -1,7 +1,6 @@
 // segment tree for minimum and sum
 // root is in tree[1], children in tree[2i] and tree[2i+1]
 // all ranges in build/updates/queries are 0-based
-const int INFI=1000000000;
 struct Node {
   int min, sum, up, size; // size of subtree
   void update(int x) {
@@ -10,7 +9,7 @@ struct Node {
     up += x;
   }
   Node() {
-    min = INFI;
+    min = INF;
     up = sum = size = 0;
   }
 };
@@ -45,7 +44,7 @@ void push(int v) {
 pair<int, int> query_aux(int v, int left, int right, int l, int r) {
   push(v);
   if(right <= l || r <= left) // outside
-    return {INFI, 0};
+    return {INF, 0};
   if(l <= left && right <= r) // inside
     return {tree[v].min, tree[v].sum};
   int m = (left+right)/2;

+ 1 - 0
code/SegmentTree_test.cpp

@@ -1,6 +1,7 @@
 #include <bits/stdc++.h>
 using namespace std;
 
+#define INF 1000000000
 #include "SegmentTree.cpp"
 
 int main() {

+ 0 - 1
code/Simplex.cc

@@ -13,7 +13,6 @@
 typedef long double DOUBLE;
 typedef vector<DOUBLE> VD;
 typedef vector<VD> VVD;
-typedef vector<int> VI;
 const DOUBLE EPS = 1e-9;
 struct LPSolver {
   int m, n;

+ 22 - 35
code/SuffixArray.cc

@@ -1,57 +1,44 @@
 // Suffix array construction in O(L log^2 L) time.  Routine for
 // computing the length of the longest common prefix of any two
 // suffixes in O(log L) time.
-//
 // INPUT:   string s
-//
 // OUTPUT:  array suffix[] such that suffix[i] = index (from 0 to L-1)
 //          of substring s[i...L-1] in the list of sorted suffixes.
 //          That is, if we take the inverse of the permutation suffix[],
 //          we get the actual suffix array.
-
-#include <vector>
-#include <iostream>
-#include <string>
-
-using namespace std;
-
 struct SuffixArray {
   const int L;
   string s;
-  vector<vector<int> > P;
-  vector<pair<pair<int,int>,int> > M;
-
-  SuffixArray(const string &s) : L(s.length()), s(s), P(1, vector<int>(L, 0)), M(L) {
-    for (int i = 0; i < L; i++) P[0][i] = int(s[i]);
-    for (int skip = 1, level = 1; skip < L; skip *= 2, level++) {
-      P.push_back(vector<int>(L, 0));
-      for (int i = 0; i < L; i++) 
-	M[i] = make_pair(make_pair(P[level-1][i], i + skip < L ? P[level-1][i + skip] : -1000), i);
-      sort(M.begin(), M.end());
-      for (int i = 0; i < L; i++) 
-	P[level][M[i].second] = (i > 0 && M[i].first == M[i-1].first) ? P[level][M[i-1].second] : i;
-    }    
+  VVI P;
+  vector<pair<PII,int> > M;
+  SuffixArray(const string &s) : L(s.length()), s(s), P(1, VI(L, 0)), M(L) {
+    for(int i = 0; i < L; i++)
+      P[0][i] = int(s[i]);
+    for(int skip = 1, level = 1; skip < L; skip *= 2, level++) {
+      P.push_back(VI(L, 0));
+      for(int i = 0; i < L; i++)
+        M[i] = make_pair(make_pair(P[level-1][i], i + skip < L ? P[level-1][i + skip] : -1000), i);
+        sort(M.begin(), M.end());
+        for(int i = 0; i < L; i++)
+          P[level][M[i].second] = (i > 0 && M[i].first == M[i-1].first) ? P[level][M[i-1].second] : i;
+    }
   }
-
-  vector<int> GetSuffixArray() { return P.back(); }
-
+  VI GetSuffixArray() { return P.back(); }
   // returns the length of the longest common prefix of s[i...L-1] and s[j...L-1]
   int LongestCommonPrefix(int i, int j) {
     int len = 0;
-    if (i == j) return L - i;
-    for (int k = P.size() - 1; k >= 0 && i < L && j < L; k--) {
+    if(i == j) return L - i;
+    for(int k = P.size() - 1; k >= 0 && i < L && j < L; k--) {
       if (P[k][i] == P[k][j]) {
-	i += 1 << k;
-	j += 1 << k;
-	len += 1 << k;
+        i += 1 << k;
+        j += 1 << k;
+        len += 1 << k;
       }
     }
     return len;
   }
 };
-
 int main() {
-
   // bobocel is the 0'th suffix
   //  obocel is the 5'th suffix
   //   bocel is the 1'st suffix
@@ -60,11 +47,11 @@ int main() {
   //      el is the 3'rd suffix
   //       l is the 4'th suffix
   SuffixArray suffix("bobocel");
-  vector<int> v = suffix.GetSuffixArray();
-  
+  VI v = suffix.GetSuffixArray();
   // Expected output: 0 5 1 6 2 3 4
   //                  2
-  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
+  for(int i = 0; i < v.size(); i++)
+    cout << v[i] << " ";
   cout << endl;
   cout << suffix.LongestCommonPrefix(0, 2) << endl;
 }

+ 0 - 1
code/kmp.cpp

@@ -7,7 +7,6 @@ void kmp_pre(char x[], int m, int pnext[]) {
     pnext[++i]=++j;
   }
 }
-
 // x pattern, y text
 // answer: first index (starting with 0)
 // KMP_Count : # occurences

+ 4 - 10
code/suffix.cpp

@@ -1,11 +1,5 @@
-#include <iostream>
-#include <tuple>
-using namespace std;
-
-const int inf = 40000;
-const int log_inf = 17;
-
-void suffix_tree(string s, int p[log_inf][inf], int &k) {
+const int LOG_INF = 17;
+void suffix_tree(string s, int p[LOG_INF][inf], int &k) {
 	int N = s.length();
 	for(int i = 0 ; i < N ; i++)
 		p[0][i] = s[i] - 'a';
@@ -27,13 +21,13 @@ void suffix_tree(string s, int p[log_inf][inf], int &k) {
 }
 
 void lcp(int x, int y) {
-	
+
 }
 
 int main() {
 	string s;
 	cin >> s;
-	int k, p[log_inf][inf];
+	int k, p[LOG_INF][inf];
 	suffix_tree(s, p, k);
 	int w[inf];
 	for(int i = 0 ; i < s.length() ; i++)