#include #include #include // <-- Begin of Algorithm Implementation/Geometry/Convex hull/Monotone chain --> // https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#C.2B.2B // Implementation of Andrew's monotone chain 2D convex hull algorithm. // Asymptotic complexity: O(n log n). // Practical performance: 0.5-1.0 seconds for n=1000000 on a 1GHz machine. #include #include using namespace std; typedef long coord_t; // coordinate type typedef long long coord2_t; // must be big enough to hold 2*max(|coordinate|)^2 struct Point { Point() : x(0), y(0), r2(0) {} Point(long long xx, long long yy) : x(xx), y(yy), r2(xx*xx + yy*yy) {} coord_t x, y; coord2_t r2; bool operator <(const Point &p) const { return x < p.x || (x == p.x && y < p.y); } }; // 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product. // Returns a positive value, if OAB makes a counter-clockwise turn, // negative for clockwise turn, and zero if the points are collinear. coord2_t cross(const Point &O, const Point &A, const Point &B) { return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); } // Returns a list of points on the convex hull in counter-clockwise order. // Note: the last point in the returned list is the same as the first one. vector convex_hull(vector P) { int n = P.size(), k = 0; if (n == 1) return P; vector H(2*n); // Sort points lexicographically sort(P.begin(), P.end()); // Build lower hull for (int i = 0; i < n; ++i) { while (k >= 2 && cross(H[k-2], H[k-1], P[i]) <= 0) k--; H[k++] = P[i]; } // Build upper hull for (int i = n-2, t = k+1; i >= 0; i--) { while (k >= t && cross(H[k-2], H[k-1], P[i]) <= 0) k--; H[k++] = P[i]; } H.resize(k-1); return H; } // <-- End of Algorithm Implementation/Geometry/Convex hull/Monotone chain --> #define MAX 10000 struct QueueCompare { bool operator()(const Point& lhs, const Point& rhs) const { return lhs.r2 > rhs.r2; } }; int main() { vector hull; // lattice points priority_queue, QueueCompare> queue; for (int x=0; x<=MAX+1; x++) { queue.push(Point(x, 0)); } for (int n=0; n<=MAX; n++) { while (queue.top().r2 <= n*n) { Point p = queue.top(); queue.pop(); hull.push_back(p); queue.push(Point(p.x, p.y+1)); } hull = convex_hull(hull); if (n==0) { // special case printf("%d %d\n", n, hull.size()); } else { printf("%d %d\n", n, (hull.size()-2) * 4); } fflush(stdout); } return 0; }